Class: Chirp::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/chirp/span.rb

Constant Summary collapse

BEFORE_SPAN =
0
IN_SPAN =
1
AFTER_SPAN =
2

Instance Method Summary collapse

Constructor Details

#initialize(v1, v2) ⇒ Span

Returns a new instance of Span.



7
8
9
10
11
12
# File 'lib/chirp/span.rb', line 7

def initialize(v1, v2)
  v2 = 999999999999 unless v2
  @p1 = predicate(v1)
  @p2 = predicate(v2)
  reset
end

Instance Method Details

#evaluate(context) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/chirp/span.rb', line 18

def evaluate(context)
  if @state == BEFORE_SPAN
    @state = IN_SPAN if @p1.call(context)
  end
  ret = @state == IN_SPAN
  if @state == IN_SPAN
    @state = AFTER_SPAN if @p2.call(context)
  end
  ret
end

#predicate(value) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/chirp/span.rb', line 29

def predicate(value)
  if value.kind_of?(Regexp)
    return lambda {|context| value =~ context.line}
  else
    return lambda {|context| value == context.line_no}
  end
end

#resetObject



14
15
16
# File 'lib/chirp/span.rb', line 14

def reset
  @state = BEFORE_SPAN
end