Class: Rails::Service Abstract
Overview
This class is abstract.
Abstract base class for all 3rd party services
Provides:
- Configuration (automatically loaded from `config/services/[service_name].yml`, available as `config`)
- Logging (to separate log file `log/services/[service_name].log`, call via `logger.info(msg)`)
- Call style invocation (like `PDFGenerationService.(some, params)`)
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#service_name ⇒ Object
readonly
Returns the value of attribute service_name.
Class Method Summary collapse
-
.call ⇒ Object
Allows call syntax on class level: SomeService.(some, args).
Instance Method Summary collapse
-
#call(options) ⇒ Object
Abstract method for instance call.
-
#helpers ⇒ Object
Allows to use rails view helpers.
-
#initialize(service_name = nil) ⇒ Service
constructor
Constructor.
Constructor Details
#initialize(service_name = nil) ⇒ Service
Constructor. Call this from the subclass with the service name, like ‘super ’pdf_generator’‘. After that you can access the config and logger.
40 41 42 43 44 45 46 47 48 |
# File 'lib/rails/service.rb', line 40 def initialize(service_name = nil) raise NotImplementedError if self.class == Service raise 'Please provide a service name!' if service_name.nil? @service_name = service_name setup_logger setup_configuration end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
31 32 33 |
# File 'lib/rails/service.rb', line 31 def config @config end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
31 32 33 |
# File 'lib/rails/service.rb', line 31 def logger @logger end |
#service_name ⇒ Object (readonly)
Returns the value of attribute service_name.
31 32 33 |
# File 'lib/rails/service.rb', line 31 def service_name @service_name end |
Class Method Details
.call ⇒ Object
Allows call syntax on class level: SomeService.(some, args)
124 125 126 |
# File 'lib/rails/service.rb', line 124 def self.call(...) new.(...) end |
Instance Method Details
#call(options) ⇒ Object
Abstract method for instance call. Implement this in the subclass!
120 |
# File 'lib/rails/service.rb', line 120 def call(); end |
#helpers ⇒ Object
Allows to use rails view helpers
129 130 131 |
# File 'lib/rails/service.rb', line 129 def helpers ApplicationController.new.helpers end |