Object
An abstract definition of a column in a table.
Used to convert from BLOBs to Strings
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 124
124: def binary_to_string(value)
125: value
126: 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 used to extract the column’s length, if necessary. For example 60 in company_name varchar(60). It will be mapped to one of the standard Rails SQL types in the type attribute. null determines if this column allows NULL values.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 30
30: def initialize(name, default, sql_type = nil, null = true)
31: @name, @sql_type, @null = name, sql_type, null
32: @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
33: @type = simplified_type(sql_type)
34: @default = extract_default(default)
35:
36: @primary = nil
37: end
Used to convert from Strings to BLOBs
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 119
119: def string_to_binary(value)
120: value
121: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 128
128: def string_to_date(string)
129: return string unless string.is_a?(String)
130: return nil if string.empty?
131:
132: fast_string_to_date(string) || fallback_string_to_date(string)
133: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 142
142: def string_to_dummy_time(string)
143: return string unless string.is_a?(String)
144: return nil if string.empty?
145:
146: string_to_time "2000-01-01 #{string}"
147: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 135
135: def string_to_time(string)
136: return string unless string.is_a?(String)
137: return nil if string.empty?
138:
139: fast_string_to_time(string) || fallback_string_to_time(string)
140: end
convert something to a boolean
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 150
150: def value_to_boolean(value)
151: if value.is_a?(String) && value.blank?
152: nil
153: else
154: TRUE_VALUES.include?(value)
155: end
156: end
convert something to a BigDecimal
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 159
159: def value_to_decimal(value)
160: # Using .class is faster than .is_a? and
161: # subclasses of BigDecimal will be handled
162: # in the else clause
163: if value.class == BigDecimal
164: value
165: elsif value.respond_to?(:to_d)
166: value.to_d
167: else
168: value.to_s.to_d
169: end
170: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 206
206: def fallback_string_to_date(string)
207: new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
208: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 210
210: def fallback_string_to_time(string)
211: time_hash = Date._parse(string)
212: time_hash[:sec_fraction] = microseconds(time_hash)
213:
214: new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
215: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 192
192: def fast_string_to_date(string)
193: if string =~ Format::ISO_DATE
194: new_date $1.to_i, $2.to_i, $3.to_i
195: end
196: end
Doesn’t handle time zones.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 199
199: def fast_string_to_time(string)
200: if string =~ Format::ISO_DATETIME
201: microsec = ($7.to_f * 1_000_000).to_i
202: new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
203: end
204: end
‘0.123456’ -> 123456 ‘1.123456’ -> 123456
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 175
175: def microseconds(time)
176: ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
177: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 179
179: def new_date(year, mon, mday)
180: if year && year != 0
181: Date.new(year, mon, mday) rescue nil
182: end
183: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 185
185: def new_time(year, mon, mday, hour, min, sec, microsec)
186: # Treat 0000-00-00 00:00:00 as nil.
187: return nil if year.nil? || year == 0
188:
189: Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
190: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 113
113: def extract_default(default)
114: type_cast(default)
115: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 49
49: def has_default?
50: !default.nil?
51: end
Returns the human name of the column name.
Column.new('sales_stage', ...).human_name # => 'Sales stage'
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 109
109: def human_name
110: Base.human_attribute_name(@name)
111: end
Returns the Ruby class that corresponds to the abstract data type.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 54
54: def klass
55: case type
56: when :integer then Fixnum
57: when :float then Float
58: when :decimal then BigDecimal
59: when :datetime then Time
60: when :date then Date
61: when :timestamp then Time
62: when :time then Time
63: when :text, :string then String
64: when :binary then String
65: when :boolean then Object
66: end
67: end
Returns true if the column is either of type integer, float or decimal.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 45
45: def number?
46: type == :integer || type == :float || type == :decimal
47: end
Returns true if the column is either of type string or text.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 40
40: def text?
41: type == :string || type == :text
42: end
Casts value (which is a String) to an appropriate instance.
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 70
70: def type_cast(value)
71: return nil if value.nil?
72: case type
73: when :string then value
74: when :text then value
75: when :integer then value.to_i rescue value ? 1 : 0
76: when :float then value.to_f
77: when :decimal then self.class.value_to_decimal(value)
78: when :datetime then self.class.string_to_time(value)
79: when :timestamp then self.class.string_to_time(value)
80: when :time then self.class.string_to_dummy_time(value)
81: when :date then self.class.string_to_date(value)
82: when :binary then self.class.binary_to_string(value)
83: when :boolean then self.class.value_to_boolean(value)
84: else value
85: end
86: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 88
88: def type_cast_code(var_name)
89: case type
90: when :string then nil
91: when :text then nil
92: when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
93: when :float then "#{var_name}.to_f"
94: when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
95: when :datetime then "#{self.class.name}.string_to_time(#{var_name})"
96: when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
97: when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})"
98: when :date then "#{self.class.name}.string_to_date(#{var_name})"
99: when :binary then "#{self.class.name}.binary_to_string(#{var_name})"
100: when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
101: else nil
102: end
103: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 219
219: def extract_limit(sql_type)
220: $1.to_i if sql_type =~ /\((.*)\)/
221: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 223
223: def extract_precision(sql_type)
224: $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/
225: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 227
227: def extract_scale(sql_type)
228: case sql_type
229: when /^(numeric|decimal|number)\((\d+)\)/ then 0
230: when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/ then $4.to_i
231: end
232: end
# File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 234
234: def simplified_type(field_type)
235: case field_type
236: when /int/
237: :integer
238: when /float|double/
239: :float
240: when /decimal|numeric|number/
241: extract_scale(field_type) == 0 ? :integer : :decimal
242: when /datetime/
243: :datetime
244: when /timestamp/
245: :timestamp
246: when /time/
247: :time
248: when /date/
249: :date
250: when /clob/, /text/
251: :text
252: when /blob/, /binary/
253: :binary
254: when /char/, /string/
255: :string
256: when /boolean/
257: :boolean
258: end
259: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.