Skip to content

Commit

Permalink
Added the ability to use success_or (#1)
Browse files Browse the repository at this point in the history
* init PR

* readme
  • Loading branch information
leonovk authored Aug 2, 2024
1 parent 515be28 commit badf61f
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.byebug_history
2 changes: 1 addition & 1 deletion Gemfile.lock
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/
Expand Down
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ gem 'simple_monads'

And then execute:

```bundle install```
```bash
bundle install
```

Or install it yourself as:

```gem install simple_monads```
```bash
gem install simple_monads
```

Require if necessary:

Expand Down Expand Up @@ -44,14 +48,37 @@ end

Then using this object you can check and use monads:

***Failure examples***

```ruby
object = SomeClass.new

result = object.return_failure

result.failure? # true

result.success? # false

result.failure # { error: 'text' }

result.success_or(19.32) # 19.32
```

The successful scenario is exactly the same.
***Success examples***

```ruby
object = SomeClass.new

result = object.return_success

result.failure? # false

result.success? # true

result.success # 5

result.success_or(19.32) # 5
```

## Contribution

Expand Down
8 changes: 4 additions & 4 deletions lib/simple_monads.rb
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
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
18 changes: 18 additions & 0 deletions lib/simple_monads/failure_result.rb
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
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
3 changes: 2 additions & 1 deletion lib/simple_monads/version.rb
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
15 changes: 15 additions & 0 deletions test/lib/simple_monads/failure_result_test.rb
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
15 changes: 15 additions & 0 deletions test/lib/simple_monads/success_result_test.rb
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
8 changes: 8 additions & 0 deletions test/lib/simple_monads_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ def test_empty_success

assert_nil(result.success)
end

def test_success_or
result_success = @object.return_success
result_failure = @object.return_failure

assert_equal(7, result_failure.success_or(7))
assert_equal(5, result_success.success_or(14.54))
end
end

0 comments on commit badf61f

Please sign in to comment.