Class: Superform::NamespaceCollection

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/superform/namespace_collection.rb

Overview

A NamespaceCollection represents values that are collections of namespaces. For example, a User ActiveRecord object might have many Addresses. Each individual address is then delegated out to a Namespace object.

Instance Attribute Summary collapse

Attributes inherited from Node

#key, #parent

Instance Method Summary collapse

Constructor Details

#initialize(key, parent:, form: parent.form, &template) ⇒ NamespaceCollection

Returns a new instance of NamespaceCollection.



10
11
12
13
14
15
# File 'lib/superform/namespace_collection.rb', line 10

def initialize(key, parent:, form: parent.form, &template)
  super(key, parent:)
  @template = template
  @form = form
  @namespaces = enumerate(parent_collection)
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



8
9
10
# File 'lib/superform/namespace_collection.rb', line 8

def form
  @form
end

Instance Method Details

#assign(array) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/superform/namespace_collection.rb', line 21

def assign(array)
  # The problem with zip-ing the array is if I need to add new
  # elements to it and wrap it in the namespace.
  zip(array) do |namespace, hash|
    namespace.assign hash
  end
end

#eachObject



29
30
31
# File 'lib/superform/namespace_collection.rb', line 29

def each(&)
  @namespaces.each(&)
end

#serializeObject



17
18
19
# File 'lib/superform/namespace_collection.rb', line 17

def serialize
  map(&:serialize)
end