Class: ActiveRecord::ConnectionAdapters::Column

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

Overview

An abstract definition of a column in a table.

Direct Known Subclasses

MySQL::Column, NullColumn, PostgreSQL::Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, sql_type_metadata = nil, null = true, default_function = nil, collation: nil, comment: nil) ⇒ Column

Instantiates a new column in the table.

name is the column's name, such as supplier_id in supplier_id bigint. default is the type-casted default value, such as new in sales_stage varchar(20) default 'new'. sql_type_metadata is various information about the type of the column null determines if this column allows NULL values.



18
19
20
21
22
23
24
25
26
# File 'lib/active_record/connection_adapters/column.rb', line 18

def initialize(name, default,  = nil, null = true, default_function = nil, collation: nil, comment: nil, **)
  @name = name.freeze
  @sql_type_metadata = 
  @null = null
  @default = default
  @default_function = default_function
  @collation = collation
  @comment = comment
end

Instance Attribute Details

#collationObject (readonly)

Returns the value of attribute collation.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def collation
  @collation
end

#commentObject (readonly)

Returns the value of attribute comment.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def comment
  @comment
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def default
  @default
end

#default_functionObject (readonly)

Returns the value of attribute default_function.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def default_function
  @default_function
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def null
  @null
end

#sql_type_metadataObject (readonly)

Returns the value of attribute sql_type_metadata.



8
9
10
# File 'lib/active_record/connection_adapters/column.rb', line 8

def 
  @sql_type_metadata
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



64
65
66
67
68
69
70
71
72
73
# File 'lib/active_record/connection_adapters/column.rb', line 64

def ==(other)
  other.is_a?(Column) &&
    name == other.name &&
    default == other.default &&
     == other. &&
    null == other.null &&
    default_function == other.default_function &&
    collation == other.collation &&
    comment == other.comment
end

#bigint?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/active_record/connection_adapters/column.rb', line 32

def bigint?
  /\Abigint\b/.match?(sql_type)
end

#encode_with(coder) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/active_record/connection_adapters/column.rb', line 54

def encode_with(coder)
  coder["name"] = @name
  coder["sql_type_metadata"] = @sql_type_metadata
  coder["null"] = @null
  coder["default"] = @default
  coder["default_function"] = @default_function
  coder["collation"] = @collation
  coder["comment"] = @comment
end

#has_default?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_record/connection_adapters/column.rb', line 28

def has_default?
  !default.nil? || default_function
end

#hashObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record/connection_adapters/column.rb', line 76

def hash
  Column.hash ^
    name.hash ^
    default.hash ^
    .hash ^
    null.hash ^
    default_function.hash ^
    collation.hash ^
    comment.hash
end

#human_nameObject

Returns the human name of the column name.

Examples
Column.new('sales_stage', ...).human_name # => 'Sales stage'


40
41
42
# File 'lib/active_record/connection_adapters/column.rb', line 40

def human_name
  Base.human_attribute_name(@name)
end

#init_with(coder) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/column.rb', line 44

def init_with(coder)
  @name = coder["name"]
  @sql_type_metadata = coder["sql_type_metadata"]
  @null = coder["null"]
  @default = coder["default"]
  @default_function = coder["default_function"]
  @collation = coder["collation"]
  @comment = coder["comment"]
end