-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to use
success_or
(#1)
* init PR * readme
- Loading branch information
Showing
11 changed files
with
102 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.byebug_history |
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,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
simple_monads (0.1.3) | ||
simple_monads (1.0.1) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
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,15 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'simple_monads/failure_object' | ||
require_relative 'simple_monads/success_object' | ||
require_relative 'simple_monads/failure_result' | ||
require_relative 'simple_monads/success_result' | ||
|
||
# Main module | ||
module SimpleMonads | ||
def Success(object = nil) # rubocop:disable Naming/MethodName | ||
SuccessObject.new(object) | ||
SuccessResult.new(object) | ||
end | ||
|
||
def Failure(object = nil) # rubocop:disable Naming/MethodName | ||
FailureObject.new(object) | ||
FailureResult.new(object) | ||
end | ||
end |
15 changes: 5 additions & 10 deletions
15
lib/simple_monads/failure_object.rb → lib/simple_monads/base_result.rb
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,25 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module SimpleMonads | ||
# failure object | ||
class FailureObject | ||
# Base Result monads | ||
class BaseResult | ||
attr_reader :object | ||
alias failure object | ||
|
||
def initialize(object = nil) | ||
@object = object | ||
end | ||
|
||
def failure? | ||
true | ||
end | ||
|
||
def success? | ||
false | ||
def success_or(value) | ||
failure? ? value : success | ||
end | ||
|
||
def inspect | ||
"Failure(#{object})" | ||
"#{self.class.to_s[14..-7]}(#{object})" | ||
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'base_result' | ||
|
||
module SimpleMonads | ||
# failure object | ||
class FailureResult < BaseResult | ||
alias failure object | ||
|
||
def failure? | ||
true | ||
end | ||
|
||
def success? | ||
false | ||
end | ||
end | ||
end |
13 changes: 3 additions & 10 deletions
13
lib/simple_monads/success_object.rb → lib/simple_monads/success_result.rb
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,25 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'base_result' | ||
|
||
module SimpleMonads | ||
# success object | ||
class SuccessObject | ||
attr_reader :object | ||
class SuccessResult < BaseResult | ||
alias success object | ||
|
||
def initialize(object = nil) | ||
@object = object | ||
end | ||
|
||
def failure? | ||
false | ||
end | ||
|
||
def success? | ||
true | ||
end | ||
|
||
def inspect | ||
"Success(#{object})" | ||
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module SimpleMonads | ||
VERSION = '0.1.4' | ||
# gem version | ||
VERSION = '1.0.1' | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
module SimpleMonads | ||
class FailureResultTest < Minitest::Test | ||
def setup | ||
@failure = FailureResult.new(2) | ||
end | ||
|
||
def test_inspect | ||
assert_equal('Failure(2)', @failure.inspect) | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
module SimpleMonads | ||
class SuccessResultTest < Minitest::Test | ||
def setup | ||
@failure = SuccessResult.new(2) | ||
end | ||
|
||
def test_inspect | ||
assert_equal('Success(2)', @failure.inspect) | ||
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