Module: Representable::XML::Namespace

Defined in:
lib/representable/xml/namespace.rb

Overview

Experimental! Best explanation so far: books.xmlschemata.org/relaxng/relax-CHP-11-SECT-1.html

Note: This module doesn’t work with JRuby because Nokogiri uses a completely different implementation in Java which has other requirements that we couldn’t fulfil. Please wait for Representable 4 where we replace Nokogiri with Oga.

Defined Under Namespace

Modules: AsWithNamespace, DSL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



9
10
11
# File 'lib/representable/xml/namespace.rb', line 9

def self.included(includer)
  includer.extend(DSL)
end

.Namespaced(prefix, name) ⇒ Object



111
112
113
# File 'lib/representable/xml/namespace.rb', line 111

def self.Namespaced(prefix, name)
  [ prefix, name ].compact.join(":")
end

Instance Method Details

#add_namespace_definitions!(node, namespaces) ⇒ Object

“Physically” add ‘xmlns` attributes to `node`.



104
105
106
107
108
109
# File 'lib/representable/xml/namespace.rb', line 104

def add_namespace_definitions!(node, namespaces)
  namespaces.each do |uri, prefix|
    prefix = prefix.nil? ? nil : prefix.to_s
    node.add_namespace_definition(prefix, uri)
  end
end

#from_node(node, options = {}) ⇒ Object

FIXME: some “bug” in Representable’s XML doesn’t consider the container tag, so we could theoretically pick the wrong namespaced tag here :O



81
82
83
# File 'lib/representable/xml/namespace.rb', line 81

def from_node(node, options={})
  super
end

#representable_map(options, format) ⇒ Object

FIXME: this is a PoC, we need a better API to inject code.



116
117
118
119
120
# File 'lib/representable/xml/namespace.rb', line 116

def representable_map(options, format)
  super.tap do |map|
    map.each { |bin| bin.extend(AsWithNamespace) unless bin.is_a?(Binding::Attribute) }
  end
end

#to_node(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/representable/xml/namespace.rb', line 85

def to_node(options={})
  local_uri = representable_attrs.options[:local_namespace] # every decorator MUST have a local namespace.
  prefix    = self.class.namespace_defs[local_uri]

  root_tag = [prefix, representation_wrap(options)].compact.join(":")

  options = { wrap: root_tag }.merge(options)

  # TODO: there should be an easier way to pass a set of options to all nested #to_node decorators.
  representable_attrs.keys.each do |property|
    options[property.to_sym] = { show_definition: false, namespaces: options[:namespaces] }
  end

  super(options).tap do |node|
    add_namespace_definitions!(node, self.class.namespace_defs) unless options[:show_definition] == false
  end
end