Class: FunctionalLightService::Context::KeyVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/functional-light-service/context/key_verifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, action) ⇒ KeyVerifier

Returns a new instance of KeyVerifier.



8
9
10
11
# File 'lib/functional-light-service/context/key_verifier.rb', line 8

def initialize(context, action)
  @context = context
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/functional-light-service/context/key_verifier.rb', line 6

def action
  @action
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/functional-light-service/context/key_verifier.rb', line 6

def context
  @context
end

Class Method Details

.verify_keys(context, action, &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/functional-light-service/context/key_verifier.rb', line 48

def self.verify_keys(context, action, &block)
  ReservedKeysVerifier.new(context, action).verify
  ExpectedKeyVerifier.new(context, action).verify

  block.call

  PromisedKeyVerifier.new(context, action).verify
end

Instance Method Details

#are_all_keys_in_context?(keys) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/functional-light-service/context/key_verifier.rb', line 13

def are_all_keys_in_context?(keys)
  not_found_keys = keys_not_found(keys)
  not_found_keys.none?
end

#error_messageObject



28
29
30
31
# File 'lib/functional-light-service/context/key_verifier.rb', line 28

def error_message
  "#{type_name} #{format_keys(keys_not_found(keys))} " \
    "to be in the context during #{action}"
end

#format_keys(keys) ⇒ Object



24
25
26
# File 'lib/functional-light-service/context/key_verifier.rb', line 24

def format_keys(keys)
  keys.map { |k| ":#{k}" }.join(', ')
end

#keys_not_found(keys) ⇒ Object



18
19
20
21
22
# File 'lib/functional-light-service/context/key_verifier.rb', line 18

def keys_not_found(keys)
  keys ||= context.keys
  # context.key? risolve anche gli alias
  keys.reject { |key| context.key?(key) }
end

#throw_error_predicate(_keys) ⇒ Object

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/functional-light-service/context/key_verifier.rb', line 33

def throw_error_predicate(_keys)
  raise NotImplementedError, 'Sorry, you have to override length'
end

#verifyObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/functional-light-service/context/key_verifier.rb', line 37

def verify
  return context if context.failure?

  if throw_error_predicate(keys)
    Configuration.logger.error error_message
    raise error_to_throw, error_message
  end

  context
end