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.



23
24
25
# File 'lib/nested_record/collection_proxy.rb', line 23

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



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

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



18
19
20
# File 'lib/nested_record/collection_proxy.rb', line 18

def __nested_record_setup__
  @setup
end

.subclass_for(setup) ⇒ Object



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

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



47
48
49
# File 'lib/nested_record/collection_proxy.rb', line 47

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

#__collection__Object



60
61
62
# File 'lib/nested_record/collection_proxy.rb', line 60

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

#build(attributes = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/nested_record/collection_proxy.rb', line 40

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

#find_or_initialize_by(attributes) ⇒ Object



51
52
53
54
55
56
# File 'lib/nested_record/collection_proxy.rb', line 51

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)


36
37
38
# File 'lib/nested_record/collection_proxy.rb', line 36

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