Class: ActiveMocker::ModelSchema::Assemble
- Inherits:
-
Object
- Object
- ActiveMocker::ModelSchema::Assemble
- Defined in:
- lib/active_mocker/model_schema/assemble.rb
Instance Attribute Summary collapse
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
Instance Method Summary collapse
- #build_attributes(attributes, primary_attribute) ⇒ Object
- #build_methods(model) ⇒ Object
- #build_relationships(model) ⇒ Object
- #find_join_table(relation, model) ⇒ Object
- #get_belongs_to(class_name, foreign_key) ⇒ 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(progress: nil) ⇒ Assemble
constructor
A new instance of Assemble.
- #models ⇒ Object
- #primary_key(attributes, model) ⇒ Object
- #relations_by_type(model) ⇒ Object
- #run ⇒ Object
- #table_to_class_name(table) ⇒ Object
- #table_to_model_file(table_name) ⇒ Object
- #tables ⇒ Object
Constructor Details
#initialize(progress: nil) ⇒ Assemble
Returns a new instance of Assemble.
39 40 41 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 39 def initialize(progress: nil) @progress = progress end |
Instance Attribute Details
#progress ⇒ Object (readonly)
Returns the value of attribute progress.
37 38 39 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 37 def progress @progress end |
Instance Method Details
#build_attributes(attributes, primary_attribute) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 90 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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 105 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
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 123 def build_relationships(model) relations_by_type(model).map do |type, relations| relations.map do |relation| Relationships.new(name: relation.name, class_name: relation.class_name, type: type, through: nil, source: nil, foreign_key: relation.foreign_key, join_table: nil) end end.flatten end |
#find_join_table(relation, model) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 145 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_belongs_to(class_name, foreign_key) ⇒ Object
212 213 214 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 212 def get_belongs_to(class_name, foreign_key) get_table(nil, class_name) end |
#get_model(model_file_name) ⇒ Object
193 194 195 196 197 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 193 def get_model(model_file_name) model = ModelReader.new.parse(model_file_name) raise ModelLoadError::General.new(model_file_name) unless model unless model model end |
#get_table(model, model_name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 51 def get_table(model, model_name) if model.parent_class.present? model = get_model(model_name) end selected_table = tables.select{|table| table.name == model.table_name}.first if selected_table.nil? Logger.info "Table: `#{model.table_name}`, can not be found for model #{model_name.camelize}.\n" end selected_table end |
#get_table_name(model_table_name, model_name) ⇒ Object
199 200 201 202 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 199 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
43 44 45 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 43 def increment_progress progress.increment unless progress.nil? end |
#models ⇒ Object
187 188 189 190 191 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 187 def models Dir["#{Config.model_dir}/*.rb"].map do |file| Pathname.new(file).basename.to_s.sub('.rb', '') end end |
#primary_key(attributes, model) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 153 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
137 138 139 140 141 142 143 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 137 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 62 def run model_schemas = models.map do |model_name| begin model = get_model(model_name) table = get_table(model, 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: -> { model.try(:table_name) }, attributes: -> { attributes }, _methods: -> { build_methods(model) }, relationships: -> { build_relationships(model) }, constants: -> { model.constants }, modules: -> { model.modules }, parent_class: -> { model.parent_class }, abstract_class: -> { model.abstract_class} ) rescue Exception => e e end end ModelSchemaCollection.new(model_schemas.compact) end |
#table_to_class_name(table) ⇒ Object
208 209 210 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 208 def table_to_class_name(table) table.camelize.singularize end |
#table_to_model_file(table_name) ⇒ Object
204 205 206 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 204 def table_to_model_file(table_name) table_name.singularize end |
#tables ⇒ Object
47 48 49 |
# File 'lib/active_mocker/model_schema/assemble.rb', line 47 def tables @tables ||= SchemaReader.new.tables end |