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.



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

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

Instance Method Details

#build_row_in_target_type(hsh) ⇒ Object



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

def build_row_in_target_type(hsh)
  @type.new hsh
end

#convert_associations(hsh) ⇒ Object



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

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



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

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