Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dry::Monads ❤️ Mutations::Outcome #124

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.rvmrc
.ruby-version
*.gem
Gemfile.lock
Gemfile*lock
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ rvm:
- 2.4.0
- jruby-9.1.7.0
- rbx-3
gemfile:
- Gemfile
- Gemfile.with_dry_monads_03
- Gemfile.with_dry_monads_04
matrix:
exclude:
- rvm: 1.9.3
gemfile: Gemfile.with_dry_monads_03
- rvm: jruby-1.7.26
gemfile: Gemfile.with_dry_monads_03
- rvm: rbx-3
gemfile: Gemfile.with_dry_monads_03
- rvm: 2.0.0
gemfile: Gemfile.with_dry_monads_03
- rvm: 1.9.3
gemfile: Gemfile.with_dry_monads_04
- rvm: jruby-1.7.26
gemfile: Gemfile.with_dry_monads_04
- rvm: rbx-3
gemfile: Gemfile.with_dry_monads_04
- rvm: 2.0.0
gemfile: Gemfile.with_dry_monads_04
- rvm: 2.1.10
gemfile: Gemfile.with_dry_monads_04
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source 'http://rubygems.org'
# gem 'dry-monads'

gemspec

platforms :rbx do
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.with_dry_monads_03
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'http://rubygems.org'
gem 'dry-monads', '< 0.4'

gemspec

platforms :rbx do
gem 'rubysl', '~> 2.0'
gem 'psych'
gem 'rubinius-developer_tools'
end
10 changes: 10 additions & 0 deletions Gemfile.with_dry_monads_04
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'http://rubygems.org'
gem 'dry-monads', '>= 0.4'

gemspec

platforms :rbx do
gem 'rubysl', '~> 2.0'
gem 'psych'
gem 'rubinius-developer_tools'
end
61 changes: 60 additions & 1 deletion lib/mutations/outcome.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
module Mutations
class Outcome
module Outcome
def self.new(is_success, result, errors, inputs)
if defined?(::Mutations::SuccessOutcome)
if is_success
::Mutations::SuccessOutcome.new(result, inputs: inputs)
else
::Mutations::FailureOutcome.new(errors, inputs: inputs)
end
else
::Mutations::ClassicOutcome.new(is_success, result, errors, inputs)
end
end
end

class ClassicOutcome
include Outcome
attr_reader :result, :errors, :inputs

def initialize(is_success, result, errors, inputs)
Expand All @@ -10,4 +25,48 @@ def success?
@success
end
end

begin
require 'dry/monads/either'

module MonadicOutcome
def initialize(value, options = {})
super(value)
@inputs = options[:inputs]
end

def result(*args)
args.empty? ? (@value || @right if success?) : super
end

def errors
@value || @left unless success?
end
end

class SuccessOutcome < Dry::Monads::Either::Right
include Outcome
include MonadicOutcome

attr_reader :inputs
end

class FailureOutcome < Dry::Monads::Either::Left
include Outcome
include MonadicOutcome

attr_reader :inputs

def or_fmap(*args, &block)
SuccessOutcome.new(self.or(*args, &block), inputs: @inputs)
end

def flip
SuccessOutcome.new(@value, inputs: @inputs)
end
end
rescue LoadError
puts "dry/monads integration not available. Load the dry-monads gem before mutations if needed."
end

end