Class: Twobook::AccountQuery::WhereQuery

Inherits:
Twobook::AccountQuery show all
Defined in:
lib/twobook/account_query.rb

Constant Summary collapse

ATTRIBUTE_CONSTRAINTS =
%i(ledger name)
CLASS_CONSTRAINTS =
%i(category account_type)

Instance Method Summary collapse

Methods inherited from Twobook::AccountQuery

#after, #and, named, #named, none, #none, #or, where, #where

Constructor Details

#initialize(child_query, original_constraints) ⇒ WhereQuery

Returns a new instance of WhereQuery.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

Instance Method Details

#categoryObject



104
105
106
# File 'lib/twobook/account_query.rb', line 104

def category
  @class_constraints[:category]
end

#construct_accountObject



121
122
123
124
125
# File 'lib/twobook/account_query.rb', line 121

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



112
113
114
115
116
117
118
119
# File 'lib/twobook/account_query.rb', line 112

def convert_to_name_query
  if @attribute_constraints[:name].present?
    NameQuery.new(@attribute_constraints[:name])
  else
     = 
    NameQuery.new(.name, account: )
  end
end

#dataObject



108
109
110
# File 'lib/twobook/account_query.rb', line 108

def data
  @data_constraints
end

#inspectObject



127
128
129
130
131
132
# 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?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/twobook/account_query.rb', line 87

def none?
  @child_query.none?
end

#on(array) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twobook/account_query.rb', line 91

def on(array)
  @child_query.on(array).select do ||
    next unless matches_attributes()
    next unless matches_class()
    next unless matches_data()
    next unless matches_balance()

    next if @tags_constraint && !.class.tagged?(@tags_constraint)

    true
  end
end