Module: Twobook::Handler::QueryHelpers

Included in:
Twobook::Handler
Defined in:
lib/twobook/handler/query_helpers.rb

Overview

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(account_label, *_) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/twobook/handler/query_helpers.rb', line 52

def method_missing(, *_)
  super unless .to_s =~ /_accounts?$/
  super if @event_in_process.blank?

  requirement = .dig()
  super unless requirement

  satisfy_requirement(requirement)
end

Instance Method Details

#account_requirementsObject



74
75
76
# File 'lib/twobook/handler/query_helpers.rb', line 74

def 
  .values
end

#accounts(*_) ⇒ Object



78
79
80
# File 'lib/twobook/handler/query_helpers.rb', line 78

def accounts(*_)
  {}
end

#existing(query) ⇒ Object



11
12
13
# File 'lib/twobook/handler/query_helpers.rb', line 11

def existing(query)
  { requested: :existing, query: query.convert_to_name_query }
end

#labelled_account_requirementsObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twobook/handler/query_helpers.rb', line 62

def 
  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) ⇒ Object



15
16
17
# File 'lib/twobook/handler/query_helpers.rb', line 15

def many(query)
  { requested: :many, query: query }
end

#one(query) ⇒ Object



7
8
9
# 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, *_) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/twobook/handler/query_helpers.rb', line 48

def respond_to_missing?(, *_)
  .to_s =~ /_accounts?$/
end

#satisfy_requirement(requested:, query:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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.
    @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.(accounts)
  else
    raise "Cannot satisfy requirement request #{requested}: not supported"
  end
end

#where(constraints) ⇒ Object



19
20
21
# File 'lib/twobook/handler/query_helpers.rb', line 19

def where(constraints)
  AccountQuery.where(constraints)
end