Module: ModelBuilder
- Defined in:
- lib/model_builder.rb,
lib/model_builder/version.rb,
lib/model_builder/class_builder.rb
Defined Under Namespace
Modules: ClassBuilder
Constant Summary collapse
- VERSION =
'2.2.0'
Class Method Summary collapse
- .build(class_full_name, opts = {}) ⇒ Object
- .clean ⇒ Object
- .create_attribute(migration, key, opts) ⇒ Object
- .create_attributes(migration, attributes = {}) ⇒ Object
- .create_table(class_full_name, opts) ⇒ Object
- .define_validations(klass, validations) ⇒ Object
- .drop_table(class_full_name) ⇒ Object
- .dynamic_models ⇒ Object
- .get_default_options ⇒ Object
- .table_already_exists(class_full_name) ⇒ Object
- .table_name_for(class_full_name) ⇒ Object
Class Method Details
.build(class_full_name, opts = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/model_builder.rb', line 7 def self.build(class_full_name, opts={}) opts.reverse_merge! klass = ClassBuilder.build class_full_name, opts unless table_already_exists class_full_name create_table class_full_name, opts define_validations klass, opts[:validates] end klass end |
.clean ⇒ Object
66 67 68 69 |
# File 'lib/model_builder.rb', line 66 def self.clean dynamic_models.map {|c| drop_table c } @@dynamic_models = [] end |
.create_attribute(migration, key, opts) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/model_builder.rb', line 49 def self.create_attribute(migration, key, opts) if opts.is_a?(Hash) opts = opts.clone type = opts.delete :type else type = opts opts = {} end migration.send(type, key, opts) end |
.create_attributes(migration, attributes = {}) ⇒ Object
43 44 45 46 47 |
# File 'lib/model_builder.rb', line 43 def self.create_attributes(migration, attributes={}) attributes.each_pair do |key, value| create_attribute(migration, key, value) end end |
.create_table(class_full_name, opts) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/model_builder.rb', line 35 def self.create_table(class_full_name, opts) ActiveRecord::Migration.create_table(table_name_for(class_full_name)) do |migration| create_attributes(migration, opts[:attributes]) end @@dynamic_models << class_full_name end |
.define_validations(klass, validations) ⇒ Object
60 61 62 63 64 |
# File 'lib/model_builder.rb', line 60 def self.define_validations(klass, validations) return if validations.nil? or !validations.kind_of?(Array) or validations.empty? validations = [validations] unless validations.first.kind_of? Array validations.each {|v| klass.validates *v} end |
.drop_table(class_full_name) ⇒ Object
75 76 77 |
# File 'lib/model_builder.rb', line 75 def self.drop_table(class_full_name) ActiveRecord::Migration.drop_table(table_name_for(class_full_name)) end |
.dynamic_models ⇒ Object
71 72 73 |
# File 'lib/model_builder.rb', line 71 def self.dynamic_models @@dynamic_models end |
.get_default_options ⇒ Object
20 21 22 23 24 25 |
# File 'lib/model_builder.rb', line 20 def self. { superclass: ::ActiveRecord::Base, attributes: {} } end |
.table_already_exists(class_full_name) ⇒ Object
27 28 29 |
# File 'lib/model_builder.rb', line 27 def self.table_already_exists(class_full_name) ActiveRecord::Base.connection.tables.include? table_name_for(class_full_name) end |
.table_name_for(class_full_name) ⇒ Object
31 32 33 |
# File 'lib/model_builder.rb', line 31 def self.table_name_for(class_full_name) class_full_name.tableize.gsub(/\//,'_') end |