Class: AcDc::Item

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

Direct Known Subclasses

Attribute, Element

Constant Summary collapse

Types =
[String, Float, Time, Date, DateTime, Integer, Boolean]

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @name = name.to_s
  @type = type
  @tag = options.delete(:tag) || name.to_s
  @options = options
  @xml_type = self.class.to_s.split('::').last.downcase
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/acdc/item.rb', line 5

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/acdc/item.rb', line 5

def namespace
  @namespace
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/acdc/item.rb', line 5

def options
  @options
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/acdc/item.rb', line 5

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/acdc/item.rb', line 5

def type
  @type
end

Instance Method Details

#attribute?Boolean

Returns:



27
28
29
# File 'lib/acdc/item.rb', line 27

def attribute?
  !element?
end

#constantObject



19
20
21
# File 'lib/acdc/item.rb', line 19

def constant
  @constant ||= constantize(type)
end

#element?Boolean

Returns:



23
24
25
# File 'lib/acdc/item.rb', line 23

def element?
  @xml_type == 'element'
end

#method_nameObject



15
16
17
# File 'lib/acdc/item.rb', line 15

def method_name
  @method_name ||= name.tr('-','_')
end

#primitive?Boolean

Returns:



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, options)
  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