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

Instance Attribute Summary

Attributes inherited from Binding

#cached_representer, #getter, #name, #setter

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Binding

#[], build, #default_for, #initialize, #skipable_empty_value?

Methods included from Binding::Factories

#collect_for, #default_parse_fragment_functions, #default_parse_init_functions, #default_post_functions, #default_render_fragment_functions, #default_render_init_functions, #parse_functions, #pipeline_for, #render_functions

Methods included from Binding::EvaluateOption

#evaluate_option

Methods included from Binding::Deprecatable

#compile_fragment, #uncompile_fragment

Constructor Details

This class inherits a constructor from Representable::Binding

Class Method Details

.build_for(definition) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/representable/xml/binding.rb', line 13

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



56
57
58
# File 'lib/representable/xml/binding.rb', line 56

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

#deserialize_methodObject



65
66
67
# File 'lib/representable/xml/binding.rb', line 65

def deserialize_method
  :from_node
end

#read(node, as) ⇒ Object



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

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.



41
42
43
44
# File 'lib/representable/xml/binding.rb', line 41

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_methodObject

DISCUSS: why is this public?



61
62
63
# File 'lib/representable/xml/binding.rb', line 61

def serialize_method
  :to_node
end

#serialize_node(node, value, as) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/representable/xml/binding.rb', line 46

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



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

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