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.
default | [R] | |
limit | [R] | |
name | [R] | |
null | [R] | |
precision | [R] | |
primary | [RW] | |
scale | [R] | |
sql_type | [R] | |
type | [R] |
Used to convert from BLOBs to Strings
# 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, 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.
# 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
# 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
# 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
# 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
# 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
# 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
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 146 146: def value_to_decimal(value) 147: # Using .class is faster than .is_a? and 148: # subclasses of BigDecimal will be handled 149: # in the else clause 150: if value.class == BigDecimal 151: value 152: elsif value.respond_to?(:to_d) 153: value.to_d 154: else 155: value.to_s.to_d 156: end 157: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 193 193: def fallback_string_to_date(string) 194: new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday)) 195: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 197 197: def fallback_string_to_time(string) 198: time_hash = Date._parse(string) 199: time_hash[:sec_fraction] = microseconds(time_hash) 200: 201: new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) 202: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 179 179: def fast_string_to_date(string) 180: if string =~ Format::ISO_DATE 181: new_date $1.to_i, $2.to_i, $3.to_i 182: end 183: end
Doesn‘t handle time zones.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 186 186: def fast_string_to_time(string) 187: if string =~ Format::ISO_DATETIME 188: microsec = ($7.to_f * 1_000_000).to_i 189: new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec 190: end 191: end
‘0.123456’ -> 123456 ‘1.123456’ -> 123456
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 162 162: def microseconds(time) 163: ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i 164: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 166 166: def new_date(year, mon, mday) 167: if year && year != 0 168: Date.new(year, mon, mday) rescue nil 169: end 170: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 172 172: def new_time(year, mon, mday, hour, min, sec, microsec) 173: # Treat 0000-00-00 00:00:00 as nil. 174: return nil if year.nil? || year == 0 175: 176: Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil 177: end
# 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.
Column.new('sales_stage', ...).human_name # => 'Sales stage'
# 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.
# 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
# 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
# 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.
# 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
# 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