Class: LightServiceObject::Base
- Inherits:
-
Object
- Object
- LightServiceObject::Base
- Extended by:
- Dry::Initializer
- Includes:
- Dry::Monads::Result::Mixin
- Defined in:
- lib/light_service_object.rb
Class Method Summary collapse
- .call(**options) ⇒ Object
- .expected_result_class(klass) ⇒ Object
- .optional(key, **options) ⇒ Object
- .param(key, **options) ⇒ Object
- .required(key, **options) ⇒ Object
-
.result_class ⇒ Object
— CLASS METHODS.
Instance Method Summary collapse
- #call ⇒ Object
- #error_reason(error) ⇒ Object
- #fail!(error) ⇒ Object
-
#result_class ⇒ Object
— INSTANCE METHODS.
Class Method Details
.call(**options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/light_service_object.rb', line 61 def self.call(**) obj = self.new(**) # Identify incoming params that weren't specified # set_params = obj.instance_variables.map{|e| e.to_s.tr("@","").to_sym } # unknown_params = (options.keys - set_params) # ap("#{self.name} > Unknown Parameters #{unknown_params}") if unknown_params.present? result = obj.call end |
.expected_result_class(klass) ⇒ Object
56 57 58 59 |
# File 'lib/light_service_object.rb', line 56 def self.expected_result_class(klass) @result_class = klass @result_class = klass.constantize if klass.is_a?(String) end |
.optional(key, **options) ⇒ Object
51 52 53 54 |
# File 'lib/light_service_object.rb', line 51 def self.optional(key, **) [:optional] = true option(key, **) end |
.param(key, **options) ⇒ Object
43 44 45 |
# File 'lib/light_service_object.rb', line 43 def self.param(key, **) raise Error.new("Do not use param in a service object") end |
.required(key, **options) ⇒ Object
47 48 49 |
# File 'lib/light_service_object.rb', line 47 def self.required(key, **) option key, ** end |
.result_class ⇒ Object
— CLASS METHODS
39 40 41 |
# File 'lib/light_service_object.rb', line 39 def self.result_class @result_class end |
Instance Method Details
#call ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/light_service_object.rb', line 77 def call result = self.perform if self.result_class.present? if !result.is_a?(self.result_class) a_name = "#{self.result_class}" a_name = %w[a e i o u y].include?(a_name.first.downcase) ? "an #{a_name}" : "a #{a_name}" fail!("#{self.name} is not returning #{a_name}") end end Dry::Monads.Success(result) rescue StandardError => error reason = self.error_reason(error) Dry::Monads.Failure(reason) end |
#error_reason(error) ⇒ Object
98 99 100 101 |
# File 'lib/light_service_object.rb', line 98 def error_reason(error) # Give subclasses a chance to see errors first "#{self}: #{error}" end |
#fail!(error) ⇒ Object
93 94 95 96 |
# File 'lib/light_service_object.rb', line 93 def fail!(error) error = ::StandardError.new(error.to_s) if !error.is_a?(::StandardError) raise error end |
#result_class ⇒ Object
— INSTANCE METHODS
73 74 75 |
# File 'lib/light_service_object.rb', line 73 def result_class self.class.result_class end |