Class: Isomorphic::Node::Element

Inherits:
AbstractNode show all
Includes:
Internal::InstanceMethodsForMember
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::InstanceMethodsForMember

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

Methods inherited from AbstractNode

#to_isomorphism_for

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

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

Returns a new instance of Element.



814
815
816
817
818
819
820
821
822
# File 'lib/isomorphic/node.rb', line 814

def initialize(parent, 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

  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



812
813
814
# File 'lib/isomorphic/node.rb', line 812

def klass
  @klass
end

Instance Method Details

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



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/isomorphic/node.rb', line 824

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

  instance.try(:each) { |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(options[:allow_blank]) { |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



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/isomorphic/node.rb', line 842

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

  instance_for_node = 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(:<<, instance_for_node)

    instance_for_node
  else
    nil
  end
end