Class: LightServiceObject::Base

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Includes:
Dry::Monads::Result::Mixin
Defined in:
lib/light_service_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(**options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/light_service_object.rb', line 71

def call(**options)
  begin
    obj = self.new(**options)
  rescue KeyError => e
    return  Dry::Monads.Failure(e.message)
  end

  # 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



66
67
68
69
# File 'lib/light_service_object.rb', line 66

def expected_result_class(klass)
  @result_class = klass
  @result_class = klass.constantize if klass.is_a?(String)
end

.failed(error) ⇒ Object



87
88
# File 'lib/light_service_object.rb', line 87

def self.failed(error)
end

.option(*args, **opts, &block) ⇒ Object



48
49
50
51
52
53
# File 'lib/light_service_object.rb', line 48

def option(*args, **opts, &block)
  if opts.delete(:mutable)
    self.send("attr_writer", args.first)
  end
  super(*args, **opts, &block)
end

.optional(key, **options) ⇒ Object



60
61
62
63
64
# File 'lib/light_service_object.rb', line 60

def optional(key, **options)
  options[:optional] = true
  options[:private] = true
  option(key, **options)
end

.param(key, **options) ⇒ Object

Raises:

  • (Error)


44
45
46
# File 'lib/light_service_object.rb', line 44

def param(key, **options)
  raise Error.new("Do not use param in a service object")
end

.required(key, **options) ⇒ Object



55
56
57
58
# File 'lib/light_service_object.rb', line 55

def required(key, **options)
  options[:private] = true
  option(key, **options)
end

.result_classObject



40
41
42
# File 'lib/light_service_object.rb', line 40

def result_class
  @result_class
end

Instance Method Details

#callObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/light_service_object.rb', line 95

def call
  result = self.perform
  if self.result_class
    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
  fail!(error)
end

#error_reason(error) ⇒ Object



117
118
119
120
# File 'lib/light_service_object.rb', line 117

def error_reason(error)
  # Give subclasses a chance to see errors first
  "[#{self.class}] #{error}"
end

#fail!(error) ⇒ Object



110
111
112
113
114
115
# File 'lib/light_service_object.rb', line 110

def fail!(error)
  error = ::StandardError.new(error.to_s) if !error.is_a?(::StandardError)
  reason = self.error_reason(error)
  self.class.failed(error)
  Dry::Monads.Failure(reason)
end

#result_classObject

— INSTANCE METHODS



91
92
93
# File 'lib/light_service_object.rb', line 91

def result_class
  self.class.result_class
end