Class: Enterprisifier::CodeGeneration::CodeGenerator

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

Constant Summary collapse

FacetElements =
%w[enumeration fractionDigits length maxExclusive maxInclusive maxLength
minExclusive minInclusive minLength pattern totalDigits whiteSpace]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, node, mod_name = nil, entity_name = nil) ⇒ CodeGenerator

Returns a new instance of CodeGenerator.



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

def initialize(generator, node, mod_name = nil, entity_name = nil)
  @generator = generator
  @base_mod, @mod_name, @entity_name, @node = generator.base_module, mod_name, entity_name, node
  @dependencies = []
  @built = false
  @code = CodeBuilder.new
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



11
12
13
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 11

def dependencies
  @dependencies
end

#entity_nameObject (readonly)

<< self



49
50
51
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 49

def entity_name
  @entity_name
end

Class Method Details

.build(generator, node, mod_name, entity_name = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 40

def build(generator, node, mod_name, entity_name = nil)
  if code_generator = CodeGenerator.registered_types[node.registered_as.to_sym]
    code_generator.new(generator, node, mod_name, entity_name)
  else
    raise NotImplementedError, "No generator for '#{node.registered_as}' found."
  end
end

.extends(extension_type) ⇒ Object



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

def extends(extension_type)
  self.extension_type = extension_type
end

.macro_mappings(*mappings) ⇒ Object



36
37
38
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 36

def macro_mappings(*mappings)
  write_inheritable_array :macro_mappings, mappings
end

.register(xsd_type, opts = {}) ⇒ Object



30
31
32
33
34
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 30

def register(xsd_type, opts = {})
  self.registered_as = xsd_type.to_sym
  write_inheritable_hash(:registration_options, opts)
  CodeGenerator.registered_types[xsd_type.to_sym] = self
end

Instance Method Details

#entity_or_attr_nameObject



63
64
65
66
67
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 63

def entity_or_attr_name
  entity_name || begin
    name ? name[:value] : nil
  end
end

#extension_requireObject



84
85
86
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 84

def extension_require
  extension_type.split("::").map(&:underscore).join("/")
end

#macro_reference_requireObject



88
89
90
91
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 88

def macro_reference_require
  return nil unless macro_reference_type
  macro_reference_type.split("::").map(&:underscore).join("/")
end

#nameObject



59
60
61
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 59

def name
  @node.name
end

#requiresObject



80
81
82
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 80

def requires
  @requires ||= ([extension_require, macro_reference_require] + child_generators.map(&:requires)).flatten.compact.uniq.sort
end

#to_rubyObject



73
74
75
76
77
78
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 73

def to_ruby
  return @code.to_s if @built
  entity_name ? build_qualified_ruby : build_unqualified_ruby
  @built = true
  @code.to_s
end

#to_sObject



69
70
71
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 69

def to_s
  to_ruby.to_s
end

#write_fileObject



93
94
95
96
# File 'lib/enterprisifier/code_generation/code_generator.rb', line 93

def write_file
  return nil unless filesystem_path
  @generator.write_file(filesystem_path, to_ruby)
end