Class: ToyModel::AttributeWrapper
- Inherits:
-
Object
- Object
- ToyModel::AttributeWrapper
- Defined in:
- lib/toy-model/attribute_wrapper.rb
Instance Attribute Summary collapse
-
#_as ⇒ Object
Returns the value of attribute _as.
-
#_helper ⇒ Object
Returns the value of attribute _helper.
-
#_options ⇒ Object
Returns the value of attribute _options.
-
#column ⇒ Object
Returns the value of attribute column.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #as(value) ⇒ Object
- #enforce_relations ⇒ Object
-
#initialize(column, parent) ⇒ AttributeWrapper
constructor
A new instance of AttributeWrapper.
- #skip_table_column? ⇒ Boolean
- #to_table_column ⇒ Object
- #to_table_type ⇒ Object
Constructor Details
#initialize(column, parent) ⇒ AttributeWrapper
Returns a new instance of AttributeWrapper.
6 7 8 9 10 |
# File 'lib/toy-model/attribute_wrapper.rb', line 6 def initialize column, parent @column = column @parent = parent @_as = :string end |
Instance Attribute Details
#_as ⇒ Object
Returns the value of attribute _as.
4 5 6 |
# File 'lib/toy-model/attribute_wrapper.rb', line 4 def _as @_as end |
#_helper ⇒ Object
Returns the value of attribute _helper.
4 5 6 |
# File 'lib/toy-model/attribute_wrapper.rb', line 4 def _helper @_helper end |
#_options ⇒ Object
Returns the value of attribute _options.
4 5 6 |
# File 'lib/toy-model/attribute_wrapper.rb', line 4 def @_options end |
#column ⇒ Object
Returns the value of attribute column.
4 5 6 |
# File 'lib/toy-model/attribute_wrapper.rb', line 4 def column @column end |
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/toy-model/attribute_wrapper.rb', line 4 def parent @parent end |
Instance Method Details
#as(value) ⇒ Object
12 13 14 15 16 |
# File 'lib/toy-model/attribute_wrapper.rb', line 12 def as value @_as = value enforce_relations self end |
#enforce_relations ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/toy-model/attribute_wrapper.rb', line 18 def enforce_relations @parent.send @_as, @column if [:has_many, :has_and_belongs_to_many, :has_one, :belongs_to].include? @_as if [:has_many, :has_one].include? @_as = @column.to_s.singularize.classify.constantize .attribute(@parent.to_s.underscore.gsub(' ', '_').to_sym).as(:belongs_to) ToyModel::AttributeObserver.new end end |
#skip_table_column? ⇒ Boolean
39 40 41 |
# File 'lib/toy-model/attribute_wrapper.rb', line 39 def skip_table_column? [:has_many, :has_and_belongs_to_many, :has_one].include? @_as end |
#to_table_column ⇒ Object
27 28 29 30 31 |
# File 'lib/toy-model/attribute_wrapper.rb', line 27 def to_table_column return nil if skip_table_column? return :"#{@column}_id" if @_as == :belongs_to @column end |
#to_table_type ⇒ Object
33 34 35 36 37 |
# File 'lib/toy-model/attribute_wrapper.rb', line 33 def to_table_type return nil if skip_table_column? return :integer if @_as == :belongs_to @_as end |