Module: WCC::Arena::Mappers::XML

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/wcc/arena/mappers/xml.rb', line 8

def document
  @document
end

Class Method Details

.included(receiver) ⇒ Object



125
126
127
# File 'lib/wcc/arena/mappers/xml.rb', line 125

def self.included(receiver)
  receiver.extend ClassMethods
end

Instance Method Details

#[](attribute) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/wcc/arena/mappers/xml.rb', line 14

def [](attribute)
  if self.class.attributes[attribute]
    load_attribute(attribute)
  else
    raise KeyError, "Attribute #{attribute} is not defined"
  end
end

#attributesObject



22
23
24
25
26
# File 'lib/wcc/arena/mappers/xml.rb', line 22

def attributes
  self.class.attributes.each_with_object({}) do |(name, options), hash|
    hash[name] = self[name]
  end
end

#cast_attribute(text, type) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wcc/arena/mappers/xml.rb', line 54

def cast_attribute(text, type)
  return nil if text.empty?

  case type
  when :integer
    Integer(text)
  when :date
    Date.strptime(text.split("T").first, "%Y-%m-%d")
  when :time
    Time.parse(text)
  when :boolean
    text.downcase == "true"
  else
    text
  end
end

#initialize(document) ⇒ Object



10
11
12
# File 'lib/wcc/arena/mappers/xml.rb', line 10

def initialize(document)
  @document = document
end

#inspectObject



28
29
30
31
# File 'lib/wcc/arena/mappers/xml.rb', line 28

def inspect
  "<#{self.class.name} " \
  "#{attributes.collect { |(name, value)| [name, value.inspect].join("=") }.join(" ")}>"
end

#load_attribute(attribute) ⇒ Object



48
49
50
51
52
# File 'lib/wcc/arena/mappers/xml.rb', line 48

def load_attribute(attribute)
  config = self.class.attributes[attribute]
  node = document.xpath(config[:xpath])
  cast_attribute(node.text, config[:type])
end