Class ActiveRecord::ConnectionAdapters::Column
In: vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
Parent: Object

An abstract definition of a column in a table.

Methods

Classes and Modules

Module ActiveRecord::ConnectionAdapters::Column::Format

Constants

TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set

Attributes

default  [R] 
limit  [R] 
name  [R] 
null  [R] 
precision  [R] 
primary  [RW] 
scale  [R] 
sql_type  [R] 
type  [R] 

Public Class methods

Used to convert from BLOBs to Strings

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 118
118:         def binary_to_string(value)
119:           value
120:         end

Instantiates a new column in the table.

name is the column‘s name, such as supplier_id in supplier_id int(11). default is the type-casted default value, such as new in sales_stage varchar(20) default ‘new. sql_type is only used to extract the column‘s length, if necessary. For example +60+ in company_name varchar(60). null determines if this column allows NULL values.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 26
26:       def initialize(name, default, sql_type = nil, null = true)
27:         @name, @sql_type, @null = name, sql_type, null
28:         @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
29:         @type = simplified_type(sql_type)
30:         @default = extract_default(default)
31: 
32:         @primary = nil
33:       end

Used to convert from Strings to BLOBs

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 113
113:         def string_to_binary(value)
114:           value
115:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 122
122:         def string_to_date(string)
123:           return string unless string.is_a?(String)
124:           return nil if string.empty?
125: 
126:           fast_string_to_date(string) || fallback_string_to_date(string)
127:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 136
136:         def string_to_dummy_time(string)
137:           return string unless string.is_a?(String)
138:           return nil if string.empty?
139: 
140:           string_to_time "2000-01-01 #{string}"
141:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 129
129:         def string_to_time(string)
130:           return string unless string.is_a?(String)
131:           return nil if string.empty?
132: 
133:           fast_string_to_time(string) || fallback_string_to_time(string)
134:         end

convert something to a boolean

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 144
144:         def value_to_boolean(value)
145:           if value.is_a?(String) && value.blank?
146:             nil
147:           else
148:             TRUE_VALUES.include?(value)
149:           end
150:         end

convert something to a BigDecimal

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 153
153:         def value_to_decimal(value)
154:           # Using .class is faster than .is_a? and
155:           # subclasses of BigDecimal will be handled
156:           # in the else clause
157:           if value.class == BigDecimal
158:             value
159:           elsif value.respond_to?(:to_d)
160:             value.to_d
161:           else
162:             value.to_s.to_d
163:           end
164:         end

Protected Class methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 200
200:           def fallback_string_to_date(string)
201:             new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
202:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 204
204:           def fallback_string_to_time(string)
205:             time_hash = Date._parse(string)
206:             time_hash[:sec_fraction] = microseconds(time_hash)
207: 
208:             new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
209:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 186
186:           def fast_string_to_date(string)
187:             if string =~ Format::ISO_DATE
188:               new_date $1.to_i, $2.to_i, $3.to_i
189:             end
190:           end

Doesn‘t handle time zones.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 193
193:           def fast_string_to_time(string)
194:             if string =~ Format::ISO_DATETIME
195:               microsec = ($7.to_f * 1_000_000).to_i
196:               new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
197:             end
198:           end

‘0.123456’ -> 123456 ‘1.123456’ -> 123456

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 169
169:           def microseconds(time)
170:             ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
171:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 173
173:           def new_date(year, mon, mday)
174:             if year && year != 0
175:               Date.new(year, mon, mday) rescue nil
176:             end
177:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 179
179:           def new_time(year, mon, mday, hour, min, sec, microsec)
180:             # Treat 0000-00-00 00:00:00 as nil.
181:             return nil if year.nil? || year == 0
182: 
183:             Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
184:           end

Public Instance methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 107
107:       def extract_default(default)
108:         type_cast(default)
109:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 43
43:       def has_default?
44:         !default.nil?
45:       end

Returns the human name of the column name.

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 103
103:       def human_name
104:         Base.human_attribute_name(@name)
105:       end

Returns the Ruby class that corresponds to the abstract data type.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 48
48:       def klass
49:         case type
50:           when :integer       then Fixnum
51:           when :float         then Float
52:           when :decimal       then BigDecimal
53:           when :datetime      then Time
54:           when :date          then Date
55:           when :timestamp     then Time
56:           when :time          then Time
57:           when :text, :string then String
58:           when :binary        then String
59:           when :boolean       then Object
60:         end
61:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 39
39:       def number?
40:         type == :integer || type == :float || type == :decimal
41:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 35
35:       def text?
36:         type == :string || type == :text
37:       end

Casts value (which is a String) to an appropriate instance.

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 64
64:       def type_cast(value)
65:         return nil if value.nil?
66:         case type
67:           when :string    then value
68:           when :text      then value
69:           when :integer   then value.to_i rescue value ? 1 : 0
70:           when :float     then value.to_f
71:           when :decimal   then self.class.value_to_decimal(value)
72:           when :datetime  then self.class.string_to_time(value)
73:           when :timestamp then self.class.string_to_time(value)
74:           when :time      then self.class.string_to_dummy_time(value)
75:           when :date      then self.class.string_to_date(value)
76:           when :binary    then self.class.binary_to_string(value)
77:           when :boolean   then self.class.value_to_boolean(value)
78:           else value
79:         end
80:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 82
82:       def type_cast_code(var_name)
83:         case type
84:           when :string    then nil
85:           when :text      then nil
86:           when :integer   then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
87:           when :float     then "#{var_name}.to_f"
88:           when :decimal   then "#{self.class.name}.value_to_decimal(#{var_name})"
89:           when :datetime  then "#{self.class.name}.string_to_time(#{var_name})"
90:           when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
91:           when :time      then "#{self.class.name}.string_to_dummy_time(#{var_name})"
92:           when :date      then "#{self.class.name}.string_to_date(#{var_name})"
93:           when :binary    then "#{self.class.name}.binary_to_string(#{var_name})"
94:           when :boolean   then "#{self.class.name}.value_to_boolean(#{var_name})"
95:           else nil
96:         end
97:       end

[Validate]