Class: Redpear::Schema::Column
- Inherits:
-
String
- Object
- String
- Redpear::Schema::Column
- Defined in:
- lib/redpear/schema/column.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#encode_value(value) ⇒ Object
Encodes a value, for storage.
-
#initialize(model, name, type = nil) ⇒ Column
constructor
Creates a new column.
-
#name ⇒ String
The column name.
-
#readable? ⇒ Boolean
True if the column is readable.
-
#type_cast(value, type = self.type) ⇒ Object
Casts a value to a type.
-
#writable? ⇒ Boolean
True if the column is writable.
Constructor Details
#initialize(model, name, type = nil) ⇒ Column
Creates a new column.
11 12 13 14 15 |
# File 'lib/redpear/schema/column.rb', line 11 def initialize(model, name, type = nil) super name.to_s @model = model @type = type.to_sym if type end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
2 3 4 |
# File 'lib/redpear/schema/column.rb', line 2 def model @model end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
2 3 4 |
# File 'lib/redpear/schema/column.rb', line 2 def type @type end |
Instance Method Details
#encode_value(value) ⇒ Object
Encodes a value, for storage
40 41 42 43 44 45 46 47 |
# File 'lib/redpear/schema/column.rb', line 40 def encode_value(value) case value when Time value.to_i else value end end |
#name ⇒ String
Returns the column name.
50 51 52 |
# File 'lib/redpear/schema/column.rb', line 50 def name to_s end |
#readable? ⇒ Boolean
Returns true if the column is readable.
55 56 57 |
# File 'lib/redpear/schema/column.rb', line 55 def readable? true end |
#type_cast(value, type = self.type) ⇒ Object
Casts a value to a type
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/redpear/schema/column.rb', line 22 def type_cast(value, type = self.type) case type when :counter type_cast(value, :integer).to_i when :integer Kernel::Integer(value) rescue nil if value when :float Kernel::Float(value) rescue nil if value when :timestamp value = type_cast(value, :integer) Time.at(value) if value else value end end |
#writable? ⇒ Boolean
Returns true if the column is writable.
60 61 62 |
# File 'lib/redpear/schema/column.rb', line 60 def writable? true end |