Module: Representable::XML::Namespace::DSL

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

Instance Method Summary collapse

Instance Method Details

#namespace(namespace) ⇒ Object



14
15
16
17
18
# 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) ⇒ Object



20
21
22
# File 'lib/representable/xml/namespace.rb', line 20

def namespace_def(mapping)
  namespace_defs.merge!(mapping.invert)
end

#namespace_defsObject

:private:



25
26
27
# File 'lib/representable/xml/namespace.rb', line 25

def namespace_defs
  representable_attrs.options[:namespace_mappings] ||= {}
end

#property(name, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
# 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