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.
56 57 58 |
# File 'lib/light_service_object.rb', line 56 def result_class @result_class end |
Class Method Details
.call(**options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/light_service_object.rb', line 59 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
50 51 52 53 |
# File 'lib/light_service_object.rb', line 50 def self.expected_result_class(klass) @result_class = klass @result_class = klass.constantize if klass.is_a?(String) end |
.failed(error) ⇒ Object
87 88 89 |
# File 'lib/light_service_object.rb', line 87 def self.failed(error) # Give subclasses a chance to see errors first end |
.optional(key, **options) ⇒ Object
45 46 47 48 |
# File 'lib/light_service_object.rb', line 45 def self.optional(key, **) [:optional] = true option(key, **) end |
.param(key, **options) ⇒ Object
37 38 39 |
# File 'lib/light_service_object.rb', line 37 def self.param(key, **) raise Error.new("Do not use param in a service object") end |
.required(key, **options) ⇒ Object
41 42 43 |
# File 'lib/light_service_object.rb', line 41 def self.required(key, **) option key, ** end |
Instance Method Details
#fail!(error) ⇒ Object
83 84 85 |
# File 'lib/light_service_object.rb', line 83 def fail!(error) raise (error.is_a?(String) ? ::StandardError.new(error) : error) end |