Module: SimpleService::ServiceBase::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#all_context_keysObject



83
84
85
# File 'lib/simple_service/service_base.rb', line 83

def all_context_keys
  (expects + returns + ['message', 'success']).uniq
end

#define_getters_and_settersObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/simple_service/service_base.rb', line 92

def define_getters_and_setters
  all_context_keys.each do |key|
    self.class.class_eval do

      # getter
      define_method key do
        self.context[key]
      end

      # setter
      define_method "#{key}=" do |val|
        self.context[key] = val
      end

    end
  end
end

#expectsObject



71
72
73
# File 'lib/simple_service/service_base.rb', line 71

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

#failure!(message = nil) ⇒ Object



87
88
89
90
# File 'lib/simple_service/service_base.rb', line 87

def failure!(message = nil)
  context[:success] = false
  context[:message] = message || 'There was a problem'
end

#find_specified_return_keysObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simple_service/service_base.rb', line 54

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

#return_context_with_success_statusObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_service/service_base.rb', line 43

def return_context_with_success_status
  _context = find_specified_return_keys

  # only automatically set context[:success] on Organizers and only if its not already set
  if !_context.has_key?(:success) && self.class.ancestors.include?(SimpleService::Organizer)
    _context[:success] = true
  end

  _context
end

#returnsObject



75
76
77
# File 'lib/simple_service/service_base.rb', line 75

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

#setup_call_chainObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_service/service_base.rb', line 20

def setup_call_chain
  self.class.class_eval do

    # grab the method object and hold onto it here
    call_method = instance_method(:call)

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

#skip_validationObject



79
80
81
# File 'lib/simple_service/service_base.rb', line 79

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

#symbolize_context_keysObject



37
38
39
40
41
# File 'lib/simple_service/service_base.rb', line 37

def symbolize_context_keys
  context.keys.each do |key|
    context[key.to_sym] = context.delete(key)
  end
end