Sha256: db014ba64c0af60e559a9a11e002e2d0513d60a5b4d6327daddf69e7d8c4b9a8

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

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

    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.5 lib/banking_data/query.rb
banking_data-0.9.4 lib/banking_data/query.rb
banking_data-0.9.3 lib/banking_data/query.rb
banking_data-0.9.2 lib/banking_data/query.rb
banking_data-0.9.1 lib/banking_data/query.rb