Module: SimpleService::ServiceBase::InstanceMethods

Included in:
Command, Organizer
Defined in:
lib/simple_service/service_base.rb

Instance Method Summary collapse

Instance Method Details

#expectsObject



50
51
52
# File 'lib/simple_service/service_base.rb', line 50

def expects
  self.class.instance_variable_get('@expects') || []
end

#find_specified_return_keysObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_service/service_base.rb', line 33

def find_specified_return_keys
  if returns.nil? || returns.empty?
    context
  else
    returns.inject({}) do |to_return, return_param|
      if context.has_key?(return_param)
        to_return[return_param] = context[return_param]
      else
        error_msg = "#{self.class} tried to return #{return_param}, but it did not exist in the context: #{context.inspect}"
        raise ReturnKeyError, error_msg
      end

      to_return
    end
  end
end

#returnsObject



54
55
56
# File 'lib/simple_service/service_base.rb', line 54

def returns
  self.class.instance_variable_get('@returns') || []
end

#setup_execute_chainObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_service/service_base.rb', line 16

def setup_execute_chain
  self.class.class_eval do

    # grab the method object and hold onto it here
    execute_method = instance_method(:execute)

    # redefine the execute method, call the existing execute method object,
    # and then run return key checking... allows user to implement execute in
    # their individual command classes without having to call super or any
    # other method to return only specific context keys
    define_method :execute do
      execute_method.bind(self).call
      find_specified_return_keys
    end
  end
end