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

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

Overview

:nodoc:

Direct Known Subclasses

ImmutableConverter, TypeConverter2

Instance Method Summary collapse

Constructor Details

#initialize(type:, associations:) ⇒ TypeConverter

Returns a new instance of TypeConverter.



30
31
32
33
# File 'lib/simple/sql/helpers/row_converter.rb', line 30

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

Instance Method Details

#build_row_in_target_type(hsh) ⇒ Object



40
41
42
# File 'lib/simple/sql/helpers/row_converter.rb', line 40

def build_row_in_target_type(hsh)
  @type.new hsh
end

#convert_associations(hsh) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/simple/sql/helpers/row_converter.rb', line 44

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



35
36
37
38
# File 'lib/simple/sql/helpers/row_converter.rb', line 35

def convert_row(hsh)
  hsh = convert_associations(hsh) if @associations
  build_row_in_target_type hsh
end