Class: MotionModel::Model::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_model/model/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, type = nil, options = {}) ⇒ Column

Returns a new instance of Column.



9
10
11
12
13
14
15
16
# File 'lib/motion_model/model/column.rb', line 9

def initialize(name = nil, type = nil, options = {})
  @name = name
  @type = type
  raise RuntimeError.new "columns need a type declared." if type.nil?
  @default = options.delete :default
  @destroy = options.delete :dependent
  @options = options
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/motion_model/model/column.rb', line 6

def default
  @default
end

#destroyObject

Returns the value of attribute destroy.



7
8
9
# File 'lib/motion_model/model/column.rb', line 7

def destroy
  @destroy
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/motion_model/model/column.rb', line 4

def name
  @name
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/motion_model/model/column.rb', line 5

def type
  @type
end

Instance Method Details

#classifyObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/motion_model/model/column.rb', line 22

def classify
  case @type
  when :belongs_to
    @klass ||= Object.const_get(@name.to_s.camelize)
  when :has_many
    @klass ||= Object.const_get(@name.to_s.singularize.camelize)
  else
    raise "#{@name} is not a relation. This isn't supposed to happen."
  end
end

#optionsObject



18
19
20
# File 'lib/motion_model/model/column.rb', line 18

def options
  @options
end