module NoBrainer::Document::LazyFetch::ClassMethods

Public Instance Methods

all() click to toggle source
Calls superclass method
# File lib/no_brainer/document/lazy_fetch.rb, line 67
def all
  criteria = super
  criteria = criteria.lazy_fetch(*self.fields_to_lazy_fetch) if self.fields_to_lazy_fetch.present?
  criteria
end
field(attr, options={}) click to toggle source
Calls superclass method
# File lib/no_brainer/document/lazy_fetch.rb, line 31
def field(attr, options={})
  super
  attr = attr.to_s

  case options[:lazy_fetch]
  when true  then subclass_tree.each { |subclass| subclass.fields_to_lazy_fetch << attr }
  when false then subclass_tree.each { |subclass| subclass.fields_to_lazy_fetch.delete(attr) }
  end

  inject_in_layer :lazy_fetch do
    # Lazy loading can also specified through criteria, we have to define
    # this method regardless of the provided options.
    define_method("#{attr}") do
      return super() unless @lazy_fetch

      begin
        super()
      rescue NoBrainer::Error::MissingAttribute => e
        raise e unless attr.in?(@lazy_fetch)
        reload(:pluck => attr, :keep_ivars => true)
        @lazy_fetch.delete(attr)
        clear_missing_field(attr)
        retry
      end
    end
  end
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/no_brainer/document/lazy_fetch.rb, line 26
def inherited(subclass)
  subclass.fields_to_lazy_fetch = self.fields_to_lazy_fetch.dup
  super
end
remove_field(attr, options={}) click to toggle source
Calls superclass method
# File lib/no_brainer/document/lazy_fetch.rb, line 59
def remove_field(attr, options={})
  subclass_tree.each { |subclass| subclass.fields_to_lazy_fetch.delete(attr.to_s) }
  inject_in_layer :lazy_fetch do
    remove_method("#{attr}")
  end
  super
end