Class: Relief::Element
- Inherits:
-
Object
- Object
- Relief::Element
- Defined in:
- lib/relief/element.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #attribute(name, options = {}, &block) ⇒ Object
- #element(name, options = {}, &block) ⇒ Object
- #elements(name, options = {}, &block) ⇒ Object
-
#initialize(name = nil, options = {}, &block) ⇒ Element
constructor
A new instance of Element.
- #parse(document) ⇒ Object
- #xpath ⇒ Object
Constructor Details
#initialize(name = nil, options = {}, &block) ⇒ Element
Returns a new instance of Element.
7 8 9 10 11 12 13 |
# File 'lib/relief/element.rb', line 7 def initialize(name=nil, ={}, &block) @name = name @options = @children = [] instance_eval(&block) if block_given? end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/relief/element.rb', line 5 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/relief/element.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/relief/element.rb', line 5 def @options end |
Instance Method Details
#attribute(name, options = {}, &block) ⇒ Object
62 63 64 |
# File 'lib/relief/element.rb', line 62 def attribute(name, ={}, &block) element(name, .merge(:attribute => true), &block) end |
#element(name, options = {}, &block) ⇒ Object
53 54 55 56 |
# File 'lib/relief/element.rb', line 53 def element(name, ={}, &block) [:xpath] ||= name if name =~ %r([/.]) @children << self.class.new(name, , &block) end |
#elements(name, options = {}, &block) ⇒ Object
58 59 60 |
# File 'lib/relief/element.rb', line 58 def elements(name, ={}, &block) element(name, .merge(:collection => true), &block) end |
#parse(document) ⇒ Object
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 48 49 50 51 |
# File 'lib/relief/element.rb', line 15 def parse(document) @children.inject({}) do |values, element| key = element.[:as] || element.name type = element.[:type] values[key] = begin target = (document / element.xpath) parse_node = lambda { |target| if element.children.any? element.parse(target) else value = target.to_s if type.nil? then value elsif value.empty? then nil elsif type == Integer then value.to_i elsif type == Float then value.to_f elsif type == Date then Date.parse(value) elsif type == Time then Time.parse(value) elsif type == DateTime then DateTime.parse(value) elsif type.is_a?(Parser) then type.parse(document) else value end end } if element.[:collection] target.collect { |child| parse_node.call(child) } else parse_node.call(target) end end values end end |
#xpath ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/relief/element.rb', line 66 def xpath if .has_key?(:xpath) [:xpath] elsif @children.any? name.to_s else attribute = @options[:attribute] attribute = name if attribute == true !attribute ? "#{name}/text()" : ".//@#{attribute}" end end |