class Representable::XML::Binding

Public Class Methods

build_for(definition) click to toggle source
# 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

Public Instance Methods

deserialize_from(nodes) click to toggle source
# File lib/representable/xml/binding.rb, line 56
def deserialize_from(nodes)
  content_for(nodes.first)
end
deserialize_method() click to toggle source
# File lib/representable/xml/binding.rb, line 65
def deserialize_method
  :from_node
end
read(node, as) click to toggle source
# 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) click to toggle source

Creates wrapped node for the property.

# 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_method() click to toggle source

DISCUSS: why is this public?

# File lib/representable/xml/binding.rb, line 61
def serialize_method
  :to_node
end
serialize_node(node, value, as) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

content_for(node) click to toggle source
# File lib/representable/xml/binding.rb, line 76
def content_for(node) # TODO: move this into a ScalarDecorator.
  return node if typed?

  node.content
end
find_nodes(doc, as) click to toggle source
# File lib/representable/xml/binding.rb, line 70
def find_nodes(doc, as)
  selector  = as
  selector  = "#{self[:wrap]}/#{as}" if self[:wrap]
  doc.xpath(selector) # nodes
end