Module: SimpleService::ClassMethods
- Defined in:
- lib/simple_service.rb
Instance Method Summary collapse
- #call(kwargs = {}) ⇒ Object
- #command(command_name) ⇒ Object
- #commands(*args) ⇒ Object
- #execute_command(cmnd, service) ⇒ Object
- #get_commands(service) ⇒ Object
Instance Method Details
#call(kwargs = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/simple_service.rb', line 32 def call(kwargs={}) service = self.new # if kwargs is a result obj then pull its hash out via #value service.result.value = kwargs.is_a?(Result) ? kwargs.value : kwargs get_commands(service).each do |cmnd| service = execute_command(cmnd, service) break if service.result.failure? end service.result end |
#command(command_name) ⇒ Object
22 23 24 25 |
# File 'lib/simple_service.rb', line 22 def command(command_name) @commands ||= [] @commands << command_name end |
#commands(*args) ⇒ Object
27 28 29 30 |
# File 'lib/simple_service.rb', line 27 def commands(*args) @commands ||= [] @commands += args end |
#execute_command(cmnd, service) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/simple_service.rb', line 57 def execute_command(cmnd, service) service.current_command = cmnd command_output = if cmnd.is_a?(Class) cmnd.call(service.result.value) elsif cmnd.is_a?(Symbol) if service.method(cmnd).arity.zero? service.public_send(cmnd) else service.public_send(cmnd, service.result.value) end end if command_output.is_a?(Result) service.result.append_result(command_output) end service end |
#get_commands(service) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/simple_service.rb', line 46 def get_commands(service) if !@commands.nil? && @commands.any? @commands elsif service.respond_to?(:call) [:call] else raise "No commands defined for #{self.to_s}, define at least one " \ 'command or implement the #call method' end end |