class Twobook::AccountQuery::WhereQuery
Constants
- ATTRIBUTE_CONSTRAINTS
- CLASS_CONSTRAINTS
Public Class Methods
new(child_query, original_constraints)
click to toggle source
# File lib/twobook/account_query.rb, line 64 def initialize(child_query, original_constraints) constraints = original_constraints.deep_dup # Slice up the constraints into types @attribute_constraints = constraints.extract!(*ATTRIBUTE_CONSTRAINTS) @class_constraints = constraints.extract!(*CLASS_CONSTRAINTS) # Support category shorthand and check it exists # e.g. "sme/liabilities/payout" instead of Accounting::Accounts::Sme::Liabilities::Payout.name category = @attribute_constraints[:category] if category.present? unless category_name.in?(Twobook::Account.types.map { |t| t.class.category }) raise "Invalid category: #{category}" end end @balance_constraint = constraints.delete(:balance) @tags_constraint = constraints.delete(:tags) @data_constraints = constraints @child_query = child_query end
Public Instance Methods
category()
click to toggle source
# File lib/twobook/account_query.rb, line 104 def category @class_constraints[:category] end
construct_account()
click to toggle source
# File lib/twobook/account_query.rb, line 121 def construct_account klass = Account.types.detect { |t| t.category == category } raise "Can't find matching class for category #{category}" unless klass.present? klass.new(balance: 0, **data) end
convert_to_name_query()
click to toggle source
# File lib/twobook/account_query.rb, line 112 def convert_to_name_query if @attribute_constraints[:name].present? NameQuery.new(@attribute_constraints[:name]) else account = construct_account NameQuery.new(account.name, account: construct_account) end end
data()
click to toggle source
# File lib/twobook/account_query.rb, line 108 def data @data_constraints end
inspect()
click to toggle source
# File lib/twobook/account_query.rb, line 127 def inspect "<#{self.class.name} @attribute_constraints=#{@attribute_constraints} " \ "@class_constraints=#{@class_constraints} " \ "@data_constraints=#{@data_constraints} " \ "@tags_constraint=#{@tags_constraint || 'nil'}>" end
none?()
click to toggle source
# File lib/twobook/account_query.rb, line 87 def none? @child_query.none? end
on(array)
click to toggle source
# File lib/twobook/account_query.rb, line 91 def on(array) @child_query.on(array).select do |account| next unless matches_attributes(account) next unless matches_class(account) next unless matches_data(account) next unless matches_balance(account) next if @tags_constraint && !account.class.tagged?(@tags_constraint) true end end
Private Instance Methods
matches_attributes(account)
click to toggle source
# File lib/twobook/account_query.rb, line 141 def matches_attributes(account) @attribute_constraints.none? { |k, v| account.public_send(k) != v } end
matches_balance(account)
click to toggle source
# File lib/twobook/account_query.rb, line 149 def matches_balance(account) return true if @balance_constraint.nil? return account.balance.positive? if @balance_constraint == :positive account.balance == @balance_constraint end
matches_class(account)
click to toggle source
# File lib/twobook/account_query.rb, line 145 def matches_class(account) @class_constraints.none? { |k, v| account.class.public_send(k) != v } end
matches_data(account)
click to toggle source
# File lib/twobook/account_query.rb, line 136 def matches_data(account) account_data = account.data.to_a @data_constraints.none? { |pair| !pair.in?(account_data) } end