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] | |
primary | [RW] | |
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 98 98: def self.binary_to_string(value) 99: value 100: 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.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 16 16: def initialize(name, default, sql_type = nil, null = true) 17: @name, @type, @null = name, simplified_type(sql_type), null 18: @sql_type = sql_type 19: # have to do this one separately because type_cast depends on #type 20: @default = type_cast(default) 21: @limit = extract_limit(sql_type) unless sql_type.nil? 22: @primary = nil 23: @text = [:string, :text].include? @type 24: @number = [:float, :integer].include? @type 25: end
Used to convert from Strings to BLOBs
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 93 93: def self.string_to_binary(value) 94: value 95: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 102 102: def self.string_to_date(string) 103: return string unless string.is_a?(String) 104: date_array = ParseDate.parsedate(string) 105: # treat 0000-00-00 as nil 106: Date.new(date_array[0], date_array[1], date_array[2]) rescue nil 107: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 116 116: def self.string_to_dummy_time(string) 117: return string unless string.is_a?(String) 118: time_array = ParseDate.parsedate(string) 119: # pad the resulting array with dummy date information 120: time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; 121: Time.send(Base.default_timezone, *time_array) rescue nil 122: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 109 109: def self.string_to_time(string) 110: return string unless string.is_a?(String) 111: time_array = ParseDate.parsedate(string)[0..5] 112: # treat 0000-00-00 00:00:00 as nil 113: Time.send(Base.default_timezone, *time_array) rescue nil 114: end
convert something to a boolean
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 125 125: def self.value_to_boolean(value) 126: return value if value==true || value==false 127: case value.to_s.downcase 128: when "true", "t", "1" then true 129: else false 130: end 131: 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 88 88: def human_name 89: Base.human_attribute_name(@name) 90: 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 36 36: def klass 37: case type 38: when :integer then Fixnum 39: when :float then Float 40: when :datetime then Time 41: when :date then Date 42: when :timestamp then Time 43: when :time then Time 44: when :text, :string then String 45: when :binary then String 46: when :boolean then Object 47: end 48: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 31 31: def number? 32: @number 33: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 27 27: def text? 28: @text 29: 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 51 51: def type_cast(value) 52: return nil if value.nil? 53: case type 54: when :string then value 55: when :text then value 56: when :integer then value.to_i rescue value ? 1 : 0 57: when :float then value.to_f 58: when :datetime then self.class.string_to_time(value) 59: when :timestamp then self.class.string_to_time(value) 60: when :time then self.class.string_to_dummy_time(value) 61: when :date then self.class.string_to_date(value) 62: when :binary then self.class.binary_to_string(value) 63: when :boolean then self.class.value_to_boolean(value) 64: else value 65: end 66: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 68 68: def type_cast_code(var_name) 69: case type 70: when :string then nil 71: when :text then nil 72: when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 73: when :float then "#{var_name}.to_f" 74: when :datetime then "#{self.class.name}.string_to_time(#{var_name})" 75: when :timestamp then "#{self.class.name}.string_to_time(#{var_name})" 76: when :time then "#{self.class.name}.string_to_dummy_time(#{var_name})" 77: when :date then "#{self.class.name}.string_to_date(#{var_name})" 78: when :binary then "#{self.class.name}.binary_to_string(#{var_name})" 79: when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" 80: else nil 81: end 82: end