Class: AnyQuery::Adapters::FixedLength Private

Inherits:
Base
  • Object
show all
Defined in:
lib/any_query/adapters/fixed_length.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, #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

#initialize(config) ⇒ FixedLength

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.

Returns a new instance of FixedLength.



18
19
20
21
# File 'lib/any_query/adapters/fixed_length.rb', line 18

def initialize(config)
  super(config)
  @file = File.open(url)
end

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/any_query/adapters/fixed_length.rb', line 40

def load(model, select:, joins:, where:, limit:)
  @file.rewind

  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.



61
62
63
64
65
66
67
68
# File 'lib/any_query/adapters/fixed_length.rb', line 61

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.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/any_query/adapters/fixed_length.rb', line 23

def parse_fields(model)
  @file.each_line.map do |line|
    result = {}
    last_index = 0
    model.fields.each do |name, field|
      raw_value = line[last_index...(last_index + field[:length])]
      result[name] = parse_field(field, raw_value)
      last_index += field[:length]
    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.



54
55
56
57
58
59
# File 'lib/any_query/adapters/fixed_length.rb', line 54

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.



36
37
38
# File 'lib/any_query/adapters/fixed_length.rb', line 36

def url
  @config[:url]
end