Class: Isomorphic::Node::Association

Inherits:
AbstractNode show all
Includes:
Internal::InstanceMethodsForMember
Defined in:
lib/isomorphic/node.rb

Direct Known Subclasses

NamespaceAssociation

Instance Attribute Summary

Attributes inherited from AbstractNode

#factory, #inflector, #options, #parent

Instance Method Summary collapse

Methods included from Internal::InstanceMethodsForMember

#attribute, #attribute_for, #collection, #from, #guard_attribute_for, #member, #namespace, #namespace_attribute_for, #to

Methods inherited from AbstractNode

#initialize, #to_isomorphism_for

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

This class inherits a constructor from Isomorphic::Node::AbstractNode

Instance Method Details

#from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new) ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/isomorphic/node.rb', line 571

def from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  is_present = false

  instance.each do |instance_for_node|
    _set(scope, record, instance_for_node, xmlattr_acc).try { |record_for_node|
      is_present ||= true

      @__child_nodes.inject(false) { |acc, child_node| child_node.from_isomorphism(scope, instance_for_node, record_for_node, xmlattr_acc).nil? ? acc : true }
    }
  end

  if is_present
    record
  else
    nil
  end
end

#to_isomorphism(scope, record, instance = nil) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/isomorphic/node.rb', line 589

def to_isomorphism(scope, record, instance = nil)
  instance_for_node_or_array = _get(scope, record).try { |instance_for_node_or_hash|
    @__cache_for_to_isomorphism_for[record] ||= instance_for_node_or_hash

    if instance_for_node_or_hash.is_a?(::Hash)
      instance_for_node_or_hash.values
    else
      instance_for_node_or_hash
    end
  }

  unless instance_for_node_or_array.nil?
    array = (instance_for_node_or_array.instance_of?(::Array) ? instance_for_node_or_array : [instance_for_node_or_array]).reject(&:nil?)

    if array.any? || options[:allow_blank]
      array.each do |instance_for_node|
        @__child_nodes.inject(false) { |acc, child_node| child_node.to_isomorphism(scope, record, instance_for_node).nil? ? acc : true }

        instance.send(:<<, instance_for_node)
      end

      instance_for_node_or_array
    else
      nil
    end
  else
    nil
  end
end