lib/trav3/options.rb in trav3-0.2.2 vs lib/trav3/options.rb in trav3-0.2.3
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
module Trav3
class Options
def initialize(**args)
build(**args)
end
@@ -21,10 +23,26 @@
end
self
end
+ def fetch(key)
+ @opts.each do |item|
+ return item if key.to_s == split.call(item).first
+ end
+
+ raise KeyError, "key not found #{key}" unless block_given?
+
+ yield
+ end
+
+ def fetch!(key, &block)
+ result = fetch(key, &block)
+ remove(key)
+ result
+ end
+
def remove(key)
return_value = nil
@opts = @opts.delete_if do |item|
head, tail = split.call item
@@ -35,9 +53,11 @@
return_value
end
def reset!
@opts = []
+
+ self
end
def +(other)
raise TypeError, "Options type expected, #{other.class} given" unless other.is_a? Options