Class: AndDistance

Inherits:
Object
  • Object
show all
Includes:
Loggable
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.



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
51
52
53
# File 'lib/glark/match/and_distance.rb', line 25

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.



23
24
25
# File 'lib/glark/match/and_distance.rb', line 23

def distance
  @distance
end

Instance Method Details

#numeric?(x) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/glark/match/and_distance.rb', line 55

def numeric? x
  x && (x.kind_of?(Fixnum) || x.to_i == INFINITE_DISTANCE || x.num)
end