Class: ActiveRecord::ConnectionAdapters::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/abstract_adapter.rb

Overview

:nodoc:

Direct Known Subclasses

ColumnWithIdentity, SQLiteColumn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default, sql_type = nil) ⇒ Column

The name should contain the name of the column, such as “name” in “name varchar(250)” The default should contain the type-casted default of the column, such as 1 in “count int(11) DEFAULT 1” The type parameter should either contain :integer, :float, :datetime, :date, :text, or :string The sql_type is just used for extracting the limit, such as 10 in “varchar(10)”



154
155
156
157
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 154

def initialize(name, default, sql_type = nil)
  @name, @default, @type = name, type_cast(default), simplified_type(sql_type)
  @limit = extract_limit(sql_type) unless sql_type.nil?
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



149
150
151
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 149

def default
  @default
end

#limitObject (readonly)

Returns the value of attribute limit.



149
150
151
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 149

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



149
150
151
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 149

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



149
150
151
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 149

def type
  @type
end

Instance Method Details

#binary_to_string(value) ⇒ Object



198
199
200
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 198

def binary_to_string(value)
  value
end

#human_nameObject



190
191
192
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 190

def human_name
  Base.human_attribute_name(@name)
end

#klassObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 159

def klass
  case type
    when :integer       then Fixnum
    when :float         then Float
    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

#string_to_binary(value) ⇒ Object



194
195
196
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 194

def string_to_binary(value)
  value
end

#type_cast(value) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 173

def type_cast(value)
  if value.nil? then return nil end
  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 :datetime  then string_to_time(value)
    when :timestamp then string_to_time(value)
    when :time      then string_to_dummy_time(value)
    when :date      then string_to_date(value)
    when :binary    then binary_to_string(value)
    when :boolean   then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1'
    else value
  end
end