Module: XMLMapping

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



27
28
29
30
31
32
# File 'lib/xmlmapping.rb', line 27

def self.included(mod)
  mod.extend(ClassMethods)

  mod.instance_variable_set("@raw_mappings", {})
  mod.instance_variable_set("@mappings", { :element => {}, :attribute => {}, :text => {}, :namespace => nil})
end

Instance Method Details

#initialize(input) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xmlmapping.rb', line 35

def initialize(input)
  root = parse(input)

  mappings = self.class.mappings
  raw_mappings = self.class.raw_mappings

  # initialize :many attributes
  raw_mappings.values.select { |mapping| mapping[:cardinality] == :many }.each { |m|
    instance_variable_set("@#{m[:attribute]}", [])
  }

  root.each_element { |e| 
    process(e, mappings[:element])
  }

  root.attributes.each_attribute { |a|
    process(a, mappings[:attribute])
  }

  mappings[:text].values.each { |mapping|
    name = mapping[:attribute]
    value = extract_value(root, mapping)
    instance_variable_set("@#{name}", value)
  }

end