Class: Simple::SQL::Helpers::RowConverter::StructConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/sql/helpers/row_converter.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ StructConverter

Returns a new instance of StructConverter.



53
54
55
# File 'lib/simple/sql/helpers/row_converter.rb', line 53

def initialize(attributes)
  @klass = Struct.new(*attributes)
end

Class Method Details

.for(attributes:) ⇒ Object



48
49
50
51
# File 'lib/simple/sql/helpers/row_converter.rb', line 48

def self.for(attributes:)
  @cache ||= {}
  @cache[attributes] ||= new(attributes)
end

Instance Method Details

#convert_ary(hsh) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simple/sql/helpers/row_converter.rb', line 57

def convert_ary(hsh)
  values = hsh.values_at(*@klass.members)
  updates = {}

  values.each_with_index do |value, idx|
    case value
    when Hash   then updates[idx] = SELF.convert(value, into: :struct)
    when Array  then updates[idx] = SELF.convert_ary(value, into: :struct)
    end
  end

  updates.each do |idx, updated_value|
    values[idx] = updated_value
  end

  @klass.new(*values)
end