Class: SqlDatasource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rotating_es_loader/sql_datasource.rb

Overview

:nodoc

Instance Method Summary collapse

Constructor Details

#initialize(sql:, ar_connection:) ⇒ SqlDatasource

Returns a new instance of SqlDatasource.



9
10
11
12
13
# File 'lib/rotating_es_loader/sql_datasource.rb', line 9

def initialize(sql:, ar_connection:)
  @sql = sql
  @ar_connection = ar_connection
  raise unless @sql
end

Instance Method Details

#dataObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rotating_es_loader/sql_datasource.rb', line 19

def data
  queries = @sql.is_a?(String) ? [@sql] : @sql

  queries.flat_map do |query|
    records_array = @ar_connection.execute(@sql)
    fields = records_array.fields.map(&:to_sym)

    records_array.map do |row_array|
      normalize(fields.zip(row_array).to_h)
    end
  end
end

#each(&block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/rotating_es_loader/sql_datasource.rb', line 32

def each(&block)
  return to_enum(:each) unless block

  data.each(&block)

  self
end

#normalize(o) ⇒ Object



15
16
17
# File 'lib/rotating_es_loader/sql_datasource.rb', line 15

def normalize(o)
  o
end