Class: NestedRecord::CollectionProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/nested_record/collection_proxy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



21
22
23
# File 'lib/nested_record/collection_proxy.rb', line 21

def initialize(owner)
  @owner = owner
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/nested_record/collection_proxy.rb', line 25

def method_missing(method_name, *args, &block)
  collection = __collection__
  if collection.respond_to? method_name
    collection.public_send(method_name, *args, &block)
  else
    super
  end
end

Class Method Details

.__nested_record_setup__Object



16
17
18
# File 'lib/nested_record/collection_proxy.rb', line 16

def __nested_record_setup__
  @setup
end

.subclass_for(setup) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/nested_record/collection_proxy.rb', line 6

def subclass_for(setup)
  Class.new(self) do
    methods = setup.collection_class.public_instance_methods
    methods -= NestedRecord::Collection.public_instance_methods
    methods -= NestedRecord::CollectionProxy.public_instance_methods(false)
    def_delegators :__collection__, *methods unless methods.empty?
    @setup = setup
  end
end

Instance Method Details

#__build__(attributes) ⇒ Object



45
46
47
# File 'lib/nested_record/collection_proxy.rb', line 45

def __build__(attributes)
  __collection__.build(attributes)
end

#__collection__Object



58
59
60
# File 'lib/nested_record/collection_proxy.rb', line 58

def __collection__
  @owner.read_attribute(self.class.__nested_record_setup__.name)
end

#build(attributes = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/nested_record/collection_proxy.rb', line 38

def build(attributes = {})
  __collection__.build(attributes) do |record|
    ensure_primary! record
    yield record if block_given?
  end
end

#find_or_initialize_by(attributes) ⇒ Object



49
50
51
52
53
54
# File 'lib/nested_record/collection_proxy.rb', line 49

def find_or_initialize_by(attributes)
  __collection__.find_or_initialize_by(attributes) do |record|
    ensure_primary! record
    yield record if block_given?
  end
end

#respond_to_missing?(method_name, _) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/nested_record/collection_proxy.rb', line 34

def respond_to_missing?(method_name, _)
  super || __collection__.respond_to?(method_name)
end