Class: Hydramata::Core::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/hydramata/core/runner.rb

Overview

A Runner is responsible for performing the guts of what might traditionally be a controller’s action.

It exists for two reasons:

  • Controllers are extremely over-worked (parameter negotiation, authentication, authorization, marshalling an object, user messages via flash, and http response determination)

  • The inner method content of a controller’s action is subject to change especially as other implementors look to change the behavior.

So, the Runner provides a seem in the code in which you can more readily make changes to the “inner method” of a route. In some ways, I see this as a separation of state change and response; a somewhat analogous separation to the Command/Query separation principle.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, collaborators = {}) {|@callbacks| ... } ⇒ Runner

Returns a new instance of Runner.

Yields:

  • (@callbacks)


26
27
28
29
30
# File 'lib/hydramata/core/runner.rb', line 26

def initialize(context, collaborators = {})
  @callbacks = collaborators.fetch(:callbacks) { default_callbacks }
  self.context = context
  yield(@callbacks) if block_given?
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



20
21
22
# File 'lib/hydramata/core/runner.rb', line 20

def context
  @context
end

Class Method Details

.run(context, *args, &block) ⇒ Object



22
23
24
# File 'lib/hydramata/core/runner.rb', line 22

def self.run(context,*args,&block)
  new(context, &block).run(*args)
end

Instance Method Details

#callback(name, *args) ⇒ Object



32
33
34
35
# File 'lib/hydramata/core/runner.rb', line 32

def callback(name, *args)
  @callbacks.call(name, *args)
  args
end

#run(*args) ⇒ Object

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/hydramata/core/runner.rb', line 41

def run(*args)
  raise NotImplementedError.new("You must define #{self.class}#run")
end

#servicesObject



37
38
39
# File 'lib/hydramata/core/runner.rb', line 37

def services
  context.services
end