diff --git a/README.md b/README.md index 9c7c51c..1a0ad99 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,10 @@ foo.inside { |v| v.upcase! } #=> Some("BAR") for value in foo puts value #=> bar end + +# Thread an option through multiple methods +Option(" 1 ").thread(:strip, :to_i, :next).get #=> 2 +Option(" 1 ").thread(:strip, :to_i, :next).get_or_else { "else" } #=> else ``` ## Build Status diff --git a/lib/option.rb b/lib/option.rb index c9ddf39..bee7e6c 100644 --- a/lib/option.rb +++ b/lib/option.rb @@ -45,6 +45,11 @@ def each(&blk) nil end + def thread(*funcs) + return self unless funcs.length > 0 + Option(funcs.shift.to_proc.call(get)).thread(*funcs) + end + def or_nil get end @@ -128,6 +133,10 @@ def or_nil nil end + def thread(*funcs) + self + end + def present? false end