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_optional, get_returns, optional, returns, skip_validation

Methods included from ServiceBase::InstanceMethods

#all_context_keys, #define_getters_and_setters, #expects, #failed?, #failure!, #find_specified_return_keys, #halted?, #optional, #organizer?, #return_context_with_success_status, #returns, #setup_call_chain, #skip_validation, #success!, #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 = validate_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



60
61
62
# File 'lib/simple_service/organizer.rb', line 60

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
52
53
54
55
56
# 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 or halt is set
      break if context[:success] == false || context[:halt] == true

      # 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

      # also merge any optional keys
      command.get_optional.each do |key|
        _context[key] = context[key]
      end

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

      # update the master context with the results of the command
      @context.merge!(resulting_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