class Speculate::Args
Constants
- FLAG_RGX
- KEYWORD_RGX
Attributes
args[R]
Public Class Methods
new(with)
click to toggle source
# File lib/speculate/args.rb, line 65 def initialize with @args = with.dup end
Public Instance Methods
empty?()
click to toggle source
# File lib/speculate/args.rb, line 8 def empty?; args.empty? end
fetch(symbol, default=nil, &block)
click to toggle source
# File lib/speculate/args.rb, line 10 def fetch symbol, default=nil, &block index = args.index("#{symbol}:") return default unless index args.fetch( index.succ, default ).tap do |value| return block.(value) if block end end
fetch!(symbol, default=nil, &block)
click to toggle source
# File lib/speculate/args.rb, line 18 def fetch! symbol, default=nil, &block index = args.index("#{symbol}:") return default unless index args.delete_at index args.fetch( index ) { return default }.tap{ |value| args.delete_at index return block.(value) if block } end
flag!(value)
click to toggle source
# File lib/speculate/args.rb, line 34 def flag! value flag?(value).tap do |idx| return unless idx args.delete_at(idx) end end
flag?(value)
click to toggle source
# File lib/speculate/args.rb, line 30 def flag? value args.index ":#{value}" end
positionals()
click to toggle source
# File lib/speculate/args.rb, line 41 def positionals args.inject [false, []] do |(last_was_keyword, result), ele| if last_was_keyword [false, result] else case ele when KEYWORD_RGX [true, result] when FLAG_RGX [false, result] else [false, result << ele] end end end.last end
to_a()
click to toggle source
# File lib/speculate/args.rb, line 58 def to_a args.dup end
Private Instance Methods
_flag(arg)
click to toggle source
# File lib/speculate/args.rb, line 69 def _flag arg FLAG_RGX.match? arg end
_keyword?(arg)
click to toggle source
# File lib/speculate/args.rb, line 73 def _keyword? arg KEYWORD_RGX.match arg end