Class: ToyModel::AttributeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/toy-model/attribute_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#_asObject

Returns the value of attribute _as.



4
5
6
# File 'lib/toy-model/attribute_wrapper.rb', line 4

def _as
  @_as
end

#_helperObject

Returns the value of attribute _helper.



4
5
6
# File 'lib/toy-model/attribute_wrapper.rb', line 4

def _helper
  @_helper
end

#_optionsObject

Returns the value of attribute _options.



4
5
6
# File 'lib/toy-model/attribute_wrapper.rb', line 4

def _options
  @_options
end

#columnObject

Returns the value of attribute column.



4
5
6
# File 'lib/toy-model/attribute_wrapper.rb', line 4

def column
  @column
end

#parentObject

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_relationsObject



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
    related = @column.to_s.singularize.classify.constantize
    related.attribute(@parent.to_s.underscore.gsub(' ', '_').to_sym).as(:belongs_to)
    ToyModel::AttributeObserver.new related
  end
end

#skip_table_column?Boolean

Returns:

  • (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_columnObject



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_typeObject



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