diff --git a/.gitignore b/.gitignore index 45d0610..aaee826 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .rvmrc .ruby-version *.gem -Gemfile.lock +Gemfile*lock diff --git a/.travis.yml b/.travis.yml index e69414c..1d6e59f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Gemfile b/Gemfile index e3d7ebf..01948f9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,6 @@ source 'http://rubygems.org' +# gem 'dry-monads' + gemspec platforms :rbx do diff --git a/Gemfile.with_dry_monads_03 b/Gemfile.with_dry_monads_03 new file mode 100644 index 0000000..7106b7e --- /dev/null +++ b/Gemfile.with_dry_monads_03 @@ -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 diff --git a/Gemfile.with_dry_monads_04 b/Gemfile.with_dry_monads_04 new file mode 100644 index 0000000..67d52bd --- /dev/null +++ b/Gemfile.with_dry_monads_04 @@ -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 diff --git a/lib/mutations/outcome.rb b/lib/mutations/outcome.rb index 177e039..624845c 100644 --- a/lib/mutations/outcome.rb +++ b/lib/mutations/outcome.rb @@ -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) @@ -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