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

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 111
111:         def binary_to_string(value)
112:           value
113:         end

Instantiates a new column in the table.

name is the column‘s name, as in supplier_id int(11). default is the type-casted default value, such as sales_stage varchar(20) default new. sql_type is only used to extract the column‘s length, if necessary. For example, 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 23
23:       def initialize(name, default, sql_type = nil, null = true)
24:         @name, @sql_type, @null = name, sql_type, null
25:         @limit, @precision, @scale  = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type) 
26:         @type = simplified_type(sql_type)
27:         @default = extract_default(default)
28: 
29:         @primary = nil
30:       end

Used to convert from Strings to BLOBs

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 106
106:         def string_to_binary(value)
107:           value
108:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 115
115:         def string_to_date(string)
116:           return string unless string.is_a?(String)
117:           return nil if string.empty?
118: 
119:           fast_string_to_date(string) || fallback_string_to_date(string)
120:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 129
129:         def string_to_dummy_time(string)
130:           return string unless string.is_a?(String)
131:           return nil if string.empty?
132: 
133:           string_to_time "2000-01-01 #{string}"
134:         end

[Source]

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

convert something to a boolean

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 137
137:         def value_to_boolean(value)
138:           if value == true || value == false
139:             value
140:           else
141:             %w(true t 1).include?(value.to_s.downcase)
142:           end
143:         end

convert something to a BigDecimal

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 146
146:         def value_to_decimal(value)
147:           if value.is_a?(BigDecimal)
148:             value
149:           elsif value.respond_to?(:to_d)
150:             value.to_d
151:           else
152:             value.to_s.to_d
153:           end
154:         end

Protected Class methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 194
194:           def fallback_string_to_date(string)
195:             new_date *ParseDate.parsedate(string)[0..2]
196:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 198
198:           def fallback_string_to_time(string)
199:             time_hash = Date._parse(string)
200:             time_hash[:sec_fraction] = microseconds(time_hash)
201: 
202:             new_time *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)
203:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 180
180:           def fast_string_to_date(string)
181:             if string =~ Format::ISO_DATE
182:               new_date $1.to_i, $2.to_i, $3.to_i
183:             end
184:           end

Doesn‘t handle time zones.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 187
187:           def fast_string_to_time(string)
188:             if string =~ Format::ISO_DATETIME
189:               microsec = ($7.to_f * 1_000_000).to_i
190:               new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
191:             end
192:           end

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

[Source]

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 163
163:           def new_date(year, mon, mday)
164:             if year && year != 0
165:               Date.new(year, mon, mday) rescue nil
166:             end
167:           end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 169
169:           def new_time(year, mon, mday, hour, min, sec, microsec)
170:             # Treat 0000-00-00 00:00:00 as nil.
171:             return nil if year.nil? || year == 0
172: 
173:             Time.send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec)
174:           # Over/underflow to DateTime
175:           rescue ArgumentError, TypeError
176:             zone_offset = Base.default_timezone == :local ? DateTime.local_offset : 0
177:             DateTime.civil(year, mon, mday, hour, min, sec, zone_offset) rescue nil
178:           end

Public Instance methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 100
100:       def extract_default(default)
101:         type_cast(default)
102:       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 96
96:       def human_name
97:         Base.human_attribute_name(@name)
98:       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 41
41:       def klass
42:         case type
43:           when :integer       then Fixnum
44:           when :float         then Float
45:           when :decimal       then BigDecimal
46:           when :datetime      then Time
47:           when :date          then Date
48:           when :timestamp     then Time
49:           when :time          then Time
50:           when :text, :string then String
51:           when :binary        then String
52:           when :boolean       then Object
53:         end
54:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 36
36:       def number?
37:         [:float, :integer, :decimal].include? type
38:       end

[Source]

    # File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 32
32:       def text?
33:         [:string, :text].include? type
34:       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 57
57:       def type_cast(value)
58:         return nil if value.nil?
59:         case type
60:           when :string    then value
61:           when :text      then value
62:           when :integer   then value.to_i rescue value ? 1 : 0
63:           when :float     then value.to_f
64:           when :decimal   then self.class.value_to_decimal(value)
65:           when :datetime  then self.class.string_to_time(value)
66:           when :timestamp then self.class.string_to_time(value)
67:           when :time      then self.class.string_to_dummy_time(value)
68:           when :date      then self.class.string_to_date(value)
69:           when :binary    then self.class.binary_to_string(value)
70:           when :boolean   then self.class.value_to_boolean(value)
71:           else value
72:         end
73:       end

[Source]

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

[Validate]