Class: AcDc::Body
- Inherits:
-
Object
- Object
- AcDc::Body
- Defined in:
- lib/acdc/body.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#elements ⇒ Object
Returns the value of attribute elements.
Class Method Summary collapse
-
.acdc(xml) ⇒ Object
Converts the XML to a Class object found in the library.
-
.attribute(name, value = nil) ⇒ Object
Declare an attribute for this Body.
-
.declared_attributes ⇒ Object
Returns a Hash list of Attributes declared.
-
.declared_elements ⇒ Object
Returns the Hash list of Elements declared.
-
.element(*options) ⇒ Object
Declare an Element for this Body.
- .inherited(child) ⇒ Object
Instance Method Summary collapse
-
#acdc ⇒ Object
Converts object to XML.
-
#initialize(values = {}) ⇒ Body
constructor
Populates the attributes and elements declared for this object.
- #method_missing(method_id, *args, &block) ⇒ Object
-
#tag_name ⇒ Object
The name to use for the tag.
Constructor Details
#initialize(values = {}) ⇒ Body
Populates the attributes and elements declared for this object. class ThunderStruck < Body
attribute :thunder
element :lightning
end kaboom = ThunderStruck.new(=> {:thunder => “boom”, :lightning => “crash”})
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/acdc/body.rb', line 14 def initialize(values = {}) @attributes ||= {} @elements ||= {} # catch default values for attributes unless self.class.declared_attributes.values.all?{|val| val.nil?} self.class.declared_attributes.each do |key,val| attributes.update(key => Attribute(key,val)) unless val.nil? end end # now set initialized attribute values if values.has_key?(:attributes) values.delete(:attributes).each do |key,val| if self.class.declared_attributes.has_key?(key) attributes.update(key => Attribute(key,val)) end end end # and finally set values values.each do |key,val| if self.class.declared_elements.has_key?(key) type = (key)[:type] if type if val.respond_to?(:each) val.each {|v| raise ArgumentError.new("Type is invalid") unless v.is_a?(type)} else raise ArgumentError.new("Type is invalid") unless val.is_a?(type) end elements.update(key => type.new(val,(key),key)) else elements.update(key => Element(val,(key),key)) end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args, &block) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/acdc/body.rb', line 66 def method_missing(method_id, *args, &block) key = method_id.to_s.gsub(/\=/,"").to_sym if elements.has_key?(key) or attributes.has_key?(key) (method_id.to_s.match(/\=$/)) ? write(method_id, *args, &block) : read(method_id) else super end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/acdc/body.rb', line 4 def attributes @attributes end |
#elements ⇒ Object
Returns the value of attribute elements.
4 5 6 |
# File 'lib/acdc/body.rb', line 4 def elements @elements end |
Class Method Details
.acdc(xml) ⇒ Object
Converts the XML to a Class object found in the library
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/acdc/body.rb', line 110 def acdc(xml) doc = Hpricot.XML(xml) klass = doc.root.name.constantize attributes = doc.root.attributes.inject({}){|acc,attr| acc.update(attr[0].to_sym => attr[1])} values = doc.root.children.inject({}) do |acc, node| name = node.name.underscore.to_sym value = value_from_node(node) attrs = node.attributes acc.merge!({name => value, :attributes => attrs}) end klass.new(values.merge(:attributes => attributes)) end |
.attribute(name, value = nil) ⇒ Object
Declare an attribute for this Body
93 94 95 |
# File 'lib/acdc/body.rb', line 93 def attribute(name, value = nil) declared_attributes.update(name => value) end |
.declared_attributes ⇒ Object
Returns a Hash list of Attributes declared
104 105 106 107 |
# File 'lib/acdc/body.rb', line 104 def declared_attributes @attributes ||= {} @declared_attributes ||= @attributes[to_s] ||= {} end |
.declared_elements ⇒ Object
Returns the Hash list of Elements declared
98 99 100 101 |
# File 'lib/acdc/body.rb', line 98 def declared_elements @elements ||= {} @declared_elements ||= @elements[to_s] ||= {} end |
.element(*options) ⇒ Object
Declare an Element for this Body
85 86 87 88 89 90 |
# File 'lib/acdc/body.rb', line 85 def element(*) name = .first type = .second || nil opts = . declared_elements.update(name => opts.merge(:type => type)) end |
.inherited(child) ⇒ Object
76 77 78 79 |
# File 'lib/acdc/body.rb', line 76 def inherited(child) child.instance_variable_set('@declared_elements', @declared_elements) child.instance_variable_set('@declared_attributes', @declared_attributes) end |
Instance Method Details
#acdc ⇒ Object
Converts object to XML
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/acdc/body.rb', line 50 def acdc xml = Builder::XmlMarkup.new attrs = attributes.inject({}){|acc,attr| acc.update(attr[1].to_hash)} xml.tag!(tag_name,attrs){ |body| elements.each do |key, elem| body << elem.acdc end } xml.target! end |
#tag_name ⇒ Object
The name to use for the tag
62 63 64 |
# File 'lib/acdc/body.rb', line 62 def tag_name self.class.to_s.split("::").last end |