Class: AcDc::Item

Inherits:
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
14
15
16
17
# File 'lib/acdc/item.rb', line 7

def initialize(name, type, options={})
  @name = name.to_s
  @type = type
  @renderable = options.delete(:render_empty)
  @renderable = true if @renderable.nil?
  @tag = options.delete(:tag) || 
         @type.tag_name rescue nil ||
         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:



39
40
41
# File 'lib/acdc/item.rb', line 39

def attribute?
  !element?
end

#constantObject



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

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

#element?Boolean

Returns:



35
36
37
# File 'lib/acdc/item.rb', line 35

def element?
  @xml_type == 'element'
end

#method_nameObject



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

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

#primitive?Boolean

Returns:



53
54
55
# File 'lib/acdc/item.rb', line 53

def primitive?
  Types.include?(constant)
end

#renderable?Boolean

Returns:



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

def renderable?
  @renderable
end

#single?Boolean

Returns:



31
32
33
# File 'lib/acdc/item.rb', line 31

def single?
  @single ||= options[:single].nil? || options[:single]
end

#value_from_xml(node, namespace) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/acdc/item.rb', line 43

def value_from_xml(node, namespace)
  n = find(node,namespace)
  if primitive?
    value = n.respond_to?(:content) ? n.content : n.to_s
    typecast(value)
  else
    constant.acdc(n)
  end
end

#xpath(namespace = self.namespace) ⇒ Object



57
58
59
60
61
62
# File 'lib/acdc/item.rb', line 57

def xpath(namespace = self.namespace)
  xpath  = ''
  xpath += "#{DEFAULT_NAMESPACE}:" if namespace
  xpath += tag
  xpath
end