Class ActiveRecord::ConnectionAdapters::Column
In: activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
Parent: Object

An abstract definition of a column in a table.

Methods

Attributes

default  [R] 
limit  [R] 
name  [R] 
null  [R] 
precision  [R] 
primary  [RW] 
scale  [R] 
sql_type  [R] 
type  [R] 

Public Class methods

Used to convert from BLOBs to Strings

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 101
      def self.binary_to_string(value)
        value
      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.

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 18
      def initialize(name, default, sql_type = nil, null = true)
        @name, @sql_type, @null = name, sql_type, null
        @limit, @precision, @scale  = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type) 
        @type = simplified_type(sql_type)
        @default = type_cast(default)

        @primary = nil
      end

Used to convert from Strings to BLOBs

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 96
      def self.string_to_binary(value)
        value
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 105
      def self.string_to_date(string)
        return string unless string.is_a?(String)
        date_array = ParseDate.parsedate(string)
        # treat 0000-00-00 as nil
        Date.new(date_array[0], date_array[1], date_array[2]) rescue nil
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 127
      def self.string_to_dummy_time(string)
        return string unless string.is_a?(String)
        return nil if string.empty?
        time_hash = Date._parse(string)
        time_hash[:sec_fraction] = microseconds(time_hash)
        # pad the resulting array with dummy date information
        time_array = [2000, 1, 1]
        time_array += time_hash.values_at(:hour, :min, :sec, :sec_fraction)
        Time.send(Base.default_timezone, *time_array) rescue nil
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 112
      def self.string_to_time(string)
        return string unless string.is_a?(String)
        time_hash = Date._parse(string)
        time_hash[:sec_fraction] = microseconds(time_hash)
        time_array = time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)
        # treat 0000-00-00 00:00:00 as nil
        begin
          Time.send(Base.default_timezone, *time_array)
        rescue
          zone_offset = if Base.default_timezone == :local then DateTime.now.offset else 0 end
          # Append zero calendar reform start to account for dates skipped by calendar reform
          DateTime.new(*time_array[0..5] << zone_offset << 0) rescue nil
        end
      end

convert something to a boolean

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 139
      def self.value_to_boolean(value)
        if value == true || value == false
          value
        else
          %w(true t 1).include?(value.to_s.downcase)
        end
      end

convert something to a BigDecimal

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 148
      def self.value_to_decimal(value)
        if value.is_a?(BigDecimal)
          value
        elsif value.respond_to?(:to_d)
          value.to_d
        else
          value.to_s.to_d
        end
      end

Private Class methods

‘0.123456’ -> 123456 ‘1.123456’ -> 123456

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 161
        def self.microseconds(time)
          ((time[:sec_fraction].to_f % 1) * 1_000_000).to_i
        end

Public Instance methods

Returns the human name of the column name.

Examples
 Column.new('sales_stage', ...).human_name #=> 'Sales stage'

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 91
      def human_name
        Base.human_attribute_name(@name)
      end

Returns the Ruby class that corresponds to the abstract data type.

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 36
      def klass
        case type
          when :integer       then Fixnum
          when :float         then Float
          when :decimal       then BigDecimal
          when :datetime      then Time
          when :date          then Date
          when :timestamp     then Time
          when :time          then Time
          when :text, :string then String
          when :binary        then String
          when :boolean       then Object
        end
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 31
      def number?
        [:float, :integer, :decimal].include? type
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 27
      def text?
        [:string, :text].include? type
      end

Casts value (which is a String) to an appropriate instance.

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 52
      def type_cast(value)
        return nil if value.nil?
        case type
          when :string    then value
          when :text      then value
          when :integer   then value.to_i rescue value ? 1 : 0
          when :float     then value.to_f
          when :decimal   then self.class.value_to_decimal(value)
          when :datetime  then self.class.string_to_time(value)
          when :timestamp then self.class.string_to_time(value)
          when :time      then self.class.string_to_dummy_time(value)
          when :date      then self.class.string_to_date(value)
          when :binary    then self.class.binary_to_string(value)
          when :boolean   then self.class.value_to_boolean(value)
          else value
        end
      end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 70
      def type_cast_code(var_name)
        case type
          when :string    then nil
          when :text      then nil
          when :integer   then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
          when :float     then "#{var_name}.to_f"
          when :decimal   then "#{self.class.name}.value_to_decimal(#{var_name})"
          when :datetime  then "#{self.class.name}.string_to_time(#{var_name})"
          when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"
          when :time      then "#{self.class.name}.string_to_dummy_time(#{var_name})"
          when :date      then "#{self.class.name}.string_to_date(#{var_name})"
          when :binary    then "#{self.class.name}.binary_to_string(#{var_name})"
          when :boolean   then "#{self.class.name}.value_to_boolean(#{var_name})"
          else nil
        end
      end

Private Instance methods

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 165
        def extract_limit(sql_type)
          $1.to_i if sql_type =~ /\((.*)\)/
        end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 169
        def extract_precision(sql_type)
          $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
        end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 173
        def extract_scale(sql_type)
          case sql_type
            when /^(numeric|decimal|number)\((\d+)\)/i then 0
            when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
          end
        end

[Source]

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 180
        def simplified_type(field_type)
          case field_type
            when /int/i
              :integer
            when /float|double/i
              :float
            when /decimal|numeric|number/i
              extract_scale(field_type) == 0 ? :integer : :decimal
            when /datetime/i
              :datetime
            when /timestamp/i
              :timestamp
            when /time/i
              :time
            when /date/i
              :date
            when /clob/i, /text/i
              :text
            when /blob/i, /binary/i
              :binary
            when /char/i, /string/i
              :string
            when /boolean/i
              :boolean
          end
        end

[Validate]