Class: WiseGopher::Column
- Inherits:
-
Object
- Object
- WiseGopher::Column
- Defined in:
- lib/wise_gopher/column.rb
Overview
Cast query columns and transform value
Instance Attribute Summary collapse
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #define_getter(row_class) ⇒ Object
-
#initialize(name, type_symbol, transform: nil, as: nil) ⇒ Column
constructor
A new instance of Column.
- #instance_variable_name ⇒ Object
Constructor Details
#initialize(name, type_symbol, transform: nil, as: nil) ⇒ Column
Returns a new instance of Column.
8 9 10 11 12 13 |
# File 'lib/wise_gopher/column.rb', line 8 def initialize(name, type_symbol, transform: nil, as: nil) @alias = as&.to_s.freeze || name.to_s.freeze @name = name.to_s.freeze @type = ActiveRecord::Type.lookup type_symbol @transform = transform&.to_proc end |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
6 7 8 |
# File 'lib/wise_gopher/column.rb', line 6 def alias @alias end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/wise_gopher/column.rb', line 6 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/wise_gopher/column.rb', line 6 def type @type end |
Instance Method Details
#cast(value) ⇒ Object
15 16 17 18 19 |
# File 'lib/wise_gopher/column.rb', line 15 def cast(value) casted_value = @type.deserialize(value) @transform ? transform_value(casted_value) : casted_value end |
#define_getter(row_class) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/wise_gopher/column.rb', line 21 def define_getter(row_class) column = self row_class.define_method(@alias) do instance_variable_get(column.instance_variable_name) end end |
#instance_variable_name ⇒ Object
29 30 31 |
# File 'lib/wise_gopher/column.rb', line 29 def instance_variable_name @instance_variable_name ||= "@#{@alias.tr("?!", "")}" end |