module Twobook::Handler::QueryHelpers
Mixin for Handler
with some query shorthand. Expects @accounts_in_process to be set when running a handler Expects @data_in_process to be set when looking up account requirements
Public Instance Methods
account_requirements()
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 74 def account_requirements labelled_account_requirements.values end
accounts(*_)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 78 def accounts(*_) {} end
existing(query)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 11 def existing(query) { requested: :existing, query: query.convert_to_name_query } end
labelled_account_requirements()
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 62 def labelled_account_requirements them = Utilities.match_params( method(:accounts), @data_in_process, "Cannot generate accounts for #{inspect} with data #{@data_in_process}" ) them.keys.map(&:to_s).each do |k| raise "Invalid account label #{k} for #{inspect} (must end in _account(s))" unless k =~ /_accounts?/ end them end
many(query)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 15 def many(query) { requested: :many, query: query } end
method_missing(account_label, *_)
click to toggle source
Calls superclass method
# File lib/twobook/handler/query_helpers.rb, line 52 def method_missing(account_label, *_) super unless account_label.to_s =~ /_accounts?$/ super if @event_in_process.blank? requirement = labelled_account_requirements.dig(account_label) super unless requirement satisfy_requirement(requirement) end
one(query)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 7 def one(query) { requested: :one, query: query.convert_to_name_query } end
respond_to_missing?(account_name, *_)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 48 def respond_to_missing?(account_name, *_) account_name.to_s =~ /_accounts?$/ end
satisfy_requirement(requested:, query:)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 23 def satisfy_requirement(requested:, query:) accounts = query.on(@accounts_in_process) case requested when :one existing = accounts.first return existing if existing.present? new = query.construct_account @accounts_in_process << new new when :existing it = accounts.first if it.nil? raise "Cannot process #{@event_in_process.inspect} with #{inspect}: " \ "no match for query #{query.inspect}). I have #{@accounts_in_process.join(', ')}" end it when :many Twobook.wrap_account_list!(accounts) else raise "Cannot satisfy requirement request #{requested}: not supported" end end
where(constraints)
click to toggle source
# File lib/twobook/handler/query_helpers.rb, line 19 def where(constraints) AccountQuery.where(constraints) end