An abstract definition of a column in a table.
- binary_to_string
- human_name
- klass
- new
- number?
- string_to_binary
- string_to_date
- string_to_dummy_time
- string_to_time
- text?
- type_cast
- type_cast_code
- value_to_boolean
- value_to_decimal
| [R] | default | |
| [R] | limit | |
| [R] | name | |
| [R] | null | |
| [R] | precision | |
| [RW] | primary | |
| [R] | scale | |
| [R] | sql_type | |
| [R] | type |
Used to convert from BLOBs to Strings
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 101
101: def self.binary_to_string(value)
102: value
103: 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.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 18
18: def initialize(name, default, sql_type = nil, null = true)
19: @name, @sql_type, @null = name, sql_type, null
20: @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
21: @type = simplified_type(sql_type)
22: @default = type_cast(default)
23:
24: @primary = nil
25: end
Used to convert from Strings to BLOBs
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 96
96: def self.string_to_binary(value)
97: value
98: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 105
105: def self.string_to_date(string)
106: return string unless string.is_a?(String)
107: date_array = ParseDate.parsedate(string)
108: # treat 0000-00-00 as nil
109: Date.new(date_array[0], date_array[1], date_array[2]) rescue nil
110: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 121
121: def self.string_to_dummy_time(string)
122: return string unless string.is_a?(String)
123: return nil if string.empty?
124: time_hash = Date._parse(string)
125: time_hash[:sec_fraction] = microseconds(time_hash)
126: # pad the resulting array with dummy date information
127: time_array = [2000, 1, 1]
128: time_array += time_hash.values_at(:hour, :min, :sec, :sec_fraction)
129: Time.send(Base.default_timezone, *time_array) rescue nil
130: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 112
112: def self.string_to_time(string)
113: return string unless string.is_a?(String)
114: time_hash = Date._parse(string)
115: time_hash[:sec_fraction] = microseconds(time_hash)
116: time_array = time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)
117: # treat 0000-00-00 00:00:00 as nil
118: Time.send(Base.default_timezone, *time_array) rescue DateTime.new(*time_array[0..5]) rescue nil
119: end
convert something to a boolean
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 133
133: def self.value_to_boolean(value)
134: if value == true || value == false
135: value
136: else
137: %w(true t 1).include?(value.to_s.downcase)
138: end
139: end
convert something to a BigDecimal
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 142
142: def self.value_to_decimal(value)
143: if value.is_a?(BigDecimal)
144: value
145: elsif value.respond_to?(:to_d)
146: value.to_d
147: else
148: value.to_s.to_d
149: end
150: end
Returns the human name of the column name.
Examples
Column.new('sales_stage', ...).human_name #=> 'Sales stage'
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 91
91: def human_name
92: Base.human_attribute_name(@name)
93: end
Returns the Ruby class that corresponds to the abstract data type.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 36
36: def klass
37: case type
38: when :integer then Fixnum
39: when :float then Float
40: when :decimal then BigDecimal
41: when :datetime then Time
42: when :date then Date
43: when :timestamp then Time
44: when :time then Time
45: when :text, :string then String
46: when :binary then String
47: when :boolean then Object
48: end
49: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 31
31: def number?
32: [:float, :integer, :decimal].include? type
33: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 27
27: def text?
28: [:string, :text].include? type
29: end
Casts value (which is a String) to an appropriate instance.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 52
52: def type_cast(value)
53: return nil if value.nil?
54: case type
55: when :string then value
56: when :text then value
57: when :integer then value.to_i rescue value ? 1 : 0
58: when :float then value.to_f
59: when :decimal then self.class.value_to_decimal(value)
60: when :datetime then self.class.string_to_time(value)
61: when :timestamp then self.class.string_to_time(value)
62: when :time then self.class.string_to_dummy_time(value)
63: when :date then self.class.string_to_date(value)
64: when :binary then self.class.binary_to_string(value)
65: when :boolean then self.class.value_to_boolean(value)
66: else value
67: end
68: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 70
70: def type_cast_code(var_name)
71: case type
72: when :string then nil
73: when :text then nil
74: when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
75: when :float then "#{var_name}.to_f"
76: when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
77: when :datetime then "#{self.class.name}.string_to_time(#{var_name})"
78: when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
79: when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})"
80: when :date then "#{self.class.name}.string_to_date(#{var_name})"
81: when :binary then "#{self.class.name}.binary_to_string(#{var_name})"
82: when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
83: else nil
84: end
85: end