Class: Servicer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/servicer/base.rb

Overview

This is base class for subclassing your services. Example:

class MyClass < ::Servicer::Base
  layer :require_user

  def call(current_user, params)
    true
  end
end

MyClass.call(current_user, params)

Defined Under Namespace

Classes: AuthorizationError, ParamsError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



19
20
21
# File 'lib/servicer/base.rb', line 19

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



19
20
21
# File 'lib/servicer/base.rb', line 19

def params
  @params
end

Class Method Details

.call(current_user, params) ⇒ Object

Main call. Will create instance and run ‘call` on it. In most cases instance method should be overwritten.



33
34
35
36
# File 'lib/servicer/base.rb', line 33

def call(current_user, params)
  current_user, params = layers.inject([current_user, params]) { |arr, layer| layer.call(arr[0], arr[1]) }
  new(current_user, params).call
end

.layer(layer_class, options = {}, &block) ⇒ Object

Apply layer. Layer class can be any class based on ::Servicer::Layers::Base or symbolized name of layer.



27
28
29
30
# File 'lib/servicer/base.rb', line 27

def layer(layer_class, options = {}, &block)
  layer_class = ::Servicer::Layers.const_get(layer_class.to_s.camelcase) if layer_class.is_a?(Symbol)
  layers << layer_class.new(options, &block)
end

.layersObject



22
23
24
# File 'lib/servicer/base.rb', line 22

def layers
  @layers ||= []
end

Instance Method Details

#callObject

Main call. Overwrite this.



40
# File 'lib/servicer/base.rb', line 40

def call; end