Class: Isomorphic::Node::Namespace

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, namespace_name, lens, **options, &block) ⇒ Namespace

Returns a new instance of Namespace.



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/isomorphic/node.rb', line 999

def initialize(parent, namespace_name, lens, **options, &block)
  super(parent, **options, &block)

  unless (lens.is_a?(Isomorphic::Lens::Association) && lens.last_lens.is_a?(Isomorphic::Lens::Isomorphism)) || lens.is_a?(Isomorphic::Lens::Isomorphism)
    raise Isomorphic::LensInvalid.new(nil, lens)
  end

  @namespace_name = namespace_name

  @lens = lens
end

Instance Attribute Details

#lensObject (readonly)

Returns the value of attribute lens.



997
998
999
# File 'lib/isomorphic/node.rb', line 997

def lens
  @lens
end

#namespace_nameObject (readonly)

Returns the value of attribute namespace_name.



995
996
997
# File 'lib/isomorphic/node.rb', line 995

def namespace_name
  @namespace_name
end

Instance Method Details

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



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/isomorphic/node.rb', line 1011

def from_isomorphism(scope, instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  new_scope = scope.child(namespace_name)

  is_present = @__child_nodes.inject(false) { |acc, child_node| child_node.from_isomorphism(new_scope, instance, record, xmlattr_acc).nil? ? acc : true }

  lens.set(record, new_scope.values(namespace_name), xmlattr_acc)

  if is_present
    record
  else
    nil
  end
end

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



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/isomorphic/node.rb', line 1025

def to_isomorphism(scope, record, instance = nil)
  new_scope = scope.child(namespace_name)

  lens.get(record).try { |hash|
    hash.each do |key, value|
      new_scope.set(namespace_name, key, value)
    end
  }

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

  if is_present || options[:allow_blank]
    instance
  else
    nil
  end
end