Class: ActiveRecord::ConnectionAdapters::Column

### Home Classes Methods

### In Files

- lib/active_record/connection_adapters/abstract/schema_definitions.rb

### Parent

Object

### Namespace

- MODULE ActiveRecord::ConnectionAdapters::Column::Format

### Methods

- ::binary_to_string
- ::fallback_string_to_date
- ::fallback_string_to_time
- ::fast_string_to_date
- ::fast_string_to_time
- ::microseconds
- ::new
- ::new_date
- ::new_time
- ::string_to_binary
- ::string_to_date
- ::string_to_dummy_time
- ::string_to_time
- ::value_to_boolean
- ::value_to_decimal
- #extract_default
- #has_default?
- #human_name
- #klass
- #number?
- #text?
- #type_cast
- #type_cast_code

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- ActiveRecord
- ActiveRecord::ActiveRecordError
- ActiveRecord::AdapterNotFound
- ActiveRecord::AdapterNotSpecified
- ActiveRecord::Aggregations
- ActiveRecord::Aggregations::ClassMethods
- ActiveRecord::AssociationPreload
- ActiveRecord::AssociationPreload::ClassMethods
- ActiveRecord::AssociationTypeMismatch
- ActiveRecord::Associations
- ActiveRecord::Associations::ClassMethods
- ActiveRecord::Associations::HasOneThroughAssociation
- ActiveRecord::Associations::ThroughAssociationScope
- ActiveRecord::AttributeAssignmentError
- ActiveRecord::AttributeMethods
- ActiveRecord::AttributeMethods::BeforeTypeCast
- ActiveRecord::AttributeMethods::ClassMethods
- ActiveRecord::AttributeMethods::Dirty
- ActiveRecord::AttributeMethods::PrimaryKey
- ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods
- ActiveRecord::AttributeMethods::Query
- ActiveRecord::AttributeMethods::Read
- ActiveRecord::AttributeMethods::Read::ClassMethods
- ActiveRecord::AttributeMethods::TimeZoneConversion
- ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods
- ActiveRecord::AttributeMethods::Write
- ActiveRecord::AttributeMethods::Write::ClassMethods
- ActiveRecord::AutosaveAssociation
- ActiveRecord::AutosaveAssociation::ClassMethods
- ActiveRecord::Base
- ActiveRecord::Calculations
- ActiveRecord::Callbacks
- ActiveRecord::Callbacks::ClassMethods
- ActiveRecord::ConfigurationError
- ActiveRecord::ConnectionAdapters
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- ActiveRecord::ConnectionAdapters::Column
- ActiveRecord::ConnectionAdapters::Column::Format
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- ActiveRecord::ConnectionAdapters::ConnectionManagement
- ActiveRecord::ConnectionAdapters::ConnectionPool
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::DatabaseStatements
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Quoting
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::Version
- ActiveRecord::ConnectionAdapters::SchemaStatements
- ActiveRecord::ConnectionAdapters::Table
- ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionNotEstablished
- ActiveRecord::ConnectionTimeoutError
- ActiveRecord::CounterCache
- ActiveRecord::DangerousAttributeError
- ActiveRecord::DynamicFinderMatch
- ActiveRecord::DynamicScopeMatch
- ActiveRecord::FinderMethods
- ActiveRecord::Generators
- ActiveRecord::Generators::Migration
- ActiveRecord::Generators::MigrationGenerator
- ActiveRecord::Generators::ModelGenerator
- ActiveRecord::Generators::ObserverGenerator
- ActiveRecord::Generators::SessionMigrationGenerator
- ActiveRecord::InvalidForeignKey
- ActiveRecord::IrreversibleMigration
- ActiveRecord::Locking
- ActiveRecord::Locking::Optimistic
- ActiveRecord::Locking::Optimistic::ClassMethods
- ActiveRecord::Locking::Pessimistic
- ActiveRecord::LogSubscriber
- ActiveRecord::Migration
- ActiveRecord::MigrationProxy
- ActiveRecord::MultiparameterAssignmentErrors
- ActiveRecord::NamedScope
- ActiveRecord::NamedScope::ClassMethods
- ActiveRecord::NestedAttributes
- ActiveRecord::NestedAttributes::ClassMethods
- ActiveRecord::NestedAttributes::TooManyRecords
- ActiveRecord::Observer
- ActiveRecord::Persistence
- ActiveRecord::PredicateBuilder
- ActiveRecord::PreparedStatementInvalid
- ActiveRecord::QueryCache
- ActiveRecord::QueryCache::ClassMethods
- ActiveRecord::QueryMethods
- ActiveRecord::Railtie
- ActiveRecord::Railties
- ActiveRecord::Railties::ControllerRuntime
- ActiveRecord::Railties::ControllerRuntime::ClassMethods
- ActiveRecord::ReadOnlyRecord
- ActiveRecord::RecordInvalid
- ActiveRecord::RecordNotFound
- ActiveRecord::RecordNotSaved
- ActiveRecord::RecordNotUnique
- ActiveRecord::Reflection
- ActiveRecord::Reflection::ClassMethods
- ActiveRecord::Reflection::MacroReflection
- ActiveRecord::Relation
- ActiveRecord::Rollback
- ActiveRecord::Schema
- ActiveRecord::Serialization
- ActiveRecord::SerializationTypeMismatch
- ActiveRecord::SessionStore
- ActiveRecord::SessionStore::Session
- ActiveRecord::SessionStore::SqlBypass
- ActiveRecord::SpawnMethods
- ActiveRecord::StaleObjectError
- ActiveRecord::StatementInvalid
- ActiveRecord::TestFixtures
- ActiveRecord::TestFixtures::ClassMethods
- ActiveRecord::ThrowResult
- ActiveRecord::Timestamp
- ActiveRecord::Transactions
- ActiveRecord::Transactions::ClassMethods
- ActiveRecord::UnknownAttributeError
- ActiveRecord::Validations
- ActiveRecord::Validations::AssociatedValidator
- ActiveRecord::Validations::ClassMethods
- ActiveRecord::Validations::UniquenessValidator
- ActiveRecord::WrappedDatabaseException
- <
- Fixtures
- FixturesFileNotFound

