Class: Representable::XML::Binding

Inherits:
Binding
  • Object
show all
Defined in:
lib/representable/xml/binding.rb

Direct Known Subclasses

Attribute, Collection, Content

Defined Under Namespace

Classes: Attribute, AttributeHash, Collection, Content, Hash

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_for(definition) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/representable/xml/binding.rb', line 6

def self.build_for(definition)
  return Collection.new(definition)      if definition.array?
  return Hash.new(definition)            if definition.hash? and not definition[:use_attributes] # FIXME: hate this.
  return AttributeHash.new(definition)   if definition.hash? and definition[:use_attributes]
  return Attribute.new(definition)       if definition[:attribute]
  return Content.new(definition)         if definition[:content]

  new(definition)
end

Instance Method Details

#deserialize_from(nodes) ⇒ Object



49
50
51
# File 'lib/representable/xml/binding.rb', line 49

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

#deserialize_method ⇒ Object



58
59
60
# File 'lib/representable/xml/binding.rb', line 58

def deserialize_method
  :from_node
end

#read(node, as) ⇒ Object



26
27
28
29
30
31
# File 'lib/representable/xml/binding.rb', line 26

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

  deserialize_from(nodes)
end

#serialize_for(value, parent, as) ⇒ Object

Creates wrapped node for the property.



34
35
36
37
# File 'lib/representable/xml/binding.rb', line 34

def serialize_for(value, parent, as)
  node = XML::Node(parent.document, as) # node doesn't have attr="" attributes!!!
  serialize_node(node, value, as)
end

#serialize_method ⇒ Object

DISCUSS: why is this public?



54
55
56
# File 'lib/representable/xml/binding.rb', line 54

def serialize_method
  :to_node
end

#serialize_node(node, value, as) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/representable/xml/binding.rb', line 39

def serialize_node(node, value, as)
  if typed?
    value.name = as if as != self[:name]
    return value
  end

  node.content = value
  node
end

#write(parent, fragments, as) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/representable/xml/binding.rb', line 16

def write(parent, fragments, as)
  wrap_node = parent

  if wrap = self[:wrap]
    parent << wrap_node = XML::Node(parent.document, wrap)
  end

  wrap_node << serialize_for(fragments, parent, as)
end