Class: Enterprisifier::CodeGeneration::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/enterprisifier/code_generation/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) {|_self| ... } ⇒ Generator

Returns a new instance of Generator.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
# File 'lib/enterprisifier/code_generation/generator.rb', line 9

def initialize(schema)
  @schema = schema
  @modules_for_namespaces = {}
  @generated = []
  yield(self)
  self.output_directory = Pathname(output_directory.to_s).expand_path if output_directory
end

Instance Attribute Details

#base_moduleObject

Returns the value of attribute base_module.



7
8
9
# File 'lib/enterprisifier/code_generation/generator.rb', line 7

def base_module
  @base_module
end

#output_directoryObject

Returns the value of attribute output_directory.



7
8
9
# File 'lib/enterprisifier/code_generation/generator.rb', line 7

def output_directory
  @output_directory
end

Instance Method Details

#add_module_for_namespace(namespace, mod_name) ⇒ Object



17
18
19
# File 'lib/enterprisifier/code_generation/generator.rb', line 17

def add_module_for_namespace(namespace, mod_name)
  @modules_for_namespaces[namespace] = mod_name
end

#buildObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/enterprisifier/code_generation/generator.rb', line 30

def build
  @generated.clear
  @schema.registries.each do |namespace,registry|
    mod_name = module_for_namespace(namespace)
    raise "No module for namespace '#{namespace}' is known. Try setting one with add_module_for_namespace." unless mod_name
    registry.each do |xsd_type,nodes|
      nodes.each do |name,node|
        @generated << code_generator.build(self, node, mod_name, name)
      end
    end
  end
end

#code_generatorObject



83
84
85
# File 'lib/enterprisifier/code_generation/generator.rb', line 83

def code_generator
  Enterprisifier::CodeGeneration::CodeGenerator
end

#modularize_type(type_name, mod_name, type_ns = "Types") ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/enterprisifier/code_generation/generator.rb', line 87

def modularize_type(type_name, mod_name, type_ns = "Types")
  camelized = type_name[:value].camelize
  if xsd_namespace?(type_name[:namespace])
    "Enterprisifier::Marshalling::XSD::#{type_ns}::#{camelized}"
  elsif !type_name[:namespace] || mod_name == resolve_type(type_name)
    "#{@base_module}::#{mod_name}::#{type_ns}::#{camelized}"
  elsif type_name[:namespace] && resolved = resolve_type(type_name)
    "#{@base_module}::#{resolved}::#{type_ns}::#{camelized}"
  else
    raise "Wuh-oh."
  end
end

#module_for_namespace(namespace) ⇒ Object



21
22
23
# File 'lib/enterprisifier/code_generation/generator.rb', line 21

def module_for_namespace(namespace)
  @modules_for_namespaces[namespace]
end

#resolve_type(type_name) ⇒ Object

TODO:
  • this fucking sucks



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/enterprisifier/code_generation/generator.rb', line 62

def resolve_type(type_name)
  found_ns = @schema.namespaces.detect do |ns|
    ns[0] == type_name[:namespace]
  end

  ns_aliases = @schema.namespaces.select do |ns|
    ns != found_ns && ns.last == found_ns.last
  end if found_ns

  mod_for_ns = @modules_for_namespaces.detect do |(url, mod_name)|
    url == found_ns.last
  end unless ns_aliases.empty?

  raise "Couldn't resolve #{type_name.inspect}" unless mod_for_ns
  mod_for_ns.last
end

#to_rubyObject



25
26
27
28
# File 'lib/enterprisifier/code_generation/generator.rb', line 25

def to_ruby
  build
  @generated.to_s
end

#write_file(fs_path, code) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/enterprisifier/code_generation/generator.rb', line 50

def write_file(fs_path, code)
  raise "An output directory must be provided" unless output_directory
  output_file = Pathname((output_directory + fs_path).expand_path.to_s + ".rb")
  output_file.parent.mkpath
  raise "Output file already exists at #{output_file}" if output_file.exist?
  output_file.open('w') do |f|
    f << code
  end
end

#write_filesObject



43
44
45
46
47
48
# File 'lib/enterprisifier/code_generation/generator.rb', line 43

def write_files
  raise "An output directory must be provided" unless output_directory
  build
  output_directory.mkpath
  @generated.each { |g| g.write_file }
end

#xsd_namespace?(namespace_ref) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/enterprisifier/code_generation/generator.rb', line 79

def xsd_namespace?(namespace_ref)
  @schema.xsd_namespace?(namespace_ref)
end