Class: SimpleService::Organizer

Inherits:
Object
  • Object
show all
Extended by:
ServiceBase::ClassMethods
Includes:
ServiceBase::InstanceMethods
Defined in:
lib/simple_service/organizer.rb

Direct Known Subclasses

EnsureOrganizerIsValid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServiceBase::ClassMethods

expects, get_expects, get_returns, returns, skip_validation

Methods included from ServiceBase::InstanceMethods

#all_context_keys, #define_getters_and_setters, #expects, #failed?, #failure!, #find_specified_return_keys, #organizer?, #return_context_with_success_status, #returns, #setup_call_chain, #skip_validation, #successful?, #symbolize_context_keys

Constructor Details

#initialize(context = {}) ⇒ Organizer

Returns a new instance of Organizer.



9
10
11
12
13
14
15
# File 'lib/simple_service/organizer.rb', line 9

def initialize(context = {})
  @context = context

  symbolize_context_keys
  setup_call_chain
  define_getters_and_setters
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/simple_service/organizer.rb', line 7

def context
  @context
end

Class Method Details

.call(context = {}) ⇒ Object

allow execution of the service from the class level for those that prefer that style



55
56
57
# File 'lib/simple_service/organizer.rb', line 55

def self.call(context = {})
  self.new(context).call
end

.commands(*args) ⇒ Object



17
18
19
# File 'lib/simple_service/organizer.rb', line 17

def self.commands(*args)
  @commands = args
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simple_service/organizer.rb', line 25

def call
  # underscores used to disambiguate local vars from methods with the same name
  with_validation do |_commands|
    _commands.each do |command|

      # halt further command calls if success has been set to false
      # in a previously called command
      break if context[:success] == false

      # if command class defines "expects" then only feed the command
      # those keys, otherwise just give it the entire context
      _context = if command.get_expects.any?
        {}.tap do |c|
          command.get_expects.each {|key| c[key] = context[key] }
        end
      else
        context
      end

      # instantiate and call the command
      new_context = command.new(_context).call

      # update the master context with the results of the command
      @context.merge!(new_context)
    end
  end
end

#commandsObject



21
22
23
# File 'lib/simple_service/organizer.rb', line 21

def commands
  self.class.instance_variable_get('@commands')
end