Module: Simple::SQL::Helpers::RowConverter

Defined in:
lib/simple/sql/helpers/row_converter.rb

Defined Under Namespace

Classes: ImmutableConverter, StructConverter, TypeConverter, TypeConverter2

Constant Summary collapse

SELF =
self

Class Method Summary collapse

Class Method Details

.convert(record, into:) ⇒ Object

:nodoc:



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

def self.convert(record, into:) # :nodoc:
  ary = convert_row([record], into: into)
  ary.first
end

.convert_row(records, into:, associations: nil, fq_table_name: nil) ⇒ Object

returns an array of converted records



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple/sql/helpers/row_converter.rb', line 7

def self.convert_row(records, into:, associations: nil, fq_table_name: nil)
  hsh = records.first
  return records unless hsh

  converter = if into == :struct
                StructConverter.for(attributes: hsh.keys, associations: associations)
              elsif into == :immutable
                ImmutableConverter.new(type: into, associations: associations)
              elsif into.respond_to?(:new_from_row)
                TypeConverter2.new(type: into, associations: associations, fq_table_name: fq_table_name)
              else
                TypeConverter.new(type: into, associations: associations)
              end

  records.map { |record| converter.convert_row(record) }
end