Class: Bonobot::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/bonobot/annotator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, fingerprint) ⇒ Annotator

Returns a new instance of Annotator.



13
14
15
16
# File 'lib/bonobot/annotator.rb', line 13

def initialize(path, fingerprint)
  @path = path
  @fingerprint = fingerprint
end

Class Method Details

.add_annotation(path, fingerprint) ⇒ Object



5
6
7
# File 'lib/bonobot/annotator.rb', line 5

def self.add_annotation(path, fingerprint)
  new(path, fingerprint).add_annotation
end

.update_annotation(path, fingerprint) ⇒ Object



9
10
11
# File 'lib/bonobot/annotator.rb', line 9

def self.update_annotation(path, fingerprint)
  new(path, fingerprint).update_annotation
end

Instance Method Details

#add_annotationObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bonobot/annotator.rb', line 18

def add_annotation
  f = File.read(@path).split("\n")

  if f.first == "# frozen_string_literal: true"
    f.insert(1, "\n#{annotation}")
  else
    f.insert(0, annotation)
  end

  File.write(@path, "#{f.join("\n")}\n")
end

#annotationObject



40
41
42
43
44
45
46
# File 'lib/bonobot/annotator.rb', line 40

def annotation
  if @path.to_s.end_with?(".erb")
    "<%# bonobot_fingerprint: #{@fingerprint} %>\n"
  else
    "# bonobot_fingerprint: #{@fingerprint}"
  end
end

#match_line(line) ⇒ Object



48
49
50
# File 'lib/bonobot/annotator.rb', line 48

def match_line(line)
  line.include?("# bonobot_fingerprint") || line.include?("<%# bonobot_fingerprint")
end

#update_annotationObject



30
31
32
33
34
35
36
37
38
# File 'lib/bonobot/annotator.rb', line 30

def update_annotation
  f = File.read(@path).split("\n")

  f.each_with_index do |line, index|
    f[index] = annotation if match_line(line)
  end

  File.write(@path, "#{f.join("\n")}\n")
end