Module: Lab42::Rgxargs::PredefinedMatchers

Extended by:
PredefinedMatchers
Included in:
PredefinedMatchers
Defined in:
lib/lab42/rgxargs/predefined_matchers.rb

Constant Summary collapse

PREDEFINED =
{
  existing_dirs: [%r{(.+)}, ->(glob){ Dir.glob(glob).filter{ File.directory?(_1) } }],
  int: [%r{\A([-+]?\d+)\z}, :to_i.to_proc],
  int_list: [%r{\A(-?\d+(?:,-?\d+)*)\z}, ->(*groups){ groups.first.split(",").map(&:to_i)}],
  int_range: [%r{\A(-?\d+)(?:-|\.\.)(-?\d+)\z}, ->(f, l){ Range.new(f.to_i, l.to_i) }],
  list:  [%r{(\w+)(?:,(\w+))*},   ->(*groups){ groups.compact }],
  range: [%r{\A(\d+)\.\.(\d+)\z}, ->(*groups){ Range.new(*groups.map(&:to_i)) }]
}

Instance Method Summary collapse

Instance Method Details

#_list_extractor(*groups) ⇒ Object



25
26
27
# File 'lib/lab42/rgxargs/predefined_matchers.rb', line 25

def _list_extractor(*groups)
  groups.first.split(",")
end

#defined_namesObject



11
12
13
# File 'lib/lab42/rgxargs/predefined_matchers.rb', line 11

def defined_names
   @__defined_names__ ||= PREDEFINED.keys.join("\n\t")
end

#fetch(key, default = nil, &blk) ⇒ Object



15
16
17
18
# File 'lib/lab42/rgxargs/predefined_matchers.rb', line 15

def fetch(key, default=nil, &blk)
  return PREDEFINED[key] if PREDEFINED.has_key?(key)
  blk ? blk.(key) : default
end

#list_matcher(values) ⇒ Object



20
21
22
# File 'lib/lab42/rgxargs/predefined_matchers.rb', line 20

def list_matcher values
  [%r{\A((?:#{values.join("|")})(?:,(?:#{values.join("|")}))*)\z}, method(:_list_extractor)]
end