-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create reflect class and add zeitwerk autoloader
- Loading branch information
Showing
6 changed files
with
60 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters