Class: XMLable::Builder

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/xmlable/builder.rb

Overview

Builder class builds object that represents XML attribute and values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



185
186
187
# File 'lib/xmlable/builder.rb', line 185

def initialize
  @defined_types = {}
end

Instance Attribute Details

#defined_typesHash (readonly)

Returns:

  • (Hash)


183
184
185
# File 'lib/xmlable/builder.rb', line 183

def defined_types
  @defined_types
end

Class Method Details

.build_attribute(attribute, handler) ⇒ XMLable::Mixins::Object

Build attribute object from XML attribute

Parameters:

Returns:



30
31
32
33
# File 'lib/xmlable/builder.rb', line 30

def build_attribute(attribute, handler)
  obj = handler.proxy.new({}, attribute, handler)
  populate_attribute(obj, attribute)
end

.build_document(document, handler) ⇒ XMLable::Mixins::Object

Build document object from XML document

Parameters:

Returns:



17
18
19
20
# File 'lib/xmlable/builder.rb', line 17

def build_document(document, handler)
  obj = handler.proxy.new({}, document, handler)
  populate_document(obj, document)
end

.build_element(node, handler) ⇒ XMLable::Mixins::Object

Build element object from XML element

Parameters:

Returns:



43
44
45
46
# File 'lib/xmlable/builder.rb', line 43

def build_element(node, handler)
  obj = handler.proxy.new({}, node, handler)
  populate_element(obj, node)
end

.container_proxy_for(klass) ⇒ Class

Get container proxy for the given class

Parameters:

  • klass (Class)

Returns:

  • (Class)


177
178
179
# File 'lib/xmlable/builder.rb', line 177

def container_proxy_for(klass)
  Class.new(klass) { include Mixins::Container }
end

.define_type(*names, &block) ⇒ Class

Define new type

Parameters:

  • names (Array<Object>)

    type’s names(aliases)

Returns:

  • (Class)


139
140
141
142
143
144
145
# File 'lib/xmlable/builder.rb', line 139

def define_type(*names, &block)
  names = names.map { |n| n.is_a?(Symbol) ? n.to_s : n }
  main = names.first
  klass = wrapped_type?(main) ? wrap_type(main, &block) : inherit_type(main, &block)
  names.each { |n| instance.defined_types[n] = klass }
  klass
end

.find_type(type) ⇒ Class

Find defined type

Parameters:

  • type (Object)

Returns:

  • (Class)

    returns found type or define new one if it’s not found



96
97
98
99
100
# File 'lib/xmlable/builder.rb', line 96

def find_type(type)
  type = type.to_s if type.is_a?(Symbol)
  klass = instance.defined_types[type]
  klass ? klass : define_type(type)
end

.inherit_type(klass, &block) ⇒ Class

Inherit type and set additional settings

Parameters:

  • klass (Class)

Returns:

  • (Class)

    returns wrapped class



126
127
128
129
130
# File 'lib/xmlable/builder.rb', line 126

def inherit_type(klass, &block)
  Class.new(klass) do
    class_eval(&block) if block_given?
  end
end

.populate_attribute(obj, node) ⇒ XMLable::Mixins::Object

Populate attribute object

Parameters:

Returns:



85
86
87
# File 'lib/xmlable/builder.rb', line 85

def populate_attribute(obj, node)
  obj.tap { |o| o.__set_value(node) }
end

.populate_document(obj, node) ⇒ XMLable::Mixins::Object

Populate document object

Parameters:

Returns:



56
57
58
59
# File 'lib/xmlable/builder.rb', line 56

def populate_document(obj, node)
  obj.__set_root(node.root)
  obj
end

.populate_element(obj, node) ⇒ XMLable::Mixins::Object

Populate element object

Parameters:

Returns:



69
70
71
72
73
74
75
# File 'lib/xmlable/builder.rb', line 69

def populate_element(obj, node)
  node.namespace_definitions.each { |ns| obj.__set_namespace_definition(ns) }
  node.elements.each { |el| obj.__set_element(el) }
  node.attributes.each { |_, att| obj.__set_attribute(att) }
  obj.__set_content(node)
  obj
end

.proxy_for(type) ⇒ Class

Get proxy for the given type

Parameters:

  • type (Object)

    type’s name

Returns:

  • (Class)

    returns type’s proxy class



166
167
168
# File 'lib/xmlable/builder.rb', line 166

def proxy_for(type)
  find_type(type).dup
end

.wrap_type(klass, &block) ⇒ Class

Wrap type with additional logic

Parameters:

  • klass (Class)

Returns:

  • (Class)

    returns wrapped class



109
110
111
112
113
114
115
116
117
# File 'lib/xmlable/builder.rb', line 109

def wrap_type(klass, &block)
  Class.new do
    include Mixins::Object
    include Mixins::Wrapper
    include Mixins::Castable
    include Mixins::OptionsStorage
    class_eval(&block) if block_given?
  end
end

.wrapped_type?(klass) ⇒ Boolean

Is the given class wrapped?

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'lib/xmlable/builder.rb', line 152

def wrapped_type?(klass)
  return true  if !klass.is_a?(Class)
  return false if klass.ancestors.include?(Mixins::StandaloneElement)
  return false if klass.ancestors.include?(Mixins::StandaloneAttribute)
  true
end