No matching classes.

# ActiveRecord::ConnectionAdapters::Column

An abstract definition of a column in a table.

### Constants

FALSE_VALUES
TRUE_VALUES

### Attributes

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

### Public Class Methods

binary_to_string(value) click to toggle source

Used to convert from BLOBs to Strings

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 124
    def binary_to_string(value)
      value
    end

new(name, default, sql_type = nil, null = true) click to toggle source

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 used to extract the column's length, if necessary. For example `60` in `company_name varchar(60)`. It will be mapped to one of the standard Rails SQL types in the `type` attribute. `null` determines if this column allows `NULL` values.

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 30
    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 = extract_default(default)

      @primary = nil
    end

string_to_binary(value) click to toggle source

Used to convert from Strings to BLOBs

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

string_to_date(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 128
    def string_to_date(string)
      return string unless string.is_a?(String)
      return nil if string.empty?

      fast_string_to_date(string) || fallback_string_to_date(string)
    end

string_to_dummy_time(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 142
    def string_to_dummy_time(string)
      return string unless string.is_a?(String)
      return nil if string.empty?

      string_to_time "2000-01-01 #{string}"
    end

string_to_time(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 135
    def string_to_time(string)
      return string unless string.is_a?(String)
      return nil if string.empty?

      fast_string_to_time(string) || fallback_string_to_time(string)
    end

value_to_boolean(value) click to toggle source

convert something to a boolean

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 150
    def value_to_boolean(value)
      if value.is_a?(String) && value.blank?
        nil
      else
        TRUE_VALUES.include?(value)
      end
    end

value_to_decimal(value) click to toggle source

convert something to a BigDecimal

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 159
    def value_to_decimal(value)
      # Using .class is faster than .is_a? and
      # subclasses of BigDecimal will be handled
      # in the else clause
      if value.class == BigDecimal
        value
      elsif value.respond_to?(:to_d)
        value.to_d
      else
        value.to_s.to_d
      end
    end

### Protected Class Methods

fallback_string_to_date(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 206
    def fallback_string_to_date(string)
      new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
    end

fallback_string_to_time(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 210
    def fallback_string_to_time(string)
      time_hash = Date._parse(string)
      time_hash[:sec_fraction] = microseconds(time_hash)

      new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
    end

fast_string_to_date(string) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 192
    def fast_string_to_date(string)
      if string =~ Format::ISO_DATE
        new_date $1.to_i, $2.to_i, $3.to_i
      end
    end

fast_string_to_time(string) click to toggle source

Doesn’t handle time zones.

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 199
    def fast_string_to_time(string)
      if string =~ Format::ISO_DATETIME
        microsec = ($7.to_f * 1_000_000).to_i
        new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
      end
    end

microseconds(time) click to toggle source

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

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

new_date(year, mon, mday) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 179
    def new_date(year, mon, mday)
      if year && year != 0
        Date.new(year, mon, mday) rescue nil
      end
    end

new_time(year, mon, mday, hour, min, sec, microsec) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 185
    def new_time(year, mon, mday, hour, min, sec, microsec)
      # Treat 0000-00-00 00:00:00 as nil.
      return nil if year.nil? || year == 0

      Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
    end

### Public Instance Methods

extract_default(default) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 113
    def extract_default(default)
      type_cast(default)
    end

has_default?() click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 49
    def has_default?
      !default.nil?
    end

human_name() click to toggle source

Returns the human name of the column name.

##### Examples

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

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

klass() click to toggle source

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

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 54
    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

number?() click to toggle source

Returns `true` if the column is either of type integer, float or decimal.

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 45
    def number?
      type == :integer || type == :float || type == :decimal
    end

text?() click to toggle source

Returns `true` if the column is either of type string or text.

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 40
    def text?
      type == :string || type == :text
    end

type_cast(value) click to toggle source

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

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 70
    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

type_cast_code(var_name) click to toggle source

    # File lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 88
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.