class RestPack::Service::Command

Attributes

response[RW]

Public Class Methods

inherited(command) click to toggle source
# File lib/restpack_service/command.rb, line 67
def self.inherited(command)
  namespaces = command.to_s.split('::') # eg. GroupService::Commands::Group::Create
  add_module_aliases(command, namespaces)
end

Private Class Methods

add_module_aliases(command, namespaces) click to toggle source
# File lib/restpack_service/command.rb, line 74
def self.add_module_aliases(command, namespaces)
  Modularize.create("#{namespaces[0]}::Models")
  Modularize.create("#{namespaces[0]}::Serializers")
  Modularize.create("#{namespaces[0]}::#{namespaces[2]}")

  model_class = "#{namespaces[0]}::Models::#{namespaces[2]}".safe_constantize
  if model_class
    command.const_set(:Model, model_class)
    command.send(:define_singleton_method, "model_class") { model_class }
  end

  serializer_class = "#{namespaces[0]}::Serializers::#{namespaces[2]}".safe_constantize
  if serializer_class
    command.const_set(:Serializer, serializer_class)
    command.send(:define_singleton_method, "serializer_class") { serializer_class }
  end

  command.const_set(:Commands, "#{namespaces[0]}::Commands".safe_constantize)
  command.const_set(:Models, "#{namespaces[0]}::Models".safe_constantize)
end
method_container_module(command, namespaces) click to toggle source
# File lib/restpack_service/command.rb, line 95
def self.method_container_module(command, namespaces)
  if namespaces[1] == 'Commands'
    "#{namespaces[0]}::#{namespaces[2]}".safe_constantize #GroupService::Group
  else
    namespaces.take(namespaces.length - 1).join('::').safe_constantize #Commands::Group
  end
end

Public Instance Methods

Model() click to toggle source
# File lib/restpack_service/command.rb, line 63
def Model
  self.class.model_class
end
Serializer() click to toggle source
# File lib/restpack_service/command.rb, line 59
def Serializer
  self.class.serializer_class
end
field_error(key, message) click to toggle source
# File lib/restpack_service/command.rb, line 55
def field_error(key, message)
  add_error key, key, message
end
init() click to toggle source
# File lib/restpack_service/command.rb, line 40
def init
end
run() click to toggle source
Calls superclass method
# File lib/restpack_service/command.rb, line 7
def run
  @response = RestPack::Service::Response.new

  begin
    init
    mutation = super

    if mutation.errors
      mutation.errors.message.each do |error|
        @response.add_error(error[0], error[1].gsub(error[0].capitalize, ''))
      end

      @response.status ||= :unprocessable_entity
    else
      @response.status ||= :ok
    end

    if @response.status == :ok
      @response.result = mutation.result if mutation.result
    end
  rescue Exception => e
    puts "---COMMAND EXCEPTION---"
    puts e.message #TODO: GJ: logging
    puts e.backtrace
    puts "-----------------------"

    @response.add_error(:base, 'Service Error')
    @response.status = :internal_service_error
  end

  @response
end
service_error(message) click to toggle source
# File lib/restpack_service/command.rb, line 51
def service_error(message)
  field_error :base, message
end
status(status) click to toggle source
# File lib/restpack_service/command.rb, line 43
def status(status)
  @response.status = status
end
valid?() click to toggle source
# File lib/restpack_service/command.rb, line 47
def valid?
  !has_errors?
end