Class: MongoMapper::EagerIncluder

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/eager_includer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_or_records, association_name) ⇒ EagerIncluder

Returns a new instance of EagerIncluder.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mongo_mapper/eager_includer.rb', line 39

def initialize(record_or_records, association_name)
  association_name = association_name.to_sym

  @records = Array(record_or_records)

  if @records.length == 0
    return
  end

  @association_name = association_name.to_sym
  @association = @records.first.associations[association_name]
  if !@association
    raise "Could not find association `#{association_name}` on instance of #{@records.first.class}"
  end

  @proxy_class = @association.proxy_class
end

Class Method Details

.clear_cache!Object



28
29
30
# File 'lib/mongo_mapper/eager_includer.rb', line 28

def clear_cache!
  @cache = {}
end

.eager_include(record_or_records, *association_names, &block) ⇒ Object



13
14
15
16
17
# File 'lib/mongo_mapper/eager_includer.rb', line 13

def eager_include(record_or_records, *association_names, &block)
  association_names.each do |association_name|
    new(record_or_records, association_name).eager_include(&block)
  end
end

.enabled=(bool) ⇒ Object



9
10
11
# File 'lib/mongo_mapper/eager_includer.rb', line 9

def enabled=(bool)
  @enabled = bool
end

.enabled?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/mongo_mapper/eager_includer.rb', line 5

def enabled?
  (@enabled == true || @enabled == false) ? @enabled : true
end

.read_from_cache(object_id, association_name) ⇒ Object



24
25
26
# File 'lib/mongo_mapper/eager_includer.rb', line 24

def read_from_cache(object_id, association_name)
  cache[association_name][object_id]
end

.write_to_cache(object_id, association_name, value) ⇒ Object



19
20
21
22
# File 'lib/mongo_mapper/eager_includer.rb', line 19

def write_to_cache(object_id, association_name, value)
  cache[association_name] ||= {}
  cache[association_name][object_id] = value
end

Instance Method Details

#eager_include(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mongo_mapper/eager_includer.rb', line 61

def eager_include(&block)
  return if !enabled?

  if @records.length == 0
    return
  end

  if @proxy_class == MongoMapper::Plugins::Associations::ManyDocumentsProxy
    eager_include_has_many(&block)
  elsif @proxy_class == MongoMapper::Plugins::Associations::BelongsToProxy
    eager_include_belongs_to(&block)
  elsif @proxy_class == MongoMapper::Plugins::Associations::OneProxy
    eager_include_has_one(&block)
  elsif @proxy_class == MongoMapper::Plugins::Associations::InArrayProxy
    eager_include_has_many_in(&block)
  else
    raise NotImplementedError, "#{@proxy_class} not supported yet!"
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mongo_mapper/eager_includer.rb', line 57

def enabled?
  self.class.enabled?
end