Class: TheHelp::Service
- Inherits:
-
Object
- Object
- TheHelp::Service
- Includes:
- ProvidesCallbacks, ServiceCaller
- Defined in:
- lib/the_help/service.rb
Overview
See README section “Running Callbacks”
An Abstract Service Class with Authorization and Logging
Define subclasses of Service to build out the service layer of your application.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- CB_NOT_AUTHORIZED =
The default :not_authorized callback
It will raise a TheHelp::NotAuthorizedError when the context is not authorized to perform the service.
->(service:, context:) { raise TheHelp::NotAuthorizedError, "Not authorized to access #{service.name} as #{context.inspect}." }
Class Method Summary collapse
-
.attr_accessor(*names, make_private: false, private_reader: false, private_writer: false) ⇒ Object
Defines attr_accessors with scoping options.
-
.authorization_policy(allow_all: false, &block) ⇒ Object
Defines the service authorization policy.
-
.call(*args, &block) ⇒ Object
Convenience method to instantiate the service and immediately call it.
-
.inherited(other) ⇒ Object
:nodoc:.
- .input(name, **options) ⇒ Object
-
.main(&block) ⇒ Object
Defines the primary routine of the service.
-
.required_inputs ⇒ Object
:nodoc: instances need access to this, otherwise it would be made private.
Instance Method Summary collapse
-
#call ⇒ TheHelp::Service::Result
Executes the service and returns the result.
-
#initialize(context:, logger: Logger.new($stdout), not_authorized: CB_NOT_AUTHORIZED, **inputs) ⇒ Service
constructor
A new instance of Service.
Methods included from ServiceCaller
Methods included from ProvidesCallbacks
Constructor Details
#initialize(context:, logger: Logger.new($stdout), not_authorized: CB_NOT_AUTHORIZED, **inputs) ⇒ Service
Returns a new instance of Service.
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/the_help/service.rb', line 218 def initialize(context:, logger: Logger.new($stdout), not_authorized: CB_NOT_AUTHORIZED, **inputs) @result = Result.new self.context = context self.logger = logger self. = self.inputs = inputs self.stop_caller = false end |
Class Method Details
.attr_accessor(*names, make_private: false, private_reader: false, private_writer: false) ⇒ Object
Defines attr_accessors with scoping options
97 98 99 100 101 102 103 104 |
# File 'lib/the_help/service.rb', line 97 def attr_accessor(*names, make_private: false, private_reader: false, private_writer: false) super(*names) names.each do |name| private name if make_private || private_reader private "#{name}=" if make_private || private_writer end end |
.authorization_policy(allow_all: false, &block) ⇒ Object
Defines the service authorization policy
If allow_all is set to true, or if the provided block (executed in the context of the service object) returns true, then the service will be run when called. Otherwise, the not_authorized callback will be invoked.
143 144 145 146 147 148 149 150 151 |
# File 'lib/the_help/service.rb', line 143 def (allow_all: false, &block) if allow_all define_method(:authorized?) { true } else define_method(:authorized?, &block) end private :authorized? self end |
.call(*args, &block) ⇒ Object
Convenience method to instantiate the service and immediately call it
Any arguments are passed to #initialize
109 110 111 |
# File 'lib/the_help/service.rb', line 109 def call(*args, &block) new(*args).call(&block) end |
.inherited(other) ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/the_help/service.rb', line 114 def inherited(other) other.instance_variable_set(:@required_inputs, required_inputs.dup) end |
.input(name, **options) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/the_help/service.rb', line 153 def input(name, **) attr_accessor name, make_private: true if .key?(:default) required_inputs.delete(name) define_method(name) do instance_variable_get("@#{name}") || [:default] end else required_inputs << name end self end |
.main(&block) ⇒ Object
Defines the primary routine of the service
The code that will be run when the service is called, assuming it is unauthorized.
128 129 130 131 132 |
# File 'lib/the_help/service.rb', line 128 def main(&block) define_method(:main, &block) private :main self end |
.required_inputs ⇒ Object
:nodoc: instances need access to this, otherwise it would be made private
120 121 122 |
# File 'lib/the_help/service.rb', line 120 def required_inputs @required_inputs ||= Set.new end |
Instance Method Details
#call ⇒ TheHelp::Service::Result
Executes the service and returns the result
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/the_help/service.rb', line 232 def call validate_service_definition catch(:stop) do log_service_call main check_result! self.block_result = yield result if block_given? end throw :stop if stop_caller return block_result if block_given? return result end |