Class: ActiveMocker::ModelSchema::Generate
- Inherits:
-
Object
- Object
- ActiveMocker::ModelSchema::Generate
- Defined in:
- lib/active_mocker/model_schema/generate.rb
Instance Attribute Summary collapse
-
#models_dir ⇒ Object
readonly
Returns the value of attribute models_dir.
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
-
#schema_file ⇒ Object
readonly
Returns the value of attribute schema_file.
Instance Method Summary collapse
-
#build_attributes(attributes, primary_attribute) ⇒ Object
noinspection RubyArgCount.
-
#build_methods(model) ⇒ Object
noinspection RubyArgCount.
- #build_relationships(model) ⇒ Object
- #find_join_table(relation, model) ⇒ Object
- #get_model(model_file_name) ⇒ Object
- #get_table(model, model_name) ⇒ Object
- #get_table_name(model_table_name, model_name) ⇒ Object
- #increment_progress ⇒ Object
-
#initialize(schema_file: nil, models_dir: nil, logger: nil, progress: nil) ⇒ Generate
constructor
A new instance of Generate.
- #models ⇒ Object
- #primary_key(attributes, model) ⇒ Object
- #relations_by_type(model) ⇒ Object
-
#run ⇒ Object
noinspection RubyArgCount.
- #table_to_class_name(table) ⇒ Object
- #table_to_model_file(table_name) ⇒ Object
- #tables ⇒ Object
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_dir ⇒ Object (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 |
#progress ⇒ Object (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_file ⇒ Object (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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_mocker/model_schema/generate.rb', line 59 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_mocker/model_schema/generate.rb', line 75 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/active_mocker/model_schema/generate.rb', line 93 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
117 118 119 120 121 122 123 |
# File 'lib/active_mocker/model_schema/generate.rb', line 117 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
165 166 167 |
# File 'lib/active_mocker/model_schema/generate.rb', line 165 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
169 170 171 172 |
# File 'lib/active_mocker/model_schema/generate.rb', line 169 def get_table_name(model_table_name, model_name) return model_name.tableize if model_table_name.nil? return model_table_name end |
#increment_progress ⇒ Object
16 17 18 |
# File 'lib/active_mocker/model_schema/generate.rb', line 16 def increment_progress progress.increment unless progress.nil? end |
#models ⇒ Object
159 160 161 162 163 |
# File 'lib/active_mocker/model_schema/generate.rb', line 159 def models Dir["#{models_dir}/*.rb"].map do |file| Pathname.new(file).basename.to_s.sub('.rb', '') end end |
#primary_key(attributes, model) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/active_mocker/model_schema/generate.rb', line 125 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
108 109 110 111 112 113 114 115 |
# File 'lib/active_mocker/model_schema/generate.rb', line 108 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 |
#run ⇒ Object
noinspection RubyArgCount
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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) attributes = build_attributes(table.fields, primary_key(table.fields, model)) 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
178 179 180 |
# File 'lib/active_mocker/model_schema/generate.rb', line 178 def table_to_class_name(table) table.camelize.singularize end |
#table_to_model_file(table_name) ⇒ Object
174 175 176 |
# File 'lib/active_mocker/model_schema/generate.rb', line 174 def table_to_model_file(table_name) table_name.singularize end |
#tables ⇒ Object
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 |