Class: Simple::SQL::Helpers::RowConverter::TypeConverter

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(type:, associations:) ⇒ TypeConverter

Returns a new instance of TypeConverter.



28
29
30
31
# File 'lib/simple/sql/helpers/row_converter.rb', line 28

def initialize(type:, associations:)
  @type         = type
  @associations = associations
end

Class Method Details

.for(type:, associations:) ⇒ Object



24
25
26
# File 'lib/simple/sql/helpers/row_converter.rb', line 24

def self.for(type:, associations:)
  new(type: type, associations: associations)
end

Instance Method Details

#convert_associations(hsh) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple/sql/helpers/row_converter.rb', line 38

def convert_associations(hsh)
  updates = {}

  @associations.each do |key|
    value = hsh.fetch(key)
    case value
    when Hash   then updates[key] = SELF.convert(value, into: @type)
    when Array  then updates[key] = SELF.convert_row(value, into: @type)
    end
  end

  hsh.merge(updates)
end

#convert_row(hsh) ⇒ Object



33
34
35
36
# File 'lib/simple/sql/helpers/row_converter.rb', line 33

def convert_row(hsh)
  hsh = convert_associations(hsh) if @associations
  @type.new hsh
end