Class: Snoopit::Sniffer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sniffer_params) ⇒ Sniffer

Returns a new instance of Sniffer.



6
7
8
9
10
11
12
13
14
15
# File 'lib/snoopit/sniffer.rb', line 6

def initialize(sniffer_params)
  @before  = sniffer_params['lines']['before'].nil? ? 2 : sniffer_params['lines']['before']
  @pre_before = Register.new @before
  @after   = sniffer_params['lines']['after'].nil? ? 2 : sniffer_params['lines']['after']
  @comment = sniffer_params['comment']
  @regexp  = Regexp.new sniffer_params['regexp']
  @notifiers = {}
  setup_notifiers sniffer_params
  @sniffed = []
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def before
  @before
end

#commentObject (readonly)

Returns the value of attribute comment.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def comment
  @comment
end

#notifiersObject (readonly)

Returns the value of attribute notifiers.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def notifiers
  @notifiers
end

#pre_beforeObject (readonly)

Returns the value of attribute pre_before.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def pre_before
  @pre_before
end

#regexpObject (readonly)

Returns the value of attribute regexp.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def regexp
  @regexp
end

#sniffedObject (readonly)

Returns the value of attribute sniffed.



4
5
6
# File 'lib/snoopit/sniffer.rb', line 4

def sniffed
  @sniffed
end

Instance Method Details

#as_json(options = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/snoopit/sniffer.rb', line 35

def as_json(options=nil)
  {
      before: @before,
      after: @after,
      comment: @comment,
      regexp: @regexp.to_json,
      sniffed: @sniffed,
      notifiers: @notifiers
  }
end

#setup_notifiers(params) ⇒ Object



17
18
19
# File 'lib/snoopit/sniffer.rb', line 17

def setup_notifiers(params)
  @notifiers = params['notify'] unless params['notify'].nil?
end

#to_json(*a) ⇒ Object



46
47
48
# File 'lib/snoopit/sniffer.rb', line 46

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

#track(file, line_no, line) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/snoopit/sniffer.rb', line 21

def track(file, line_no, line)
  matched = @regexp.match(line) do |m|
    @sniffed << Detected.new(@comment, @pre_before, @after, line, file, line_no)
  end
  @pre_before.push_front line if matched.nil?
  tracking line
end

#tracking(line) ⇒ Object



29
30
31
32
33
# File 'lib/snoopit/sniffer.rb', line 29

def tracking(line)
  @sniffed.each do |detected|
    detected.track line unless detected.finished?
  end
end