Class: Mongify::Mongoid::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/mongoid/generator.rb

Overview

Generator - Processes translation file and generates output based on models

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modelsObject (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

Parameters:

  • name (String)

    Name of table

Returns:

  • (Model, nil)

    Found model or nil



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_relationsArray

Goes through embedded relationships

Returns:

  • (Array)

    List of tables that were used to extra embedded relations



47
48
49
50
51
# File 'lib/mongify/mongoid/generator.rb', line 47

def generate_embedded_relations
  translation.embed_tables.each do |table|
    extract_embedded_relations(table)
  end
end

#generate_modelsArray

Generate models based on traslation tables

Returns:

  • (Array)

    List of tables that models were generated from



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

#processnil

Process translation file and generate output files

Returns:

  • (nil)


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
  generate_embedded_relations
  write_models_to_file
  nil
end

#process_fieldsArray

Goes through all the models and adds fields based on columns in the given table

Returns:

  • (Array)

    List of models that fields were added to



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

#translationMongify::Translation

Returns Mongify translation class for given translation file

Returns:

  • (Mongify::Translation)

    Translation file



70
71
72
# File 'lib/mongify/mongoid/generator.rb', line 70

def translation
  @translation ||= Mongify::Translation.parse(@translation_file)
end

#write_models_to_filePrinter

Writes models to files

Returns:

  • (Printer)

    Printer class used to write output



55
56
57
58
59
# File 'lib/mongify/mongoid/generator.rb', line 55

def write_models_to_file
  Printer.new(models, @output_dir).tap do |p|
    p.write
  end
end