Sha256: 917de207b466759b5dbc28ffbd95f05c83e5a0315394b2a9c667d4d01ff30433

Contents?: true

Size: 944 Bytes

Versions: 1

Compression:

Stored size: 944 Bytes

Contents

require 'active_support/core_ext/class/subclasses'

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

1 entries across 1 versions & 1 rubygems

Version Path
banking_data-0.4.0 lib/banking_data/query.rb