Class: AndDistance

Inherits:
Object
  • Object
show all
Defined in:
lib/glark/match/and_distance.rb

Constant Summary collapse

INFINITE_DISTANCE =

signifies no limit to the distance between matches, i.e., anywhere within the entire file is valid.

-1
AND_EQ_NUM_RE =
Regexp.new '^--and=(\-?\d+)?$'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, args) ⇒ AndDistance

Returns a new instance of AndDistance.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/glark/match/and_distance.rb', line 22

def initialize arg, args
  @distance = if arg == "-a"
                args.shift
              elsif arg == "--and"
                if args.size > 0 && numeric?(args[0])
                  args.shift
                else
                  "0"
                end
              elsif md = AND_EQ_NUM_RE.match(arg)
                md[1]
              else
                raise "invalid 'and' option: '#{arg}'"
              end
  
  # check to ensure that this is numeric
  if !numeric? @distance
    raise "invalid distance for 'and' expression: '#{@distance}'\n" +
      "    expecting an integer, or #{INFINITE_DISTANCE} for 'infinite'" 
  end
  
  if @distance.to_i == INFINITE_DISTANCE
    @distance = 1.0 / 0.0            # infinity
  else
    @distance = @distance.to_i
  end

  @distance
end

Instance Attribute Details

#distanceObject (readonly)

Returns the value of attribute distance.



20
21
22
# File 'lib/glark/match/and_distance.rb', line 20

def distance
  @distance
end

Instance Method Details

#numeric?(x) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
# File 'lib/glark/match/and_distance.rb', line 52

def numeric? x
  return nil unless x
  return true if x.kind_of?(Fixnum) || x.to_i == INFINITE_DISTANCE
  begin
    Integer x
  rescue ArgumentError
    nil
  end
end