Module: FirstOf
- Included in:
- Object
- Defined in:
- lib/first_of.rb,
lib/first_of/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#first_of(*args) ⇒ Object
Should take arguments: first_of(:symbol1, :symbol2) # will return first respond to and present first_of([:symbol1, :symbol2]) # will return value if try_chain(:symbol1, :symbol2) present first_of(1 => :symbol1, 2 => :symbol2) # will prioritize trying by key and return first present first_of(1 => :symbol1, 2 => lambda { _call }) # will prioritize and only execute callable if first not present first_of(1 => [:symbol1, :symbol2], 2 => -> { _call }) # will prioritize and execute try_chain on first and call 2nd if first not present first_of(1 => :symbol1, 2 => 4) # will prioritize and return value (4) if first not present first_of(lambda { _call }, 1 => :symbol1, 2 => :symbol2).
- #first_of!(*args) ⇒ Object
Instance Method Details
#first_of(*args) ⇒ Object
Should take arguments: first_of(:symbol1, :symbol2) # will return first respond to and present first_of([:symbol1, :symbol2]) # will return value if try_chain(:symbol1, :symbol2) present first_of(1 => :symbol1, 2 => :symbol2) # will prioritize trying by key and return first present first_of(1 => :symbol1, 2 => lambda { _call }) # will prioritize and only execute callable if first not present first_of(1 => [:symbol1, :symbol2], 2 => -> { _call }) # will prioritize and execute try_chain on first and call 2nd if first not present first_of(1 => :symbol1, 2 => 4) # will prioritize and return value (4) if first not present first_of(lambda { _call }, 1 => :symbol1, 2 => :symbol2)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/first_of.rb', line 18 def first_of(*args) extract_hash = args. # Don't care if the hash is empty or not because of #each call sorted_keys = extract_hash.keys.sort sorted_keys.each do |key| args << extract_hash[key] end args.each do |argument| if argument.respond_to?(:call) value = argument.call else value = (argument) # calls try_chain if array, or try if symbol, or returns value end return value if _valid_value?(value) # return value if found end return nil end |
#first_of!(*args) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/first_of.rb', line 40 def first_of!(*args) extract_hash = args. # Don't care if the hash is empty or not because of #each call sorted_keys = extract_hash.keys.sort sorted_keys.each do |key| args << extract_hash[key] end args.each do |argument| if argument.respond_to?(:call) value = argument.call else value = (argument, :try_chain!, :proxy_try_chain!) # calls try_chain if array, or try if symbol, or returns value end return value if _valid_value?(value) # return value if found end return nil end |