Class: ActiveRecord::ConnectionAdapters::Column
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::Column
- Defined in:
- activerecord/lib/active_record/connection_adapters/column.rb
Overview
An abstract definition of a column in a table.
Defined Under Namespace
Modules: Format
Constant Summary
- TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
- FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
Instance Attribute Summary (collapse)
-
- (Object) coder
(also: #encoded?)
Returns the value of attribute coder.
-
- (Object) default
readonly
Returns the value of attribute default.
-
- (Object) limit
readonly
Returns the value of attribute limit.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) null
readonly
Returns the value of attribute null.
-
- (Object) precision
readonly
Returns the value of attribute precision.
-
- (Object) primary
Returns the value of attribute primary.
-
- (Object) scale
readonly
Returns the value of attribute scale.
-
- (Object) sql_type
readonly
Returns the value of attribute sql_type.
-
- (Object) type
readonly
Returns the value of attribute type.
Class Method Summary (collapse)
-
+ (Object) binary_to_string(value)
Used to convert from BLOBs to Strings.
-
+ (Object) string_to_binary(value)
Used to convert from Strings to BLOBs.
- + (Object) string_to_date(string)
- + (Object) string_to_dummy_time(string)
- + (Object) string_to_time(string)
-
+ (Object) value_to_boolean(value)
convert something to a boolean.
-
+ (Object) value_to_decimal(value)
convert something to a BigDecimal.
Instance Method Summary (collapse)
- - (Object) extract_default(default)
- - (Boolean) has_default?
-
- (Object) human_name
Returns the human name of the column name.
-
- (Column) initialize(name, default, sql_type = nil, null = true)
constructor
Instantiates a new column in the table.
-
- (Object) klass
Returns the Ruby class that corresponds to the abstract data type.
-
- (Boolean) number?
Returns true if the column is either of type integer, float or decimal.
-
- (Object) string_to_binary(value)
Used to convert from Strings to BLOBs.
-
- (Boolean) text?
Returns true if the column is either of type string or text.
-
- (Object) type_cast(value)
Casts value (which is a String) to an appropriate instance.
- - (Object) type_cast_code(var_name)
Constructor Details
- (Column) initialize(name, default, sql_type = nil, null = true)
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.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 27 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) @primary = nil @coder = nil end |
Instance Attribute Details
- (Object) coder Also known as: encoded?
Returns the value of attribute coder
15 16 17 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 15 def coder @coder end |
- (Object) default (readonly)
Returns the value of attribute default
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def default @default end |
- (Object) limit (readonly)
Returns the value of attribute limit
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def limit @limit end |
- (Object) name (readonly)
Returns the value of attribute name
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def name @name end |
- (Object) null (readonly)
Returns the value of attribute null
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def null @null end |
- (Object) precision (readonly)
Returns the value of attribute precision
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def precision @precision end |
- (Object) primary
Returns the value of attribute primary
15 16 17 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 15 def primary @primary end |
- (Object) scale (readonly)
Returns the value of attribute scale
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def scale @scale end |
- (Object) sql_type (readonly)
Returns the value of attribute sql_type
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def sql_type @sql_type end |
- (Object) type (readonly)
Returns the value of attribute type
14 15 16 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 14 def type @type end |
Class Method Details
+ (Object) binary_to_string(value)
Used to convert from BLOBs to Strings
129 130 131 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 129 def binary_to_string(value) value end |
+ (Object) string_to_binary(value)
Used to convert from Strings to BLOBs
124 125 126 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 124 def string_to_binary(value) value end |
+ (Object) string_to_date(string)
133 134 135 136 137 138 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 133 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 |
+ (Object) string_to_dummy_time(string)
147 148 149 150 151 152 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 147 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 |
+ (Object) string_to_time(string)
140 141 142 143 144 145 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 140 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 |
+ (Object) value_to_boolean(value)
convert something to a boolean
155 156 157 158 159 160 161 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 155 def value_to_boolean(value) if value.is_a?(String) && value.blank? nil else TRUE_VALUES.include?(value) end end |
+ (Object) value_to_decimal(value)
convert something to a BigDecimal
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 164 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 |
Instance Method Details
- (Object) extract_default(default)
113 114 115 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 113 def extract_default(default) type_cast(default) end |
- (Boolean) has_default?
50 51 52 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 50 def has_default? !default.nil? end |
- (Object) human_name
Returns the human name of the column name.
Examples
Column.new('sales_stage', ...).human_name # => 'Sales stage'
109 110 111 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 109 def human_name Base.human_attribute_name(@name) end |
- (Object) klass
Returns the Ruby class that corresponds to the abstract data type.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 55 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 |
- (Boolean) number?
Returns true if the column is either of type integer, float or decimal.
46 47 48 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 46 def number? type == :integer || type == :float || type == :decimal end |
- (Object) string_to_binary(value)
Used to convert from Strings to BLOBs
118 119 120 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 118 def string_to_binary(value) self.class.string_to_binary(value) end |
- (Boolean) text?
Returns true if the column is either of type string or text.
41 42 43 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 41 def text? type == :string || type == :text end |
- (Object) type_cast(value)
Casts value (which is a String) to an appropriate instance.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 68 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 value.to_i rescue value ? 1 : 0 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.string_to_date(value) when :binary then klass.binary_to_string(value) when :boolean then klass.value_to_boolean(value) else value end end |
- (Object) type_cast_code(var_name)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 88 def type_cast_code(var_name) klass = self.class.name case type when :string, :text then var_name when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 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}.string_to_date(#{var_name})" when :binary then "#{klass}.binary_to_string(#{var_name})" when :boolean then "#{klass}.value_to_boolean(#{var_name})" else var_name end end |