Class: Isomorphic::Node::Root

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

Direct Known Subclasses

RootCollection, RootMember

Instance Attribute Summary collapse

Attributes inherited from AbstractNode

#factory, #inflector, #options, #parent

Instance Method Summary collapse

Methods inherited from AbstractNode

#to_isomorphism_for

Methods included from Internal::DeepEquals

#all_attributes_eql?

Constructor Details

#initialize(factory, inflector, record_class, klass, *args, &block) ⇒ Root

Returns a new instance of Root.



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
# File 'lib/isomorphic/node.rb', line 1148

def initialize(factory, inflector, record_class, klass, *args, &block)
  @factory = factory
  @inflector = inflector

  @record_class = record_class
  @klass = klass

  super(nil, *args, &block)

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



1146
1147
1148
# File 'lib/isomorphic/node.rb', line 1146

def args
  @args
end

#klassObject (readonly)

Returns the value of attribute klass.



1144
1145
1146
# File 'lib/isomorphic/node.rb', line 1144

def klass
  @klass
end

#record_classObject (readonly)

Returns the value of attribute record_class.



1142
1143
1144
# File 'lib/isomorphic/node.rb', line 1142

def record_class
  @record_class
end

Instance Method Details

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



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
# File 'lib/isomorphic/node.rb', line 1162

def from_isomorphism(instance, record = nil, xmlattr_acc = ::ActiveSupport::HashWithIndifferentAccess.new)
  unless instance.instance_of?(klass)
    raise Isomorphic::InvalidNodeObject.new(nil, klass, instance)
  end

  scope = Isomorphic::Node::Internal::Scope.new(nil, inflector)

  is_valid = all_attributes_eql?(inflector.base, instance, options[:attributes])

  if is_valid
    record_for_node = record || record_class.new

    @__child_nodes.each do |child_node|
      child_node.from_isomorphism(scope, instance, record_for_node, xmlattr_acc)
    end

    record_for_node
  else
    record
  end
end

#to_isomorphism(record, instance = nil) ⇒ Object



1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/isomorphic/node.rb', line 1184

def to_isomorphism(record, instance = nil)
  scope = Isomorphic::Node::Internal::Scope.new(nil, inflector)

  instance_for_node = instance || 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_for_node
  else
    nil
  end
end