Module: Isomorphic::Node::Internal::InstanceMethodsForCollection

Extended by:
ActiveSupport::Concern
Included in:
Collection, GuardCollection, RootCollection
Defined in:
lib/isomorphic/node.rb

Overview

Included when the base class is a collection.

Instance Method Summary collapse

Instance Method Details

#association(**options) {|node| ... } ⇒ self

Build an Isomorphic node for an Active Record association.

Parameters:

  • options (Hash<Symbol, Object>)

    the options

Options Hash (**options):

  • :allow_blank (Boolean) — default: false

    true if the new node should always return a non-nil target

  • :attributes (Hash<Symbol, Object>) — default: {}

    default attributes for the target

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (self)


137
138
139
140
141
# File 'lib/isomorphic/node.rb', line 137

def association(**options, &block)
  node = Isomorphic::Node::Association.new(self, **options, &block)
  @__child_nodes << node
  node
end

#association_for(lens_or_association, **options) {|node| ... } ⇒ self

Build an Isomorphic node for an Active Record association (as an attribute) using an Isomorphic lens.

Parameters:

  • lens_or_association (Isomorphic::Lens::AbstractLens, #to_s)

    the Isomorphic lens or the name of the Active Record association

  • options (Hash<Symbol, Object>)

    the options

Options Hash (**options):

  • :allow_blank (Boolean) — default: false

    true if the new node should always return a non-nil target

  • :attributes (Hash<Symbol, Object>) — default: {}

    default attributes for the target

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (self)


152
153
154
155
156
157
158
159
# File 'lib/isomorphic/node.rb', line 152

def association_for(lens_or_association, **options, &block)
  lens = lens_or_association.is_a?(Isomorphic::Lens::AbstractLens) ? lens_or_association : reflect_on_attribute(lens_or_association)

  association(**options.merge({
    get: ::Proc.new { |record| lens.get(record) },
    set: ::Proc.new { |record, value, xmlattr_acc| lens.set(record, value, xmlattr_acc) },
  }), &block)
end

#element(isomorphism_class, **options) {|node| ... } ⇒ self

Build an Isomorphic node for zero or more members of a collection.

Parameters:

  • isomorphism_class (Class)

    the class

  • options (Hash<Symbol, Object>)

    the options

Options Hash (**options):

  • :allow_blank (Boolean) — default: false

    true if the new node should always return a non-nil target

  • :attributes (Hash<Symbol, Object>) — default: {}

    default attributes for the target

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (self)


170
171
172
173
174
# File 'lib/isomorphic/node.rb', line 170

def element(isomorphism_class, **options, &block)
  node = Isomorphic::Node::Element.new(self, isomorphism_class, **options, &block)
  @__child_nodes << node
  node
end

#guard_association_for(lens_or_attribute_name, *values, **options) {|node| ... } ⇒ self

Build an Isomorphic node for a “guard” statement for an Active Record association using an Isomorphic lens.

Parameters:

  • lens_or_attribute_name (Isomorphic::Lens::AbstractLens, #to_s)

    the Isomorphic lens or the name of the Active Record association

  • values (Array<Object>)

    the included values

  • options (Hash<Symbol, Object>)

    the options

Options Hash (**options):

  • :allow_blank (Boolean) — default: false

    true if the new node should always return a non-nil target

  • :attributes (Hash<Symbol, Object>) — default: {}

    default attributes for the target

  • :default (Object) — default: nil

    the default value, where more than one included value is given

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (self)


187
188
189
190
191
192
193
# File 'lib/isomorphic/node.rb', line 187

def guard_association_for(lens_or_attribute_name, *values, **options, &block)
  lens = lens_or_attribute_name.is_a?(Isomorphic::Lens::AbstractLens) ? lens_or_attribute_name : reflect_on_attribute(lens_or_attribute_name)

  node = Isomorphic::Node::GuardCollection.new(self, lens, *values, **options, &block)
  @__child_nodes << node
  node
end

#namespace_association_for(namespace, terms, **options) {|node| ... } ⇒ self

Build an Isomorphic node for a value of a “namespace” statement that is within scope.

Parameters:

  • namespace (#to_s)

    the namespace name

  • terms (Array<Object>)

    the inflectable terms

  • options (Hash<Symbol, Object>)

    the options

Options Hash (**options):

  • :allow_blank (Boolean) — default: false

    true if the new node should always return a non-nil target

  • :attributes (Hash<Symbol, Object>) — default: {}

    default attributes for the target

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (self)

Raises:



206
207
208
209
210
211
212
# File 'lib/isomorphic/node.rb', line 206

def namespace_association_for(namespace, terms, **options, &block)
  key = inflector.isomorphism(terms)

  node = Isomorphic::Node::NamespaceAssociation.new(self, namespace, key, **options, &block)
  @__child_nodes << node
  node
end