Class: AnyQuery::Adapters::Csv Private

Inherits:
Base
  • Object
show all
Defined in:
lib/any_query/adapters/csv.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Config

Instance Method Summary collapse

Methods inherited from Base

#fallback_where, #group_join_data, #initialize, #instantiate_model, #load_single, #map_multi_threaded, #parse_field_type, #parse_field_type_boolean, #parse_field_type_date, #parse_field_type_datetime, #parse_field_type_decimal, #parse_field_type_float, #parse_field_type_integer, #parse_field_type_string, #resolve_join, #resolve_path, #resolve_select, #run_external_join

Constructor Details

This class inherits a constructor from AnyQuery::Adapters::Base

Instance Method Details

#load(model, select:, joins:, where:, limit:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/any_query/adapters/csv.rb', line 32

def load(model, select:, joins:, where:, limit:)
  chain = parse_fields(model)
  chain = fallback_where(chain, where) if where.present?
  chain = chain.first(limit) if limit.present?
  chain = resolve_joins(chain, joins) if joins.present?

  chain.map! { |row| instantiate_model(model, row) }
  chain = resolve_select(chain, select) if select.present?

  chain
end

#parse_field(field, line) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
57
58
# File 'lib/any_query/adapters/csv.rb', line 51

def parse_field(field, line)
  result = parse_field_type(field, line)
  if field[:transform]
    field[:transform].call(result)
  else
    result
  end
end

#parse_fields(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
26
# File 'lib/any_query/adapters/csv.rb', line 18

def parse_fields(model)
  CSV.foreach(url, headers: true).map do |line|
    result = {}
    model.fields.each do |name, field|
      result[name] = parse_field(field, line[field[:source] || name.to_s])
    end
    result
  end
end

#resolve_joins(data, joins) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
# File 'lib/any_query/adapters/csv.rb', line 44

def resolve_joins(data, joins)
  joins.map do |join|
    resolve_join(data, join)
  end
  data
end

#urlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/any_query/adapters/csv.rb', line 28

def url
  @config[:url]
end