Class: ActiveRecord::ConnectionAdapters::Column

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

Overview

An abstract definition of a column in a table.

Defined Under Namespace

Modules: Format

Constant Summary collapse

TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/connection_adapters/column.rb', line 29

def initialize(name, default, sql_type = nil, null = true)
  @name             = name
  @sql_type         = sql_type
  @null             = null
  @limit            = extract_limit(sql_type)
  @precision        = extract_precision(sql_type)
  @scale            = extract_scale(sql_type)
  @type             = simplified_type(sql_type)
  @default          = extract_default(default)
  @default_function = nil
  @primary          = nil
  @coder            = nil
end

Instance Attribute Details

#coderObject Also known as: encoded?

Returns the value of attribute coder.



17
18
19
# File 'lib/active_record/connection_adapters/column.rb', line 17

def coder
  @coder
end

#defaultObject (readonly)

Returns the value of attribute default.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def default
  @default
end

#default_functionObject (readonly)

Returns the value of attribute default_function.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def default_function
  @default_function
end

#limitObject (readonly)

Returns the value of attribute limit.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def null
  @null
end

#precisionObject (readonly)

Returns the value of attribute precision.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def precision
  @precision
end

#primaryObject

Returns the value of attribute primary.



17
18
19
# File 'lib/active_record/connection_adapters/column.rb', line 17

def primary
  @primary
end

#scaleObject (readonly)

Returns the value of attribute scale.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def scale
  @scale
end

#sql_typeObject (readonly)

Returns the value of attribute sql_type.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def sql_type
  @sql_type
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/active_record/connection_adapters/column.rb', line 16

def type
  @type
end

Class Method Details

.binary_to_string(value) ⇒ Object

Used to convert from BLOBs to Strings



159
160
161
# File 'lib/active_record/connection_adapters/column.rb', line 159

def binary_to_string(value)
  value
end

.string_to_binary(value) ⇒ Object

Used to convert from Strings to BLOBs



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

def string_to_binary(value)
  value
end

.string_to_dummy_time(string) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/active_record/connection_adapters/column.rb', line 181

def string_to_dummy_time(string)
  return string unless string.is_a?(String)
  return nil if string.empty?

  dummy_time_string = "2000-01-01 #{string}"

  fast_string_to_time(dummy_time_string) || begin
    time_hash = Date._parse(dummy_time_string)
    return nil if time_hash[:hour].nil?
    new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
  end
end

.string_to_time(string) ⇒ Object



174
175
176
177
178
179
# File 'lib/active_record/connection_adapters/column.rb', line 174

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) ⇒ Object

convert something to a boolean



195
196
197
198
199
200
201
# File 'lib/active_record/connection_adapters/column.rb', line 195

def value_to_boolean(value)
  if value.is_a?(String) && value.empty?
    nil
  else
    TRUE_VALUES.include?(value)
  end
end

.value_to_date(value) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/active_record/connection_adapters/column.rb', line 163

def value_to_date(value)
  if value.is_a?(String)
    return nil if value.empty?
    fast_string_to_date(value) || fallback_string_to_date(value)
  elsif value.respond_to?(:to_date)
    value.to_date
  else
    value
  end
end

.value_to_decimal(value) ⇒ Object

convert something to a BigDecimal



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/active_record/connection_adapters/column.rb', line 215

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

.value_to_integer(value) ⇒ Object

Used to convert values to integer. handle the case when an integer column is used to store boolean values



205
206
207
208
209
210
211
212
# File 'lib/active_record/connection_adapters/column.rb', line 205

def value_to_integer(value)
  case value
  when TrueClass, FalseClass
    value ? 1 : 0
  else
    value.to_i rescue nil
  end
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_record/connection_adapters/column.rb', line 70

def binary?
  type == :binary
end

#extract_default(default) ⇒ Object



143
144
145
# File 'lib/active_record/connection_adapters/column.rb', line 143

def extract_default(default)
  type_cast(default)
end

#has_default?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_record/connection_adapters/column.rb', line 53

def has_default?
  !default.nil?
end

#human_nameObject

Returns the human name of the column name.

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


139
140
141
# File 'lib/active_record/connection_adapters/column.rb', line 139

def human_name
  Base.human_attribute_name(@name)
end

#klassObject

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/connection_adapters/column.rb', line 58

def klass
  case type
  when :integer                     then Fixnum
  when :float                       then Float
  when :decimal                     then BigDecimal
  when :datetime, :timestamp, :time then Time
  when :date                        then Date
  when :text, :string, :binary      then String
  when :boolean                     then Object
  end
end

#number?Boolean

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_record/connection_adapters/column.rb', line 49

def number?
  type == :integer || type == :float || type == :decimal
end

#string_to_binary(value) ⇒ Object

Used to convert from Strings to BLOBs



148
149
150
# File 'lib/active_record/connection_adapters/column.rb', line 148

def string_to_binary(value)
  self.class.string_to_binary(value)
end

#text?Boolean

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/active_record/connection_adapters/column.rb', line 44

def text?
  type == :string || type == :text
end

#type_cast(value) ⇒ Object

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_record/connection_adapters/column.rb', line 91

def type_cast(value)
  return nil if value.nil?
  return coder.load(value) if encoded?

  klass = self.class

  case type
  when :string, :text        then value
  when :integer              then klass.value_to_integer(value)
  when :float                then value.to_f
  when :decimal              then klass.value_to_decimal(value)
  when :datetime, :timestamp then klass.string_to_time(value)
  when :time                 then klass.string_to_dummy_time(value)
  when :date                 then klass.value_to_date(value)
  when :binary               then klass.binary_to_string(value)
  when :boolean              then klass.value_to_boolean(value)
  else value
  end
end

#type_cast_code(var_name) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record/connection_adapters/column.rb', line 111

def type_cast_code(var_name)
  message = "Column#type_cast_code is deprecated in favor of using Column#type_cast only, " \
            "and it is going to be removed in future Rails versions."
  ActiveSupport::Deprecation.warn message

  klass = self.class.name

  case type
  when :string, :text        then var_name
  when :integer              then "#{klass}.value_to_integer(#{var_name})"
  when :float                then "#{var_name}.to_f"
  when :decimal              then "#{klass}.value_to_decimal(#{var_name})"
  when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
  when :time                 then "#{klass}.string_to_dummy_time(#{var_name})"
  when :date                 then "#{klass}.value_to_date(#{var_name})"
  when :binary               then "#{klass}.binary_to_string(#{var_name})"
  when :boolean              then "#{klass}.value_to_boolean(#{var_name})"
  when :hstore               then "#{klass}.string_to_hstore(#{var_name})"
  when :inet, :cidr          then "#{klass}.string_to_cidr(#{var_name})"
  when :json                 then "#{klass}.string_to_json(#{var_name})"
  else var_name
  end
end

#type_cast_for_write(value) ⇒ Object

Casts a Ruby value to something appropriate for writing to the database.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_record/connection_adapters/column.rb', line 75

def type_cast_for_write(value)
  return value unless number?

  case value
  when FalseClass
    0
  when TrueClass
    1
  when String
    value.presence
  else
    value
  end
end