Class: Appfuel::Handler::Base

Inherits:
Object
  • Object
show all
Extended by:
InjectDsl, ValidatorDsl
Includes:
Application::AppContainer
Defined in:
lib/appfuel/handler/base.rb

Direct Known Subclasses

Action, Command

Constant Summary

Constants included from InjectDsl

InjectDsl::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValidatorDsl

default_validator_name, load_validator, load_validators, resolve_inputs, skip_validation!, skip_validation?, validator, validator_keys, validators, validators?

Methods included from InjectDsl

inject, injections, resolve_dependencies

Methods included from Application::AppContainer

#app_container, #feature_name, included, #qualify_container_key

Constructor Details

#initialize(container = Dry::Container.new) ⇒ Base

Returns a new instance of Base.



80
81
82
# File 'lib/appfuel/handler/base.rb', line 80

def initialize(container = Dry::Container.new)
  @data = container
end

Instance Attribute Details

#dataObject (readonly)

Instance methods



78
79
80
# File 'lib/appfuel/handler/base.rb', line 78

def data
  @data
end

Class Method Details

.create_handler_failure(response) ⇒ Object



71
72
73
74
# File 'lib/appfuel/handler/base.rb', line 71

def create_handler_failure(response)
  title = "#{container_class_path} Failed:"
  HandlerFailure.new(title, response)
end

.create_response(data) ⇒ Object



63
64
65
# File 'lib/appfuel/handler/base.rb', line 63

def create_response(data)
  response_handler.create_response(data)
end

.error(*args) ⇒ Object



51
52
53
# File 'lib/appfuel/handler/base.rb', line 51

def error(*args)
  response_handler.error(*args)
end

.fail_handler!(response) ⇒ Object



67
68
69
# File 'lib/appfuel/handler/base.rb', line 67

def fail_handler!(response)
  raise create_handler_failure(response)
end

.inherited(klass) ⇒ Object

Register the extending class with the application container

Parameters:

  • klass (Class)

    the handler class that is inheriting this

Returns:

  • nil



16
17
18
# File 'lib/appfuel/handler/base.rb', line 16

def inherited(klass)
  stage_class_for_registration(klass)
end

.ok(value = nil) ⇒ Object



55
56
57
# File 'lib/appfuel/handler/base.rb', line 55

def ok(value = nil)
  response_handler.ok(value)
end

.response?(value) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/appfuel/handler/base.rb', line 59

def response?(value)
  response_handler.response?(value)
end

.response_handlerObject



20
21
22
# File 'lib/appfuel/handler/base.rb', line 20

def response_handler
  @response_handler ||= ResponseHandler.new
end

.run(inputs = {}, container = Dry::Container.new) ⇒ Response

Run will validate all inputs; returning on input failures, resolving declared dependencies, then delegate to the handlers call method with its valid inputs and resolved dependencies. Finally it ensure every response is a Response object.

Parameters:

  • inputs (Hash) (defaults to: {})

    inputs to be validated

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/appfuel/handler/base.rb', line 31

def run(inputs = {}, container = Dry::Container.new)
  response = resolve_inputs(inputs)
  return response if response.failure?
  valid_inputs = response.ok

  resolve_dependencies(container)
  handler = self.new(container)
  result = handler.call(valid_inputs)
  result = create_response(result) unless response?(result)

  result
end

.run!(inputs = {}, container = Dry::Container.new) ⇒ Object



44
45
46
47
48
49
# File 'lib/appfuel/handler/base.rb', line 44

def run!(inputs = {}, container = Dry::Container.new)
  result = run(inputs, container)
  fail_handler!(result) if result.failure?

  result.ok
end

Instance Method Details

#build_criteria(domain, options) ⇒ Object



102
103
104
# File 'lib/appfuel/handler/base.rb', line 102

def build_criteria(domain, options)

end

#call(inputs, data = {}) ⇒ Object



84
85
86
# File 'lib/appfuel/handler/base.rb', line 84

def call(inputs, data = {})
  fail "Concrete handlers must implement their own call"
end

#error(*args) ⇒ Object



92
93
94
# File 'lib/appfuel/handler/base.rb', line 92

def error(*args)
  self.class.error(*args)
end

#ok(value = nil) ⇒ Object



88
89
90
# File 'lib/appfuel/handler/base.rb', line 88

def ok(value = nil)
  self.class.ok(value)
end

#present(name, data, inputs = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/appfuel/handler/base.rb', line 106

def present(name, data, inputs = {})
  return data if inputs[:raw] == true

  key = qualify_container_key(name, 'presenters')
  container = self.class.app_container
  unless container.key?(key)
    unless data.respond_to?(:to_h)
      fail "data must implement :to_h for generic presentation"
    end

    return data.to_h
  end

  container[key].call(data, inputs)
end

#search(repo, domain, options = {}) ⇒ Object



96
97
98
99
100
# File 'lib/appfuel/handler/base.rb', line 96

def search(repo, domain, options = {})
  criteria = build_criteria(domain, options)
  repo = data[repo]
  repo.search(criteria)
end