Class: Representable::XML::PropertyBinding

Inherits:
Binding
  • Object
show all
Includes:
Binding::Object
Defined in:
lib/representable/bindings/xml_bindings.rb

Instance Attribute Summary

Attributes inherited from Binding

#represented, #user_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Binding::Object

#create_object, #deserialize, #serialize

Methods inherited from Binding

build, #compile_fragment, #get, #initialize, #read_fragment, #read_fragment_for, #set, #uncompile_fragment, #write_fragment, #write_fragment_for

Methods included from Binding::Prepare

#representer_module_for

Constructor Details

This class inherits a constructor from Representable::Binding

Class Method Details

.build_for(definition, *args) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/representable/bindings/xml_bindings.rb', line 9

def self.build_for(definition, *args)
  return CollectionBinding.new(definition, *args)      if definition.array?
  return HashBinding.new(definition, *args)            if definition.hash? and not definition.options[:use_attributes] # FIXME: hate this.
  return AttributeHashBinding.new(definition, *args)   if definition.hash? and definition.options[:use_attributes]
  return AttributeBinding.new(definition, *args)       if definition.attribute
  return ContentBinding.new(definition, *args)         if definition.content
  new(definition, *args)
end

Instance Method Details

#deserialize_from(nodes) ⇒ Object



49
50
51
52
# File 'lib/representable/bindings/xml_bindings.rb', line 49

def deserialize_from(nodes)
  content_for deserialize(nodes.first)
  #deserialize(nodes.first)
end

#deserialize_methodObject



59
60
61
# File 'lib/representable/bindings/xml_bindings.rb', line 59

def deserialize_method
  :from_node
end

#read(node) ⇒ Object



28
29
30
31
32
33
# File 'lib/representable/bindings/xml_bindings.rb', line 28

def read(node)
  nodes = find_nodes(node)
  return FragmentNotFound if nodes.size == 0 # TODO: write dedicated test!

  deserialize_from(nodes)
end

#serialize_for(value, parent) ⇒ Object

Creates wrapped node for the property.



36
37
38
39
40
# File 'lib/representable/bindings/xml_bindings.rb', line 36

def serialize_for(value, parent)
#def serialize_for(value, parent, tag_name=definition.from)
  node = node_for(parent, from)
  serialize_node(node, value)
end

#serialize_methodObject

DISCUSS: why is this public?



55
56
57
# File 'lib/representable/bindings/xml_bindings.rb', line 55

def serialize_method
  :to_node
end

#serialize_node(node, value) ⇒ Object



42
43
44
45
46
47
# File 'lib/representable/bindings/xml_bindings.rb', line 42

def serialize_node(node, value)
  return serialize(value) if typed?

  node.content = serialize(value)
  node
end

#write(parent, value) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/representable/bindings/xml_bindings.rb', line 18

def write(parent, value)
  wrap_node = parent

  if wrap = options[:wrap]
    parent << wrap_node = node_for(parent, wrap)
  end

  wrap_node << serialize_for(value, parent)
end