Class: MongoMapper::EagerIncluder
- Inherits:
-
Object
- Object
- MongoMapper::EagerIncluder
- Defined in:
- lib/mongo_mapper/eager_include.rb
Class Method Summary collapse
- .clear_cache! ⇒ Object
- .eager_include(record_or_records, *association_names, &block) ⇒ Object
- .enabled=(bool) ⇒ Object
- .enabled? ⇒ Boolean
- .read_from_cache(object_id, association_name) ⇒ Object
- .write_to_cache(object_id, association_name, value) ⇒ Object
Instance Method Summary collapse
- #eager_include(&block) ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(record_or_records, association_name) ⇒ EagerIncluder
constructor
A new instance of EagerIncluder.
Constructor Details
#initialize(record_or_records, association_name) ⇒ EagerIncluder
Returns a new instance of EagerIncluder.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongo_mapper/eager_include.rb', line 40 def initialize(record_or_records, association_name) @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
29 30 31 |
# File 'lib/mongo_mapper/eager_include.rb', line 29 def clear_cache! @cache = {} end |
.eager_include(record_or_records, *association_names, &block) ⇒ Object
14 15 16 17 18 |
# File 'lib/mongo_mapper/eager_include.rb', line 14 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
10 11 12 |
# File 'lib/mongo_mapper/eager_include.rb', line 10 def enabled=(bool) @enabled = bool end |
.enabled? ⇒ Boolean
6 7 8 |
# File 'lib/mongo_mapper/eager_include.rb', line 6 def enabled? (@enabled == true || @enabled == false) ? @enabled : true end |
.read_from_cache(object_id, association_name) ⇒ Object
25 26 27 |
# File 'lib/mongo_mapper/eager_include.rb', line 25 def read_from_cache(object_id, association_name) cache[association_name][object_id] end |
.write_to_cache(object_id, association_name, value) ⇒ Object
20 21 22 23 |
# File 'lib/mongo_mapper/eager_include.rb', line 20 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mongo_mapper/eager_include.rb', line 60 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
56 57 58 |
# File 'lib/mongo_mapper/eager_include.rb', line 56 def enabled? self.class.enabled? end |