Class: Snoopit::Detected

Inherits:
Object
  • Object
show all
Defined in:
lib/snoopit/detected.rb

Overview

When the sniffer detects a match it saves the contextual information in a Detected instances

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment, pre_before, after, match, file, line_no) ⇒ Detected

These are parameters from the match



9
10
11
12
13
14
15
16
17
18
# File 'lib/snoopit/detected.rb', line 9

def initialize(comment, pre_before, after, match, file, line_no)
  setup_before pre_before
  @comment     = comment
  @after_count = 0
  @after       = Register.new(after)
  @match       = match
  @file        = file
  @line_no     = line_no
  @finished    = false
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def after
  @after
end

#after_countObject (readonly)

Returns the value of attribute after_count.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def after_count
  @after_count
end

#beforeObject (readonly)

Returns the value of attribute before.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def before
  @before
end

#commentObject (readonly)

Returns the value of attribute comment.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def comment
  @comment
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def file
  @file
end

#finishedObject (readonly)

Returns the value of attribute finished.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def finished
  @finished
end

#line_noObject (readonly)

Returns the value of attribute line_no.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def line_no
  @line_no
end

#matchObject (readonly)

Returns the value of attribute match.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def match
  @match
end

#regexpObject (readonly)

Returns the value of attribute regexp.



6
7
8
# File 'lib/snoopit/detected.rb', line 6

def regexp
  @regexp
end

Instance Method Details

#as_json(options = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/snoopit/detected.rb', line 40

def as_json(options=nil)
  {
      after: @after,
      match: @match,
      before: @before,
      file: @file,
      line_no: @line_no
  }
end

#finished?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/snoopit/detected.rb', line 36

def finished?
  @finished
end

#setup_before(pre_before) ⇒ Object

Get the lines before the found match



21
22
23
# File 'lib/snoopit/detected.rb', line 21

def setup_before(pre_before)
  @before = Register.new pre_before.size, pre_before.register
end

#to_json(*a) ⇒ Object



50
51
52
# File 'lib/snoopit/detected.rb', line 50

def to_json(*a)
  as_json.to_json(*a)
end

#track(line) ⇒ Object

This collects the lines after a match is found



26
27
28
29
30
31
32
33
34
# File 'lib/snoopit/detected.rb', line 26

def track(line)
  return if line == @match
  if @after_count < @after.size
    @after.push_front line
    @after_count += 1
  else
    @finished = true
  end
end