An abstract definition of a column in a table.
- binary_to_string
- extract_default
- fallback_string_to_date
- fallback_string_to_time
- fast_string_to_date
- fast_string_to_time
- has_default?
- human_name
- klass
- microseconds
- new
- new_date
- new_time
- 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
TRUE_VALUES | = | [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set |
FALSE_VALUES | = | [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set |
[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 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 121 121: def binary_to_string(value) 122: value 123: 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.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 27 27: def initialize(name, default, sql_type = nil, null = true) 28: @name, @sql_type, @null = name, sql_type, null 29: @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type) 30: @type = simplified_type(sql_type) 31: @default = extract_default(default) 32: 33: @primary = nil 34: end
Used to convert from Strings to BLOBs
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 116 116: def string_to_binary(value) 117: value 118: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 125 125: def string_to_date(string) 126: return string unless string.is_a?(String) 127: return nil if string.empty? 128: 129: fast_string_to_date(string) || fallback_string_to_date(string) 130: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 139 139: def string_to_dummy_time(string) 140: return string unless string.is_a?(String) 141: return nil if string.empty? 142: 143: string_to_time "2000-01-01 #{string}" 144: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 132 132: def string_to_time(string) 133: return string unless string.is_a?(String) 134: return nil if string.empty? 135: 136: fast_string_to_time(string) || fallback_string_to_time(string) 137: end
convert something to a boolean
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 147 147: def value_to_boolean(value) 148: if value.is_a?(String) && value.blank? 149: nil 150: else 151: TRUE_VALUES.include?(value) 152: end 153: end
convert something to a BigDecimal
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 156 156: def value_to_decimal(value) 157: # Using .class is faster than .is_a? and 158: # subclasses of BigDecimal will be handled 159: # in the else clause 160: if value.class == BigDecimal 161: value 162: elsif value.respond_to?(:to_d) 163: value.to_d 164: else 165: value.to_s.to_d 166: end 167: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 203 203: def fallback_string_to_date(string) 204: new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday)) 205: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 207 207: def fallback_string_to_time(string) 208: time_hash = Date._parse(string) 209: time_hash[:sec_fraction] = microseconds(time_hash) 210: 211: new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) 212: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 189 189: def fast_string_to_date(string) 190: if string =~ Format::ISO_DATE 191: new_date $1.to_i, $2.to_i, $3.to_i 192: end 193: end
Doesn‘t handle time zones.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 196 196: def fast_string_to_time(string) 197: if string =~ Format::ISO_DATETIME 198: microsec = ($7.to_f * 1_000_000).to_i 199: new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec 200: end 201: end
‘0.123456’ -> 123456 ‘1.123456’ -> 123456
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 172 172: def microseconds(time) 173: ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i 174: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 176 176: def new_date(year, mon, mday) 177: if year && year != 0 178: Date.new(year, mon, mday) rescue nil 179: end 180: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 182 182: def new_time(year, mon, mday, hour, min, sec, microsec) 183: # Treat 0000-00-00 00:00:00 as nil. 184: return nil if year.nil? || year == 0 185: 186: Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil 187: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 110 110: def extract_default(default) 111: type_cast(default) 112: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 46 46: def has_default? 47: !default.nil? 48: end
Returns the human name of the column name.
Examples
Column.new('sales_stage', ...).human_name # => 'Sales stage'
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 106 106: def human_name 107: Base.human_attribute_name(@name) 108: end
Returns the Ruby class that corresponds to the abstract data type.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 51 51: def klass 52: case type 53: when :integer then Fixnum 54: when :float then Float 55: when :decimal then BigDecimal 56: when :datetime then Time 57: when :date then Date 58: when :timestamp then Time 59: when :time then Time 60: when :text, :string then String 61: when :binary then String 62: when :boolean then Object 63: end 64: end
Returns true if the column is either of type integer, float or decimal.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 42 42: def number? 43: type == :integer || type == :float || type == :decimal 44: end
Returns true if the column is either of type string or text.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 37 37: def text? 38: type == :string || type == :text 39: end
Casts value (which is a String) to an appropriate instance.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 67 67: def type_cast(value) 68: return nil if value.nil? 69: case type 70: when :string then value 71: when :text then value 72: when :integer then value.to_i rescue value ? 1 : 0 73: when :float then value.to_f 74: when :decimal then self.class.value_to_decimal(value) 75: when :datetime then self.class.string_to_time(value) 76: when :timestamp then self.class.string_to_time(value) 77: when :time then self.class.string_to_dummy_time(value) 78: when :date then self.class.string_to_date(value) 79: when :binary then self.class.binary_to_string(value) 80: when :boolean then self.class.value_to_boolean(value) 81: else value 82: end 83: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 85 85: def type_cast_code(var_name) 86: case type 87: when :string then nil 88: when :text then nil 89: when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 90: when :float then "#{var_name}.to_f" 91: when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})" 92: when :datetime then "#{self.class.name}.string_to_time(#{var_name})" 93: when :timestamp then "#{self.class.name}.string_to_time(#{var_name})" 94: when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})" 95: when :date then "#{self.class.name}.string_to_date(#{var_name})" 96: when :binary then "#{self.class.name}.binary_to_string(#{var_name})" 97: when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" 98: else nil 99: end 100: end