Class: ActiveMocker::ModelSchema::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mocker/model_schema/generate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_file: nil, models_dir: nil, logger: nil, progress: nil) ⇒ Generate

Returns a new instance of Generate.



9
10
11
12
13
14
# File 'lib/active_mocker/model_schema/generate.rb', line 9

def initialize(schema_file:nil, models_dir:nil, logger:nil, progress: nil)
  @schema_file = schema_file
  @models_dir  = models_dir
  @progress    = progress
  Logger.set(logger)
end

Instance Attribute Details

#models_dirObject (readonly)

Returns the value of attribute models_dir.



7
8
9
# File 'lib/active_mocker/model_schema/generate.rb', line 7

def models_dir
  @models_dir
end

#progressObject (readonly)

Returns the value of attribute progress.



7
8
9
# File 'lib/active_mocker/model_schema/generate.rb', line 7

def progress
  @progress
end

#schema_fileObject (readonly)

Returns the value of attribute schema_file.



7
8
9
# File 'lib/active_mocker/model_schema/generate.rb', line 7

def schema_file
  @schema_file
end

Instance Method Details

#build_attributes(attributes, primary_attribute) ⇒ Object

noinspection RubyArgCount



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_mocker/model_schema/generate.rb', line 61

def build_attributes(attributes, primary_attribute)
  attributes.map do |attr|
    attribute = ModelSchema::Attributes
                  .new(name:          attr.name,
                       type:          attr.type,
                       precision:     attr.precision,
                       scale:         attr.scale,
                       default_value: attr.default)
    if primary_attribute == attr
      attribute.primary_key = true
    end
    attribute
  end
end

#build_methods(model) ⇒ Object

noinspection RubyArgCount



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_mocker/model_schema/generate.rb', line 77

def build_methods(model)
  result = []
  {scope: model.scopes_with_arguments,
   instance: model.instance_methods_with_arguments,
   class: model.class_methods_with_arguments}.each do |type,methods|
    methods.map do |method|
      method_name = method.keys.first.to_s
      arguments   = method.values.first

      result.push(ModelSchema::Methods.new(name:      method_name,
                                           arguments: arguments,
                                           type:      type,
                                           proc:      method[:proc]))
    end
  end
  result
end

#build_relationships(model) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_mocker/model_schema/generate.rb', line 95

def build_relationships(model)
  relations_by_type(model).map do |type, relations|
    relations.map do |relation|
      join_table = nil
      join_table = find_join_table(relation, model) if type == :has_and_belongs_to_many
      Relationships.new(name:        relation.name,
                        class_name:  relation.class_name,
                        type:        type,
                        through:     relation.through,
                        foreign_key: relation.foreign_key,
                        join_table:  join_table)
    end
  end.flatten
end

#find_join_table(relation, model) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/active_mocker/model_schema/generate.rb', line 119

def find_join_table(relation, model)
  return relation.join_table if relation.respond_to?(:join_table) && relation.join_table
  tables.select do |table|
    
    "#{model.table_name}_#{relation.name}" == table.name.to_s || "#{relation.name}_#{model.table_name}" == table.name.to_s
  end.first
end

#get_model(model_file_name) ⇒ Object



167
168
169
# File 'lib/active_mocker/model_schema/generate.rb', line 167

def get_model(model_file_name)
  ModelReader.new({model_dir: models_dir}).parse(model_file_name)
end

#get_table(model, model_name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/active_mocker/model_schema/generate.rb', line 24

def get_table(model , model_name)
  table_name = get_table_name(model.table_name, model_name)
  selected_table = tables.select{|table| table.name == table_name}.first
  if selected_table.nil?
    Logger.error "The Implicit or defined table, `#{table_name}`, can not be found for model #{model_name.camelize}."
  end
  tables.delete(selected_table)
  selected_table
end

#get_table_name(model_table_name, model_name) ⇒ Object



171
172
173
174
# File 'lib/active_mocker/model_schema/generate.rb', line 171

def get_table_name(model_table_name, model_name)
  return model_name.tableize if model_table_name.nil?
  return model_table_name
end

#increment_progressObject



16
17
18
# File 'lib/active_mocker/model_schema/generate.rb', line 16

def increment_progress
  progress.increment unless progress.nil?
end

#modelsObject



161
162
163
164
165
# File 'lib/active_mocker/model_schema/generate.rb', line 161

def models
  Dir["#{models_dir}/*.rb"].map do |file|
    Pathname.new(file).basename.to_s.sub('.rb', '')
  end
end

#primary_key(attributes, model) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/active_mocker/model_schema/generate.rb', line 127

def primary_key(attributes, model)
  result = model_primary_key_attribute(attributes, model)
  return result unless result.nil?

  result = find_primary_key(attributes)
  return result unless result.nil?

  find_id_attribute(attributes)
end

#relations_by_type(model) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/active_mocker/model_schema/generate.rb', line 110

def relations_by_type(model)
  {belongs_to: model.belongs_to,
   has_one: model.has_one,
   has_many: model.has_many,
   has_and_belongs_to_many: model.has_and_belongs_to_many

  }
end

#runObject

noinspection RubyArgCount



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_mocker/model_schema/generate.rb', line 35

def run
  model_schemas = models.map do |model_name|

    model = get_model(model_name)
    next if model == false
    table = get_table(model, model_name)
    table_name = model_name
    attributes = []
    attributes = build_attributes(table.fields, primary_key(table.fields, model)) unless table.nil?

    increment_progress

    ModelSchema.new(class_name: model_name.camelize,
                    table_name: table_name,
                    attributes: attributes,
                    methods: build_methods(model),
                    relationships: build_relationships(model),
                    constants: model.constants,
                    modules: model.modules)

  end

  ModelSchemaCollection.new(model_schemas.compact)
end

#table_to_class_name(table) ⇒ Object



180
181
182
# File 'lib/active_mocker/model_schema/generate.rb', line 180

def table_to_class_name(table)
  table.camelize.singularize
end

#table_to_model_file(table_name) ⇒ Object



176
177
178
# File 'lib/active_mocker/model_schema/generate.rb', line 176

def table_to_model_file(table_name)
  table_name.singularize
end

#tablesObject



20
21
22
# File 'lib/active_mocker/model_schema/generate.rb', line 20

def tables
  @tables ||= SchemaReader.new({schema_file: schema_file}).search(nil)
end