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)”



160
161
162
163
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 160

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.



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

def default
  @default
end

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#binary_to_string(value) ⇒ Object



204
205
206
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 204

def binary_to_string(value)
  value
end

#human_nameObject



196
197
198
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 196

def human_name
  Base.human_attribute_name(@name)
end

#klassObject



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 165

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



200
201
202
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 200

def string_to_binary(value)
  value
end

#type_cast(value) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 179

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