aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorteddywing2017-08-01 17:39:10 +0200
committerGitHub2017-08-01 17:39:10 +0200
commit1f09ead58c9c603e9d767781ceb82859b2393f49 (patch)
treeaec7d66e003ef9768156976750e27c38fd22cc0a /lib
parent60ae8866d6d4c55bc064a2a83c1a1ffa87894202 (diff)
parentee75bd1e579ab366eb6cac938f50e7786536472b (diff)
downloadchouette-core-1f09ead58c9c603e9d767781ceb82859b2393f49.tar.bz2
Merge pull request #46 from af83/3507_1726_impl_workbench_import
3507 1726 impl workbench import
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