Class: Hecks::Adapters::SQLDatabase::Column

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

Constant Summary collapse

TYPE_MAP =
{
  'Currency' => "BigDecimal",
  'String'   => 'String',
  'Integer'  => 'Integer'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, referenced_object: nil, table_name: nil, type:, is_list: false) ⇒ Column

Returns a new instance of Column.



12
13
14
15
16
17
18
# File 'lib/column.rb', line 12

def initialize(name:, referenced_object: nil, table_name: nil, type:, is_list: false)
  @name = name
  @referenced_object = referenced_object
  @type = type
  @table_name = table_name
  @is_list = is_list
end

Instance Attribute Details

#referenced_objectObject (readonly)

Returns the value of attribute referenced_object.



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

def referenced_object
  @referenced_object
end

Class Method Details

.factory(attribute) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/column.rb', line 20

def self.factory(attribute)
  new(
    name: attribute.name,
    referenced_object: attribute.referenced_object,
    type: attribute.type,
    table_name: attribute.object_name,
    is_list: attribute.list?
  )
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
69
70
# File 'lib/column.rb', line 66

def ==(other)
  return false if name != other.name
  return false if referenced_table != other.referenced_table
  true
end

#copy(new_attributes = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/column.rb', line 55

def copy(new_attributes={})
  self.class.new(
    {
      name: self.name,
      referenced_object: self.referenced_object,
      type: self.type,
      is_list: self.list?
    }.merge(new_attributes)
  )
end

#list?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/column.rb', line 38

def list?
  @is_list
end

#nameObject



51
52
53
# File 'lib/column.rb', line 51

def name
  @name
end

#reference?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/column.rb', line 42

def reference?
  @referenced_object
end

#referenced_tableObject



46
47
48
49
# File 'lib/column.rb', line 46

def referenced_table
  return unless @referenced_object
  @referenced_object.pluralize.underscore
end

#to_foreign_keyObject



30
31
32
# File 'lib/column.rb', line 30

def to_foreign_key
  (type.downcase + '_id').to_sym
end

#to_table_nameObject



34
35
36
# File 'lib/column.rb', line 34

def to_table_name
  name.downcase.pluralize.to_sym
end

#typeObject



72
73
74
# File 'lib/column.rb', line 72

def type
  TYPE_MAP[@type] || @type
end