Class: Twobook::AccountQuery::WhereQuery
Constant Summary
collapse
- ATTRIBUTE_CONSTRAINTS =
%i(ledger name)
- CLASS_CONSTRAINTS =
%i(category account_type)
Instance Method Summary
collapse
#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
@attribute_constraints = constraints.(*ATTRIBUTE_CONSTRAINTS)
@class_constraints = constraints.(*CLASS_CONSTRAINTS)
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
#category ⇒ Object
104
105
106
|
# File 'lib/twobook/account_query.rb', line 104
def category
@class_constraints[:category]
end
|
#construct_account ⇒ Object
121
122
123
124
125
|
# 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 ⇒ Object
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
account = construct_account
NameQuery.new(account.name, account: construct_account)
end
end
|
#data ⇒ Object
108
109
110
|
# File 'lib/twobook/account_query.rb', line 108
def data
@data_constraints
end
|
#inspect ⇒ Object
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
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 |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
|