Class: Simple::SQL::Helpers::RowConverter::StructConverter
- Inherits:
-
Object
- Object
- Simple::SQL::Helpers::RowConverter::StructConverter
- Defined in:
- lib/simple/sql/helpers/row_converter.rb
Overview
:nodoc:
Class Method Summary collapse
Instance Method Summary collapse
-
#convert_associations(values) ⇒ Object
convert values at the
@association_indices
. - #convert_row(hsh) ⇒ Object
-
#initialize(attributes:, associations:) ⇒ StructConverter
constructor
A new instance of StructConverter.
Constructor Details
#initialize(attributes:, associations:) ⇒ StructConverter
Returns a new instance of StructConverter.
85 86 87 88 89 90 91 |
# File 'lib/simple/sql/helpers/row_converter.rb', line 85 def initialize(attributes:, associations:) @attributes = attributes @associations = associations @association_indices = associations.map { |association| attributes.index(association) } if associations @klass = Struct.new(*attributes) end |
Class Method Details
.for(attributes:, associations:) ⇒ Object
80 81 82 83 |
# File 'lib/simple/sql/helpers/row_converter.rb', line 80 def self.for(attributes:, associations:) @cache ||= {} @cache[[attributes, associations]] ||= new(attributes: attributes, associations: associations) end |
Instance Method Details
#convert_associations(values) ⇒ Object
convert values at the @association_indices
.
101 102 103 104 105 106 107 108 109 |
# File 'lib/simple/sql/helpers/row_converter.rb', line 101 def convert_associations(values) @association_indices.each do |idx| value = values[idx] case value when Hash then values[idx] = SELF.convert(value, into: :struct) when Array then values[idx] = SELF.convert_row(value, into: :struct) end end end |
#convert_row(hsh) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/simple/sql/helpers/row_converter.rb', line 93 def convert_row(hsh) values = hsh.values_at(*@attributes) convert_associations(values) if @associations @klass.new(*values) end |