Class: AcDc::Item
- Inherits:
-
Object
- Object
- AcDc::Item
- Defined in:
- lib/acdc/item.rb
Constant Summary collapse
- Types =
[String, Float, Time, Date, DateTime, Integer, Boolean]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#options ⇒ Object
Returns the value of attribute options.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #attribute? ⇒ Boolean
- #constant ⇒ Object
- #element? ⇒ Boolean
-
#initialize(name, type, options = {}) ⇒ Item
constructor
A new instance of Item.
- #method_name ⇒ Object
- #primitive? ⇒ Boolean
- #value_from_xml(node, namespace) ⇒ Object
- #xpath(namespace = self.namespace) ⇒ Object
Constructor Details
#initialize(name, type, options = {}) ⇒ Item
Returns a new instance of Item.
7 8 9 10 11 12 13 |
# File 'lib/acdc/item.rb', line 7 def initialize(name, type, ={}) @name = name.to_s @type = type @tag = .delete(:tag) || name.to_s @options = @xml_type = self.class.to_s.split('::').last.downcase end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/acdc/item.rb', line 5 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/acdc/item.rb', line 5 def namespace @namespace end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/acdc/item.rb', line 5 def @options end |
#tag ⇒ Object
Returns the value of attribute tag.
5 6 7 |
# File 'lib/acdc/item.rb', line 5 def tag @tag end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/acdc/item.rb', line 5 def type @type end |
Instance Method Details
#constant ⇒ Object
19 20 21 |
# File 'lib/acdc/item.rb', line 19 def constant @constant ||= constantize(type) end |
#element? ⇒ Boolean
23 24 25 |
# File 'lib/acdc/item.rb', line 23 def element? @xml_type == 'element' end |
#method_name ⇒ Object
15 16 17 |
# File 'lib/acdc/item.rb', line 15 def method_name @method_name ||= name.tr('-','_') end |
#primitive? ⇒ Boolean
42 43 44 |
# File 'lib/acdc/item.rb', line 42 def primitive? Types.include?(constant) end |
#value_from_xml(node, namespace) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/acdc/item.rb', line 31 def value_from_xml(node, namespace) if primitive? find(node, namespace) do |n| value = n.respond_to?(:content) ? n.content : n.to_s typecast(value) end else constant.parse(node, ) end end |
#xpath(namespace = self.namespace) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/acdc/item.rb', line 46 def xpath(namespace = self.namespace) xpath = '' xpath += "#{DEFAULT_NAMESPACE}:" if namespace xpath += tag xpath end |