aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobert2017-07-31 08:03:13 +0200
committerRobert2017-07-31 08:03:13 +0200
commit609b774388a7f57703ec14a224363c88f3564eaf (patch)
tree4610e6b5a82a6f472d2b6ecc7901385b7415f296 /lib
parentaa5028a21f28a2bee9f64b5e87e70828c9c8b75f (diff)
downloadchouette-core-609b774388a7f57703ec14a224363c88f3564eaf.tar.bz2
Refs: #3507_1726@6h Code Review
Diffstat (limited to 'lib')
-rw-r--r--lib/result.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/result.rb b/lib/result.rb
new file mode 100644
index 000000000..96e03d323
--- /dev/null
+++ b/lib/result.rb
@@ -0,0 +1,37 @@
+# A value wrapper adding status information to any value
+# Status can be :ok or :error, we are thusly implementing
+# what is expressed in Elixir/Erlang as result tuples and
+# in Haskell as `Data.Either`
+class Result
+
+ attr_reader :status, :value
+
+ class << self
+ def ok value
+ make :ok, value
+ end
+ def error value
+ make :error, value
+ end
+
+ def new *args
+ raise NoMethodError, "No default constructor for #{self}"
+ end
+
+ private
+ def make status, value
+ allocate.tap do | o |
+ o.instance_exec do
+ @status = status
+ @value = value
+ end
+ end
+ end
+ end
+
+ def ok?; status == :ok end
+
+ def == other
+ other.kind_of?(self.class) && other.status == status && other.value == value
+ end
+end