From 21593ace9541da9bb9989d10c3450ff63856ba2f Mon Sep 17 00:00:00 2001 From: Saverio Trioni Date: Tue, 30 Jan 2018 19:57:02 +0100 Subject: [PATCH 1/5] Dry::Monads :loves: Mutations::Outcome --- .gitignore | 2 +- .travis.yml | 4 +++ Gemfile | 2 ++ Gemfile.with_dry_monads_03 | 10 +++++++ Gemfile.with_dry_monads_04 | 10 +++++++ lib/mutations/outcome.rb | 61 +++++++++++++++++++++++++++++++++++++- 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 Gemfile.with_dry_monads_03 create mode 100644 Gemfile.with_dry_monads_04 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..483ebd0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,3 +13,7 @@ rvm: - 2.4.0 - jruby-9.1.7.0 - rbx-3 +gemfile: + - Gemfile + - Gemfile.with_dry_monads_03 + - Gemfile.with_dry_monads_04 \ No newline at end of file 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..ea19b77 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, inputs: nil) + super(value) + @inputs = 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 From 0818e63ff364228f073b74614bdd413ec5f9fa32 Mon Sep 17 00:00:00 2001 From: Saverio Trioni Date: Wed, 31 Jan 2018 10:06:34 +0100 Subject: [PATCH 2/5] Exclude from travis incomaptible versions of VM --- .travis.yml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 483ebd0..95a8eb1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,4 +16,18 @@ rvm: gemfile: - Gemfile - Gemfile.with_dry_monads_03 - - Gemfile.with_dry_monads_04 \ No newline at end of file + - Gemfile.with_dry_monads_04 +matrix: + exclude: + - rvm: + - 1.9.3 + - jruby-1.7.26 + - rbx-3 + gemfile: Gemfile.with_dry_monads_03 + - rvm: + - 1.9.3 + - jruby-1.7.26 + - rbx-3 + - 2.0.0 + - 2.1.10 + gemfile: Gemfile.with_dry_monads_04 From b377e235c8a59d4cb22642a82000a57282f6c79f Mon Sep 17 00:00:00 2001 From: Saverio Trioni Date: Wed, 31 Jan 2018 18:13:42 +0100 Subject: [PATCH 3/5] Matrix exclude does not allow nested lists --- .travis.yml | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 95a8eb1..c1827ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,15 +19,19 @@ gemfile: - Gemfile.with_dry_monads_04 matrix: exclude: - - rvm: - - 1.9.3 - - jruby-1.7.26 - - rbx-3 + - rvm: 1.9.3 gemfile: Gemfile.with_dry_monads_03 - - rvm: - - 1.9.3 - - jruby-1.7.26 - - rbx-3 - - 2.0.0 - - 2.1.10 + - rvm: jruby-1.7.26 + gemfile: Gemfile.with_dry_monads_03 + - rvm: rbx-3 + 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 From 48a171e522df05e8587a4375cf6299e62662f2cc Mon Sep 17 00:00:00 2001 From: Saverio Trioni Date: Wed, 31 Jan 2018 18:41:23 +0100 Subject: [PATCH 4/5] Exclude ruby 2.0 (because of dry-core) --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index c1827ab..1d6e59f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,6 +25,8 @@ matrix: 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 From ab620d137333870fd909cd8d579ccbe933dceba3 Mon Sep 17 00:00:00 2001 From: Saverio Trioni Date: Thu, 1 Feb 2018 10:14:04 +0100 Subject: [PATCH 5/5] Ruby 1.9.3 does not like the kwarg syntax --- lib/mutations/outcome.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mutations/outcome.rb b/lib/mutations/outcome.rb index ea19b77..624845c 100644 --- a/lib/mutations/outcome.rb +++ b/lib/mutations/outcome.rb @@ -30,9 +30,9 @@ def success? require 'dry/monads/either' module MonadicOutcome - def initialize(value, inputs: nil) + def initialize(value, options = {}) super(value) - @inputs = inputs + @inputs = options[:inputs] end def result(*args)