Class: Tiss::Generator::ModelGenerator

Inherits:
BaseGenerator show all
Extended by:
ActiveSupport::Inflector
Defined in:
lib/tiss/generator/generators/model_generator.rb

Constant Summary collapse

MODELS =
{}

Instance Attribute Summary

Attributes inherited from BaseGenerator

#template_dir

Class Method Summary collapse

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

Class Method Details

.append(version, name, attributes, extension) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/tiss/generator/generators/model_generator.rb', line 11

def self.append(version, name, attributes, extension)
  model = ModelGenerator::MODELS[name.to_sym] || {}
  model[:attributes] = [] unless model[:attributes].present?
  model[:attributes] = model[:attributes] + attributes.compact.map { |item| item.merge(version: version) }
  if extension.present?
    model[:extension] = { class: extension, file: underscore(extension) }
  end

  ModelGenerator::MODELS[name.to_sym] = model
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tiss/generator/generators/model_generator.rb', line 22

def call
  template = File.read(File.join(template_dir, 'model_template.erb'))
  models = ModelGenerator::MODELS
  models.each_key do |model_name|
    model_config = models[model_name]
    attributes = model_config[:attributes].compact.uniq.group_by { |item| item[:name] }

    file_content = ERB.new(template, nil, '-').result(binding)
    File.open("lib/tiss/models/#{underscore(model_name.to_s)}.rb", 'w+') do |f|
      f.write(file_content)
    end
  end
end