Class: Isomorphic::Node::Collection

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

Instance Attribute Summary collapse

Attributes inherited from AbstractNode

#factory, #inflector, #options, #parent

Instance Method Summary collapse

Methods included from Internal::InstanceMethodsForCollection

#association, #association_for, #element, #guard_association_for, #namespace_association_for

Methods inherited from AbstractNode

#to_isomorphism_for

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

#initialize(parent, method_name, klass, **options, &block) ⇒ Collection

Returns a new instance of Collection.



759
760
761
762
763
764
765
766
767
768
# File 'lib/isomorphic/node.rb', line 759

def initialize(parent, method_name, klass, **options, &block)
  super(parent, **options, &block)

  unless klass.is_a?(::Class) && (klass.parents[-2] == parent.inflector.base)
    raise Isomorphic::InvalidNodeClass.new(nil, klass)
  end

  @method_name = method_name
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



757
758
759
# File 'lib/isomorphic/node.rb', line 757

def klass
  @klass
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



755
756
757
# File 'lib/isomorphic/node.rb', line 755

def method_name
  @method_name
end

Instance Method Details

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



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/isomorphic/node.rb', line 770

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

  instance.send(method_name).try { |instance_for_node|
    is_valid = instance_for_node.instance_of?(klass) && all_attributes_eql?(inflector.base, instance_for_node, options[:attributes])

    if is_valid
      is_present ||= @__child_nodes.inject(false) { |acc, child_node| child_node.from_isomorphism(scope, instance_for_node, record, xmlattr_acc).nil? ? acc : true }
    end
  }

  if is_present
    record
  else
    nil
  end
end

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



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/isomorphic/node.rb', line 788

def to_isomorphism(scope, record, instance = nil)
  is_present = false

  instance_for_node = instance.send(method_name) || factory.for(klass)

  options[:attributes].try { |attributes|
    factory.update_attributes(instance_for_node, attributes)
  }

  is_present ||= @__child_nodes.inject(false) { |acc, child_node| child_node.to_isomorphism(scope, record, instance_for_node).nil? ? acc : true }

  if is_present || options[:allow_blank]
    instance.send(:"#{method_name}=", instance_for_node)

    instance_for_node
  else
    nil
  end
end