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, resolve_inputs, skip_validation!, skip_validation?, validator, validators, validators?

Methods included from InjectDsl

inject, injections, resolve_dependencies

Methods included from Application::AppContainer

#app_container, included, #qualify_container_key

Constructor Details

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

Returns a new instance of Base.



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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Class Method Details

.create_response(data) ⇒ Object



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

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

.error(*args) ⇒ Object



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

def error(*args)
  response_handler.error(*args)
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



48
49
50
# File 'lib/appfuel/handler/base.rb', line 48

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

.response?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#build_criteria(domain, options) ⇒ Object



86
87
88
# File 'lib/appfuel/handler/base.rb', line 86

def build_criteria(domain, options)

end

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



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

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

#error(*args) ⇒ Object



76
77
78
# File 'lib/appfuel/handler/base.rb', line 76

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

#ok(value = nil) ⇒ Object



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

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

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/appfuel/handler/base.rb', line 90

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



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

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