Class: ActiveRecord::ConnectionAdapters::Column
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::Column
- Defined in:
- lib/active_record/connection_adapters/abstract/schema_definitions.rb
Overview
An abstract definition of a column in a table.
Direct Known Subclasses
Defined Under Namespace
Modules: Format
Constant Summary collapse
- 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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#null ⇒ Object
readonly
Returns the value of attribute null.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#primary ⇒ Object
Returns the value of attribute primary.
-
#scale ⇒ Object
readonly
Returns the value of attribute scale.
-
#sql_type ⇒ Object
readonly
Returns the value of attribute sql_type.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.binary_to_string(value) ⇒ Object
Used to convert from BLOBs to Strings.
-
.string_to_binary(value) ⇒ Object
Used to convert from Strings to BLOBs.
- .string_to_date(string) ⇒ Object
- .string_to_dummy_time(string) ⇒ Object
- .string_to_time(string) ⇒ Object
-
.value_to_boolean(value) ⇒ Object
convert something to a boolean.
-
.value_to_decimal(value) ⇒ Object
convert something to a BigDecimal.
Instance Method Summary collapse
- #extract_default(default) ⇒ Object
- #has_default? ⇒ Boolean
-
#human_name ⇒ Object
Returns the human name of the column name.
-
#initialize(name, default, sql_type = nil, null = true) ⇒ Column
constructor
Instantiates a new column in the table.
-
#klass ⇒ Object
Returns the Ruby class that corresponds to the abstract data type.
-
#number? ⇒ Boolean
Returns
true
if the column is either of type integer, float or decimal. -
#text? ⇒ Boolean
Returns
true
if the column is either of type string or text. -
#type_cast(value) ⇒ Object
Casts value (which is a String) to an appropriate instance.
- #type_cast_code(var_name) ⇒ Object
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 only used to extract the column’s length, if necessary. For example 60
in company_name varchar(60)
. null
determines if this column allows NULL
values.
27 28 29 30 31 32 33 34 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 27 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 |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def default @default end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def name @name end |
#null ⇒ Object (readonly)
Returns the value of attribute null.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def null @null end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def precision @precision end |
#primary ⇒ Object
Returns the value of attribute primary.
19 20 21 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 19 def primary @primary end |
#scale ⇒ Object (readonly)
Returns the value of attribute scale.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def scale @scale end |
#sql_type ⇒ Object (readonly)
Returns the value of attribute sql_type.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def sql_type @sql_type end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 18 def type @type end |
Class Method Details
.binary_to_string(value) ⇒ Object
Used to convert from BLOBs to Strings
121 122 123 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 121 def binary_to_string(value) value end |
.string_to_binary(value) ⇒ Object
Used to convert from Strings to BLOBs
116 117 118 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 116 def string_to_binary(value) value end |
.string_to_date(string) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 125 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) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 139 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) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 132 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
147 148 149 150 151 152 153 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 147 def value_to_boolean(value) if value.is_a?(String) && value.blank? nil else TRUE_VALUES.include?(value) end end |
.value_to_decimal(value) ⇒ Object
convert something to a BigDecimal
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 156 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
#extract_default(default) ⇒ Object
110 111 112 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 110 def extract_default(default) type_cast(default) end |
#has_default? ⇒ Boolean
46 47 48 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 46 def has_default? !default.nil? end |
#human_name ⇒ Object
Returns the human name of the column name.
Examples
Column.new('sales_stage', ...).human_name # => 'Sales stage'
106 107 108 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 106 def human_name Base.human_attribute_name(@name) end |
#klass ⇒ Object
Returns the Ruby class that corresponds to the abstract data type.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 51 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? ⇒ Boolean
Returns true
if the column is either of type integer, float or decimal.
42 43 44 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 42 def number? type == :integer || type == :float || type == :decimal end |
#text? ⇒ Boolean
Returns true
if the column is either of type string or text.
37 38 39 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 37 def text? type == :string || type == :text end |
#type_cast(value) ⇒ Object
Casts value (which is a String) to an appropriate instance.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 67 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) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 85 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 |