module SimpleService::ClassMethods

Public Instance Methods

call(kwargs={}) click to toggle source
# 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) click to toggle source
# File lib/simple_service.rb, line 22
def command(command_name)
  @commands ||= []
  @commands << command_name
end
commands(*args) click to toggle source
# File lib/simple_service.rb, line 27
def commands(*args)
  @commands ||= []
  @commands += args
end
execute_command(cmnd, service) click to toggle source
# 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) click to toggle source
# 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