Sha256: c2ecb92e014b65eebcba9eb0a04c8878041c8d3b922630b167fe8482a25d57c6

Contents?: true

Size: 996 Bytes

Versions: 5

Compression:

Stored size: 996 Bytes

Contents

require 'active_support/core_ext/class/subclasses'
require 'active_support/core_ext/module/delegation'

module BankingData
  class Query

    delegate :each, :flatten, :map, :first, :last, to: :to_a
    attr_accessor :options, :attributes, :locale

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

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

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

    def to_a
      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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
banking_data-0.9.0 lib/banking_data/query.rb
banking_data-0.8.0 lib/banking_data/query.rb
banking_data-0.7.2 lib/banking_data/query.rb
banking_data-0.5.1 lib/banking_data/query.rb
banking_data-0.5.0 lib/banking_data/query.rb