Class: Tiss::Generator::ValidatorGenerator

Inherits:
BaseGenerator show all
Defined in:
lib/tiss/generator/generators/validator_generator.rb

Instance Attribute Summary

Attributes inherited from BaseGenerator

#template_dir

Instance Method Summary collapse

Methods inherited from BaseGenerator

call, #initialize, #is_complex_root, #is_element, #is_simple, #is_text, #is_xml_schema_node, #namespace_of, #select_children

Constructor Details

This class inherits a constructor from Tiss::Generator::BaseGenerator

Instance Method Details

#call(version, name, restrictions, target) ⇒ Object



7
8
9
10
11
12
13
14
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
# File 'lib/tiss/generator/generators/validator_generator.rb', line 7

def call(version, name, restrictions, target)
  name = classify "#{underscore(name)}_validator"
  ap name
  base = restrictions
         .attributes['base'].value.split(':').last

  base = case base
         when 'decimal'
           'float'
         when 'boolean'
           'true_class'
         else
           base
         end

  type = constantize classify base

  ws_action = select_children(restrictions, 'whiteSpace').first
  ws_action = ws_action.attributes['value'].value unless ws_action.nil?

  enum_values = select_children(restrictions, 'enumeration').collect do |enum|
    enum.attributes['value'].value
  end

  pattern = select_children(restrictions, 'pattern').first
  pattern = pattern.attributes['value'].value unless pattern.nil?

  template = File.read(File.join(template_dir, 'validator_template.erb'))
  file_content = ERB.new(template).result(binding)
  File.open("lib/tiss/#{version}/validators/#{underscore(name)}.rb", 'w+') do |f|
    f.write(file_content)
  end
  File.open("lib/tiss/#{version}/#{version}.rb", 'a') do |f|
    f.puts "require_relative 'validators/#{underscore(name)}'"
  end
end