Class: AcDc::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/acdc/body.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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”})

Examples:

Filling the values and attributes


Parameters:

  • values (Hash) (defaults to: {})

    a customizable set of options

Options Hash (values):

  • :attributes (Hash)

    Hash list of attributes to populate



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 = options_for(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,options_for(key),key))
      else
        elements.update(key => Element(val,options_for(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

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/acdc/body.rb', line 4

def attributes
  @attributes
end

#elementsObject

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



94
95
96
97
# File 'lib/acdc/body.rb', line 94

def attribute(name, value = nil)
  @attributes ||= {}
  @attributes.update(name => value)
end

.declared_attributesObject

Returns a Hash list of Attributes declared



105
106
107
# File 'lib/acdc/body.rb', line 105

def declared_attributes 
  @attributes
end

.declared_elementsObject

Returns the Hash list of Elements declared



100
101
102
# File 'lib/acdc/body.rb', line 100

def declared_elements 
  @elements
end

.element(*options) ⇒ Object

Declare an Element for this Body

Parameters:

  • name (Symbol)

    A name to assign the Element (tag name)

  • type (Class)

    A type to use for the element (enforcement)

  • options (Hash)

    a customizable set of options

Options Hash (*options):

  • :single (Boolean)

    False if object is a collection



85
86
87
88
89
90
91
# File 'lib/acdc/body.rb', line 85

def element(*options)
  @elements ||= {}
  name = options.first
  type = options.second || nil
  opts = options.extract_options!
  @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('@elements',@elements ||= {})
  child.instance_variable_set('@attributes',@attributes ||= {})
end

Instance Method Details

#acdcObject

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_nameObject

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