Class: Mongify::Mongoid::Generator
- Inherits:
-
Object
- Object
- Mongify::Mongoid::Generator
- Defined in:
- lib/mongify/mongoid/generator.rb
Overview
Generator - Processes translation file and generates output based on models
Instance Attribute Summary collapse
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
-
#find_model(name) ⇒ Model?
Returns model based on table name.
-
#generate_embedded_relations ⇒ Array
Goes through embedded relationships.
-
#generate_models ⇒ Array
Generate models based on traslation tables.
-
#initialize(translation_file, output_dir) ⇒ Generator
constructor
A new instance of Generator.
-
#process ⇒ nil
Process translation file and generate output files.
-
#process_fields ⇒ Array
Goes through all the models and adds fields based on columns in the given table.
-
#translation ⇒ Mongify::Translation
Returns Mongify translation class for given translation file.
-
#write_models_to_file ⇒ Printer
Writes models to files.
Constructor Details
#initialize(translation_file, output_dir) ⇒ Generator
Returns a new instance of Generator.
8 9 10 11 12 |
# File 'lib/mongify/mongoid/generator.rb', line 8 def initialize(translation_file, output_dir) @translation_file = translation_file @output_dir = output_dir @models = {} end |
Instance Attribute Details
#models ⇒ Object (readonly)
Returns the value of attribute models.
7 8 9 |
# File 'lib/mongify/mongoid/generator.rb', line 7 def models @models end |
Instance Method Details
#find_model(name) ⇒ Model?
Returns model based on table name
64 65 66 |
# File 'lib/mongify/mongoid/generator.rb', line 64 def find_model(name) @models[name.to_s.downcase.to_sym] end |
#generate_embedded_relations ⇒ Array
Goes through embedded relationships
47 48 49 50 51 |
# File 'lib/mongify/mongoid/generator.rb', line 47 def translation..each do |table| (table) end end |
#generate_models ⇒ Array
Generate models based on traslation tables
30 31 32 33 34 |
# File 'lib/mongify/mongoid/generator.rb', line 30 def generate_models (translation.tables + translation.polymorphic_tables).each do |table| build_model(table) end end |
#process ⇒ nil
Process translation file and generate output files
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mongify/mongoid/generator.rb', line 16 def process unless File.exists?(@translation_file) raise Mongify::Mongoid::TranslationFileNotFound, "Unable to find Translation File at #{@translation_file}" end generate_models process_fields write_models_to_file nil end |
#process_fields ⇒ Array
Goes through all the models and adds fields based on columns in the given table
38 39 40 41 42 43 |
# File 'lib/mongify/mongoid/generator.rb', line 38 def process_fields models.each do |key, model| table = translation.find(model.table_name) model = generate_fields_for model, table if table end end |
#translation ⇒ Mongify::Translation
Returns Mongify translation class for given translation file
70 71 72 |
# File 'lib/mongify/mongoid/generator.rb', line 70 def translation @translation ||= Mongify::Translation.parse(@translation_file) end |