Class: Automigration::FieldsKeeper
- Inherits:
-
Object
- Object
- Automigration::FieldsKeeper
- Defined in:
- lib/automigration/fields_keeper.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#migration_attrs ⇒ Object
readonly
Returns the value of attribute migration_attrs.
Instance Method Summary collapse
- #add_field(type, name, options = {}) ⇒ Object
- #auto_migrable? ⇒ Boolean
- #field_db_columns ⇒ Object
- #field_names ⇒ Object
- #has_fields(options = {}, &block) ⇒ Object
-
#initialize(model) ⇒ FieldsKeeper
constructor
A new instance of FieldsKeeper.
- #migration_attr(*args) ⇒ Object
Constructor Details
#initialize(model) ⇒ FieldsKeeper
Returns a new instance of FieldsKeeper.
6 7 8 9 10 11 12 13 |
# File 'lib/automigration/fields_keeper.rb', line 6 def initialize(model) @model = model @fields = nil @migration_attrs = [] = false @fields_lookup = {} end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
3 4 5 |
# File 'lib/automigration/fields_keeper.rb', line 3 def fields @fields end |
#migration_attrs ⇒ Object (readonly)
Returns the value of attribute migration_attrs.
4 5 6 |
# File 'lib/automigration/fields_keeper.rb', line 4 def migration_attrs @migration_attrs end |
Instance Method Details
#add_field(type, name, options = {}) ⇒ Object
36 37 38 39 40 |
# File 'lib/automigration/fields_keeper.rb', line 36 def add_field(type, name, = {}) has_fields(:timestamps => false) do |f| f.send type, name, end end |
#auto_migrable? ⇒ Boolean
46 47 48 |
# File 'lib/automigration/fields_keeper.rb', line 46 def auto_migrable? @fields.present? end |
#field_db_columns ⇒ Object
54 55 56 57 58 |
# File 'lib/automigration/fields_keeper.rb', line 54 def field_db_columns @fields.map do |field| Field.to_db_columns(field) end.flatten end |
#field_names ⇒ Object
50 51 52 |
# File 'lib/automigration/fields_keeper.rb', line 50 def field_names @field_names ||= fields.map{|f| f[:name]} end |
#has_fields(options = {}, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/automigration/fields_keeper.rb', line 15 def has_fields( = {}, &block) .assert_valid_keys(:timestamps) .reverse_merge!(:timestamps => true) dsl = Automigration::Dsl.new block.arity == 1 ? block.call(dsl) : dsl.instance_exec(&block) dsl.fields.each do |field| Field.extend_model!(@model, field) end @fields ||= [] @fields += dsl.fields if ! && [:timestamps] = true @fields << {:as => :datetime, :name => :created_at, :accessible => false} @fields << {:as => :datetime, :name => :updated_at, :accessible => false} end end |
#migration_attr(*args) ⇒ Object
42 43 44 |
# File 'lib/automigration/fields_keeper.rb', line 42 def migration_attr(*args) @migration_attrs += args.flatten.map(&:to_s) end |