Module: SimpleService::ServiceBase::InstanceMethods
Instance Method Summary collapse
- #all_context_keys ⇒ Object
- #define_getters_and_setters ⇒ Object
- #expects ⇒ Object
- #failed? ⇒ Boolean
- #failure!(message = nil) ⇒ Object
- #find_specified_return_keys ⇒ Object
- #halted? ⇒ Boolean
- #optional ⇒ Object
- #organizer? ⇒ Boolean
- #return_context_with_success_status ⇒ Object
- #returns ⇒ Object
-
#setup_call_chain ⇒ Object
sets up an “after” filter for #call.
- #skip_validation ⇒ Object
- #success!(message = nil) ⇒ Object
- #successful? ⇒ Boolean
- #symbolize_context_keys ⇒ Object
Instance Method Details
#all_context_keys ⇒ Object
128 129 130 |
# File 'lib/simple_service/service_base.rb', line 128 def all_context_keys (expects + optional + returns + ['message', 'success']).uniq end |
#define_getters_and_setters ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/simple_service/service_base.rb', line 148 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 |
#expects ⇒ Object
112 113 114 |
# File 'lib/simple_service/service_base.rb', line 112 def expects self.class.get_expects end |
#failed? ⇒ Boolean
43 44 45 |
# File 'lib/simple_service/service_base.rb', line 43 def failed? !successful? end |
#failure!(message = nil) ⇒ Object
136 137 138 139 140 |
# File 'lib/simple_service/service_base.rb', line 136 def failure!( = nil) context[:success] = false context[:halt] = true context[:message] = || 'There was a problem' end |
#find_specified_return_keys ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/simple_service/service_base.rb', line 95 def find_specified_return_keys if returns.nil? || returns.empty? || failed? || halted? 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 |
#halted? ⇒ Boolean
47 48 49 |
# File 'lib/simple_service/service_base.rb', line 47 def halted? context.has_key?(:halt) || context[:halt] == true end |
#optional ⇒ Object
116 117 118 |
# File 'lib/simple_service/service_base.rb', line 116 def optional self.class.get_optional end |
#organizer? ⇒ Boolean
132 133 134 |
# File 'lib/simple_service/service_base.rb', line 132 def organizer? self.class.ancestors.include?(SimpleService::Organizer) end |
#return_context_with_success_status ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/simple_service/service_base.rb', line 83 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 # by a command calling #failure! if !_context.has_key?(:success) && organizer? _context[:success] = true end _context end |
#returns ⇒ Object
120 121 122 |
# File 'lib/simple_service/service_base.rb', line 120 def returns self.class.get_returns end |
#setup_call_chain ⇒ Object
sets up an “after” filter for #call
allows user to implement #call in their individual command and organizer # classes without having to rely on super or executing another other method to do post #call housekeeping such as returning only specific context keys
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/simple_service/service_base.rb', line 62 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... define_method :call do call_method.bind(self).call return_context_with_success_status end end end |
#skip_validation ⇒ Object
124 125 126 |
# File 'lib/simple_service/service_base.rb', line 124 def skip_validation self.class.instance_variable_get('@skip_validation') end |
#success!(message = nil) ⇒ Object
142 143 144 145 146 |
# File 'lib/simple_service/service_base.rb', line 142 def success!( = nil) context[:success] = true context[:halt] = true context[:message] = || 'Success! Returned early' end |
#successful? ⇒ Boolean
51 52 53 |
# File 'lib/simple_service/service_base.rb', line 51 def successful? !context.has_key?(:success) || context[:success] == true end |
#symbolize_context_keys ⇒ Object
77 78 79 80 81 |
# File 'lib/simple_service/service_base.rb', line 77 def symbolize_context_keys context.keys.each do |key| context[key.to_sym] = context.delete(key) end end |