From a26d0d4d0f6eb56a3a47d05b7950dd3c36054496 Mon Sep 17 00:00:00 2001 From: Thadeu Esteves Jr Date: Thu, 5 Oct 2023 11:29:10 -0300 Subject: [PATCH] create reflect class and add zeitwerk autoloader --- Gemfile | 1 + lib/zx.rb | 21 +++++------------- lib/zx/mixin.rb | 22 +++++++++++++++++++ lib/zx/reflect.rb | 22 +++++++++++++++++++ lib/zx/result.rb | 52 +++++++-------------------------------------- spec/lib/zx_spec.rb | 4 ++-- 6 files changed, 60 insertions(+), 62 deletions(-) create mode 100644 lib/zx/mixin.rb create mode 100644 lib/zx/reflect.rb diff --git a/Gemfile b/Gemfile index 7915adf..04f2cda 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,5 @@ group :test do gem 'simplecov', require: false end +gem 'zeitwerk' gem 'rubocop', github: 'rubocop/rubocop', require: false \ No newline at end of file diff --git a/lib/zx.rb b/lib/zx.rb index cb84c4a..749aecf 100644 --- a/lib/zx.rb +++ b/lib/zx.rb @@ -1,21 +1,10 @@ # frozen_string_literal: true -require 'zx/result' - module Zx - module Extendable - Success = ->(*kwargs) { Result.Success(*kwargs) } - Failure = ->(*kwargs) { Result.Failure(*kwargs) } - - def Success(...) - Result.Success(...) - end +end - def Failure(...) - Result.Failure(...) - end - end +require 'zeitwerk' - include Extendable - extend Extendable -end +loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false) +loader.setup +loader.eager_load diff --git a/lib/zx/mixin.rb b/lib/zx/mixin.rb new file mode 100644 index 0000000..2d03025 --- /dev/null +++ b/lib/zx/mixin.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Zx + module Mixin + Success = ->(value = nil, options = {}) { Zx::Result.new.success!(value, options) } + Failure = ->(value = nil, options = {}) { Zx::Result.new.failure!(value, options) } + + def Success(value = nil, options = {}) + Zx::Result.new.success!(value, type: options.fetch(:type, :ok)) + end + + def Failure(value = nil, options = {}) + Zx::Result.new.failure!(value, type: options.fetch(:type, :error)) + end + + def Try(default = nil, options = {}) + Success[yield] + rescue StandardError => e + Failure[default || options.fetch(:or, nil)] + end + end +end diff --git a/lib/zx/reflect.rb b/lib/zx/reflect.rb new file mode 100644 index 0000000..b66132c --- /dev/null +++ b/lib/zx/reflect.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Zx + class Reflect + attr_accessor :result, :tag + + def initialize(result, tag) + self.result = result + self.tag = tag + end + + def apply(&block) + return block.call(result.value, [result.type, result.success?]) if tag.nil? + + block.call(result.value, [result.type, result.success?]) if result.type == tag.to_sym + end + + def self.apply(result, tag, &block) + new(result, tag).apply(&block) + end + end +end diff --git a/lib/zx/result.rb b/lib/zx/result.rb index a7a0abe..416f469 100644 --- a/lib/zx/result.rb +++ b/lib/zx/result.rb @@ -38,7 +38,7 @@ def deconstruct_keys(_) end def on_unknown(&block) - __execute__(nil, &block) + Reflect.apply(self, nil, &block) self end @@ -46,7 +46,7 @@ def on_unknown(&block) def on_success(tag = nil, &block) return self if failure? - __execute__(tag, &block) + Reflect.apply(self, tag, &block) self end @@ -54,7 +54,7 @@ def on_success(tag = nil, &block) def on_failure(tag = nil, &block) return self if success? - __execute__(tag, &block) + Reflect.apply(self, tag, &block) self end @@ -108,46 +108,10 @@ def success!(value = nil, type: :ok) self end - def __execute__(tag = nil, &block) - return block.call(@value, [@type, @success]) if tag.nil? - - block.call(@value, [@type, @success]) if @type == tag.to_sym - end - private :__execute__ - - def Success(value = nil, options = {}) - success!(value, type: options.fetch(:type, :ok)) - end - - def Success!(value = nil, options = {}) - success!(value, type: options.fetch(:type, :ok)) - end - - def Failure(value = nil, options = {}) - failure!(value, type: options.fetch(:type, :error)) - end - - def Failure!(value = nil, options = {}) - failure!(value, type: options.fetch(:type, :error)) - end - - def self.Success(value = nil, options = {}) - new.success!(value, type: options.fetch(:type, :ok)) - end - - def self.Success!(...) - Success(...) - end - - def self.Failure(value = nil, options = {}) - new.failure!(value, type: options.fetch(:type, :error)) - end - - def self.Failure!(...) - Failure(...) - end - - Success = ->(*kwargs) { Success(*kwargs) } - Failure = ->(*kwargs) { Failure(*kwargs) } + include Mixin + extend Mixin end + + include Mixin + extend Mixin end diff --git a/spec/lib/zx_spec.rb b/spec/lib/zx_spec.rb index d1eb8a2..6661751 100644 --- a/spec/lib/zx_spec.rb +++ b/spec/lib/zx_spec.rb @@ -10,9 +10,9 @@ def initialize(tax: 0.1) def apply(value) price = value + (value * @tax) - return Failure! :priceless if price < 100 + return Failure :priceless if price < 100 - Success! price: price + Success price: price end end