Module: WellsFargo::Schema

Defined in:
lib/wells_fargo/schema.rb

Constant Summary collapse

Schema =
File.expand_path File.join(File.dirname(__FILE__), '..', 'schema.xml')
ElementDir =
File.expand_path File.join(File.dirname(__FILE__), 'elements')
NameMapFile =
File.expand_path File.join(ElementDir, 'name_map.rb')

Class Method Summary collapse

Class Method Details

.generate_element_filesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
# File 'lib/wells_fargo/schema.rb', line 15

def generate_element_files
  @name_map = {}
  schema do |definition|
    class_name = definition.attributes['name'].value
    file = File.join(ElementDir, "#{WellsFargo.underscore class_name}.rb")
    puts "Writing #{class_name} => #{File.basename file}"
    File.open(file, 'w') do |f|
      f.write <<-EOF  
module WellsFargo
  class Element
    class #{class_name} < WellsFargo::Element
#{(definition / 'xsd|attribute').map do |attr|

  name = attr.attributes['name'].value.gsub(/\s+/, '')
  @name_map[WellsFargo.underscore name] = name
  name = WellsFargo.underscore name

  use = attr.attributes['use']

  if use && 'required' == use.value
    "      required_attribute :#{name}"
  else
    "      attribute :#{name}"
  end
end.join("\n")}

#{(definition / 'xsd|element').map do |element|

  name = (element.attributes['ref'] || element.attributes['name']).value.gsub(/\s+/, '')
  @name_map[WellsFargo.underscore name] = name
  name = WellsFargo.underscore name
  options = {}
  limit = ((max = element.attributes['maxOccurs']) && max && max.value).to_i
  options[:limit] = limit if limit > 0
  options[:simple] = true if element.attributes['ref'].nil?

  if {} == options
    "      child :#{name}"
  else
    "      child :#{name}, :limit => #{limit}"
  end
end.join("\n")}
    end
  end
end
EOF
    end
    save_name_map
  end
end

.NameMapObject



9
10
11
12
13
# File 'lib/wells_fargo/schema.rb', line 9

def NameMap
  Marshal.load File.read(NameMapFile)
rescue Errno::ENOENT
  {}
end

.schemaObject



66
67
68
69
70
# File 'lib/wells_fargo/schema.rb', line 66

def schema
  (load_schema / 'xsd|schema > xsd|element').each do |element|
    yield element
  end
end