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