Class: Isomorphic::Node::Member

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, method_name, klass, **options, &block) ⇒ Member

Returns a new instance of Member.



942
943
944
945
946
947
948
949
950
951
# File 'lib/isomorphic/node.rb', line 942

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.



940
941
942
# File 'lib/isomorphic/node.rb', line 940

def klass
  @klass
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



938
939
940
# File 'lib/isomorphic/node.rb', line 938

def method_name
  @method_name
end

Instance Method Details

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



953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'lib/isomorphic/node.rb', line 953

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(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



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/isomorphic/node.rb', line 971

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