Class: BankingData::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/banking_data/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, only = nil) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
# File 'lib/banking_data/query.rb', line 10

def initialize(options, only = nil)
  @locale = options.delete(:locale)
  @options = options
  @attributes = only
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/banking_data/query.rb', line 8

def attributes
  @attributes
end

#localeObject

Returns the value of attribute locale.



8
9
10
# File 'lib/banking_data/query.rb', line 8

def locale
  @locale
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/banking_data/query.rb', line 8

def options
  @options
end

Instance Method Details

#bankObject



41
42
43
# File 'lib/banking_data/query.rb', line 41

def bank
  Bank.subclasses.find { |bank_class| bank_class::LOCALE == locale }
end

#only(*attrs) ⇒ Object



23
24
25
26
27
# File 'lib/banking_data/query.rb', line 23

def only(*attrs)
  clone.tap do |query|
    query.attributes = attrs
  end
end

#to_aObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/banking_data/query.rb', line 29

def to_a
  return [] if bank.nil?

  data = bank.all
    .select { |bank| @options.map { |k, v| bank.send(k) == v }.all? }
  if @attributes
    data.map { |bank| @attributes.map { |attr| bank.send(attr) } }
  else
    data
  end
end

#where(opts = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/banking_data/query.rb', line 16

def where(opts = {})
  clone.tap do |query|
    query.locale = opts.delete(:locale)
    query.options = @options.merge(opts)
  end
end