module Representable::XML::Namespace::DSL

Public Instance Methods

namespace(namespace) click to toggle source
# File lib/representable/xml/namespace.rb, line 14
def namespace(namespace)
  representable_attrs.options[:local_namespace] = namespace
  representable_attrs.options[:namespace_mappings] ||= {}
  representable_attrs.options[:namespace_mappings][namespace] = nil # this might get overwritten via #namespace_def later.
end
namespace_def(mapping) click to toggle source
# File lib/representable/xml/namespace.rb, line 20
def namespace_def(mapping)
  namespace_defs.merge!(mapping.invert)
end
namespace_defs() click to toggle source

:private:

# File lib/representable/xml/namespace.rb, line 25
def namespace_defs
  representable_attrs.options[:namespace_mappings] ||= {}
end
property(name, options={}) click to toggle source
Calls superclass method
# File lib/representable/xml/namespace.rb, line 29
def property(name, options={})
  uri = representable_attrs.options[:local_namespace] # per default, a property belongs to the local namespace.
  options[:namespace] ||= uri # don't override if already set.

  # a nested representer is automatically assigned "its" local namespace. It's like saying
  #   property :author, namespace: "http://ns/author" do ... end

  super.tap do |dfn|
    if dfn.typed? # FIXME: ouch, this should be doable with property's API to hook into the creation process.
      dfn.merge!( namespace: dfn.representer_module.representable_attrs.options[:local_namespace] )

      update_namespace_defs!(namespace_defs)
    end
  end
end
update_namespace_defs!(namespace_defs) click to toggle source

:private: super ugly hack recursively injects the #namespace_defs into all representers of this tree. will be done better in 4.0.

# File lib/representable/xml/namespace.rb, line 48
def update_namespace_defs!(namespace_defs)
  representable_attrs.each do |dfn|
    dfn.merge!(namespace_defs: namespace_defs) # this only helps with scalars

    if dfn.typed?
      representer = Class.new(dfn.representer_module) # don't pollute classes.
      representer.update_namespace_defs!(namespace_defs)
      dfn.merge!(extend: representer)
    end
  end
end