Class: DataloaderRelationProxy::Collection
- Inherits:
-
Object
- Object
- DataloaderRelationProxy::Collection
- Defined in:
- lib/dataloader_relation_proxy/collection.rb
Overview
Represents an ActiveRecord collection and provides an enumerable that returns underlying objects that are instances of DataloaderRelationProxy::Record
Class Method Summary collapse
-
.load_target ⇒ Object
Need to ensure the collection elements are wrapped in DataloaderRelationProxy objects.
Instance Method Summary collapse
-
#initialize(object, dataloader) ⇒ Collection
constructor
A new instance of Collection.
- #loaded? ⇒ Boolean
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
Constructor Details
#initialize(object, dataloader) ⇒ Collection
Returns a new instance of Collection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dataloader_relation_proxy/collection.rb', line 8 def initialize(object, dataloader) @object = object @dataloader = dataloader # Need to ensure the collection elements are wrapped in # DataloaderRelationProxy objects. I'm sure there's a nicer alternative. # # rubocop:disable Lint/NestedMethodDefinition def @object.load_target @association.load_target.map do |record| DataloaderRelationProxy.for(record.class).new(record, @dataloader) end end # rubocop:enable Lint/NestedMethodDefinition end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dataloader_relation_proxy/collection.rb', line 24 def method_missing(method_name, *args, &block) raise NoMethodError, "Missing method '#{method_name}' on #{@object}" unless @object.respond_to?(method_name) result = @object.send(method_name, *args, &block) if result.is_a?(ActiveRecord::Base) DataloaderRelationProxy.for(result.class).new(result, @dataloader) else result end end |
Class Method Details
.load_target ⇒ Object
Need to ensure the collection elements are wrapped in DataloaderRelationProxy objects. I’m sure there’s a nicer alternative.
rubocop:disable Lint/NestedMethodDefinition
16 17 18 19 20 |
# File 'lib/dataloader_relation_proxy/collection.rb', line 16 def @object.load_target @association.load_target.map do |record| DataloaderRelationProxy.for(record.class).new(record, @dataloader) end end |
Instance Method Details
#loaded? ⇒ Boolean
39 40 41 |
# File 'lib/dataloader_relation_proxy/collection.rb', line 39 def loaded? true end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
35 36 37 |
# File 'lib/dataloader_relation_proxy/collection.rb', line 35 def respond_to_missing?(method_name, _include_private = false) @object.respond_to?(method_name) end |