Skip to content

Commit

Permalink
create reflect class and add zeitwerk autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeu committed Oct 5, 2023
1 parent 6b60b1c commit a26d0d4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 62 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ group :test do
gem 'simplecov', require: false
end

gem 'zeitwerk'
gem 'rubocop', github: 'rubocop/rubocop', require: false
21 changes: 5 additions & 16 deletions lib/zx.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/zx/mixin.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions lib/zx/reflect.rb
Original file line number Diff line number Diff line change
@@ -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
52 changes: 8 additions & 44 deletions lib/zx/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ def deconstruct_keys(_)
end

def on_unknown(&block)
__execute__(nil, &block)
Reflect.apply(self, nil, &block)

self
end

def on_success(tag = nil, &block)
return self if failure?

__execute__(tag, &block)
Reflect.apply(self, tag, &block)

self
end

def on_failure(tag = nil, &block)
return self if success?

__execute__(tag, &block)
Reflect.apply(self, tag, &block)

self
end
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/lib/zx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a26d0d4

Please sign in to comment.