Class: Servicer::Base
- Inherits:
-
Object
- Object
- Servicer::Base
- 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
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
-
.call(current_user, params) ⇒ Object
Main call.
-
.layer(layer_class, options = {}, &block) ⇒ Object
Apply layer.
- .layers ⇒ Object
Instance Method Summary collapse
-
#call ⇒ Object
Main call.
Instance Attribute Details
#current_user ⇒ Object (readonly)
Returns the value of attribute current_user.
19 20 21 |
# File 'lib/servicer/base.rb', line 19 def current_user @current_user end |
#params ⇒ Object (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, = {}, &block) layer_class = ::Servicer::Layers.const_get(layer_class.to_s.camelcase) if layer_class.is_a?(Symbol) layers << layer_class.new(, &block) end |
.layers ⇒ Object
22 23 24 |
# File 'lib/servicer/base.rb', line 22 def layers @layers ||= [] end |
Instance Method Details
#call ⇒ Object
Main call. Overwrite this.
40 |
# File 'lib/servicer/base.rb', line 40 def call; end |