Class: XMLable::Builder
- Inherits:
-
Object
- Object
- XMLable::Builder
- Includes:
- Singleton
- Defined in:
- lib/xmlable/builder.rb
Overview
Builder class builds object that represents XML attribute and values
Instance Attribute Summary collapse
- #defined_types ⇒ Hash readonly
Class Method Summary collapse
-
.build_attribute(attribute, handler) ⇒ XMLable::Mixins::Object
Build attribute object from XML attribute.
-
.build_document(document, handler) ⇒ XMLable::Mixins::Object
Build document object from XML document.
-
.build_element(node, handler) ⇒ XMLable::Mixins::Object
Build element object from XML element.
-
.container_proxy_for(klass) ⇒ Class
Get container proxy for the given class.
-
.define_type(*names, &block) ⇒ Class
Define new type.
-
.find_type(type) ⇒ Class
Find defined type.
-
.inherit_type(klass, &block) ⇒ Class
Inherit type and set additional settings.
-
.populate_attribute(obj, node) ⇒ XMLable::Mixins::Object
Populate attribute object.
-
.populate_document(obj, node) ⇒ XMLable::Mixins::Object
Populate document object.
-
.populate_element(obj, node) ⇒ XMLable::Mixins::Object
Populate element object.
-
.proxy_for(type) ⇒ Class
Get proxy for the given type.
-
.wrap_type(klass, &block) ⇒ Class
Wrap type with additional logic.
-
.wrapped_type?(klass) ⇒ Boolean
Is the given class wrapped?.
Instance Method Summary collapse
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize ⇒ Builder
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_types ⇒ Hash (readonly)
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
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
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
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
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
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
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
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
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
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
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
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
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?
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 |