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
ColumnWithIdentity, FirebirdColumn, MysqlColumn, OpenBaseColumn, SQLiteColumn
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.
-
#primary ⇒ Object
Returns the value of attribute primary.
-
#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.
Instance Method Summary collapse
-
#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
- #text? ⇒ Boolean
-
#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, as in supplier_id
int(11). default
is the type-casted default value, such as sales_stage varchar(20) default 'new'
. sql_type
is only used to extract the column’s length, if necessary. For example, company_name varchar(60)
. null
determines if this column allows NULL
values.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 16 def initialize(name, default, sql_type = nil, null = true) @name, @type, @null = name, simplified_type(sql_type), null @sql_type = sql_type # have to do this one separately because type_cast depends on #type @default = type_cast(default) @limit = extract_limit(sql_type) unless sql_type.nil? @primary = nil @text = [:string, :text].include? @type @number = [:float, :integer].include? @type end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def default @default end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def name @name end |
#null ⇒ Object (readonly)
Returns the value of attribute null.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def null @null end |
#primary ⇒ Object
Returns the value of attribute primary.
8 9 10 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 8 def primary @primary end |
#sql_type ⇒ Object (readonly)
Returns the value of attribute sql_type.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def sql_type @sql_type end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 7 def type @type end |
Class Method Details
.binary_to_string(value) ⇒ Object
Used to convert from BLOBs to Strings
98 99 100 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 98 def self.binary_to_string(value) value end |
.string_to_binary(value) ⇒ Object
Used to convert from Strings to BLOBs
93 94 95 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 93 def self.string_to_binary(value) value end |
.string_to_date(string) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 102 def self.string_to_date(string) return string unless string.is_a?(String) date_array = ParseDate.parsedate(string) # treat 0000-00-00 as nil Date.new(date_array[0], date_array[1], date_array[2]) rescue nil end |
.string_to_dummy_time(string) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 116 def self.string_to_dummy_time(string) return string unless string.is_a?(String) time_array = ParseDate.parsedate(string) # pad the resulting array with dummy date information time_array[0] = 2000; time_array[1] = 1; time_array[2] = 1; Time.send(Base.default_timezone, *time_array) rescue nil end |
.string_to_time(string) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 109 def self.string_to_time(string) return string unless string.is_a?(String) time_array = ParseDate.parsedate(string)[0..5] # treat 0000-00-00 00:00:00 as nil Time.send(Base.default_timezone, *time_array) rescue nil end |
.value_to_boolean(value) ⇒ Object
convert something to a boolean
125 126 127 128 129 130 131 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 125 def self.value_to_boolean(value) return value if value==true || value==false case value.to_s.downcase when "true", "t", "1" then true else false end end |
Instance Method Details
#human_name ⇒ Object
Returns the human name of the column name.
Examples
Column.new('sales_stage', ...).human_name #=> 'Sales stage'
88 89 90 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 88 def human_name Base.human_attribute_name(@name) end |
#klass ⇒ Object
Returns the Ruby class that corresponds to the abstract data type.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 36 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 |
#number? ⇒ Boolean
31 32 33 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 31 def number? @number end |
#text? ⇒ Boolean
27 28 29 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 27 def text? @text end |
#type_cast(value) ⇒ Object
Casts value (which is a String) to an appropriate instance.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 51 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 :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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 68 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 :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 |