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 Attribute Summary collapse
-
.result_class ⇒ Object
readonly
Returns the value of attribute result_class.
Class Method Summary collapse
- .call(**options) ⇒ Object
- .expected_result_class(klass) ⇒ Object
- .failed(error) ⇒ Object
- .optional(key, **options) ⇒ Object
- .param(key, **options) ⇒ Object
- .required(key, **options) ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.result_class ⇒ Object (readonly)
Returns the value of attribute result_class.
51 52 53 |
# File 'lib/light_service_object.rb', line 51 def result_class @result_class end |
Class Method Details
.call(**options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/light_service_object.rb', line 54 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 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 self.failed(error) Dry::Monads.Failure("#{self}: #{error}") end |
.expected_result_class(klass) ⇒ Object
45 46 47 48 |
# File 'lib/light_service_object.rb', line 45 def self.expected_result_class(klass) @result_class = klass @result_class = klass.constantize if klass.is_a?(String) end |
.failed(error) ⇒ Object
82 83 84 |
# File 'lib/light_service_object.rb', line 82 def self.failed(error) # Give subclasses a chance to see errors first end |
.optional(key, **options) ⇒ Object
40 41 42 43 |
# File 'lib/light_service_object.rb', line 40 def self.optional(key, **) [:optional] = true option(key, **) end |
.param(key, **options) ⇒ Object
32 33 34 |
# File 'lib/light_service_object.rb', line 32 def self.param(key, **) raise Error.new("Do not use param in a service object") end |
.required(key, **options) ⇒ Object
36 37 38 |
# File 'lib/light_service_object.rb', line 36 def self.required(key, **) option key, ** end |
Instance Method Details
#fail!(error) ⇒ Object
78 79 80 |
# File 'lib/light_service_object.rb', line 78 def fail!(error) raise (error.is_a?(String) ? Error.new(error) : error) end |