Sha256: 1eab82b1b4c24b1531e0ce2c461776b83a3fad69ce3d4fb30812e7391e2ae600
Contents?: true
Size: 1.74 KB
Versions: 1
Compression:
Stored size: 1.74 KB
Contents
require "light_service_object/version" require "dry/initializer" require "dry/monads/result" module LightServiceObject class Error < StandardError; end class Base extend Dry::Initializer include Dry::Monads::Result::Mixin def self.param(key, **options) raise Error.new("Do not use param in a service object") end def self.required(key, **options) option key, **options end def self.optional(key, **options) options[:optional] = true option(key, **options) end def self.expected_result_class(klass) @result_class = klass @result_class = klass.constantize if klass.is_a?(String) end class << self attr_reader :result_class end def self.call(**options) obj = self.new(**options) # 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 def fail!(error) raise (error.is_a?(String) ? Error.new(error) : error) end def self.failed(error) # Give subclasses a chance to see errors first end private def call end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
light_service_object-0.1.0 | lib/light_service_object.rb |