Class: Notes::Scanner
- Inherits:
-
Object
- Object
- Notes::Scanner
- Defined in:
- lib/notes/scanner.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
The block to execute when a note is found.
-
#tags ⇒ Array
The array of tags to look for.
Instance Method Summary collapse
-
#initialize(tags = nil, &block) ⇒ Scanner
constructor
Create a new scanner.
-
#on_note(&block) ⇒ Object
Define the callback to execute when a note is given.
-
#scan(source) ⇒ Object
Scan a source string.
-
#scan_file(path) ⇒ Object
Scan a file.
Constructor Details
#initialize(tags = nil, &block) ⇒ Scanner
Create a new scanner
37 38 39 40 |
# File 'lib/notes/scanner.rb', line 37 def initialize = nil, &block = || TAGS.dup @callback = block end |
Instance Attribute Details
#callback ⇒ Object
The block to execute when a note is found
19 20 21 |
# File 'lib/notes/scanner.rb', line 19 def callback @callback end |
#tags ⇒ Array
The array of tags to look for
11 12 13 |
# File 'lib/notes/scanner.rb', line 11 def end |
Instance Method Details
#on_note(&block) ⇒ Object
Define the callback to execute when a note is given
50 51 52 |
# File 'lib/notes/scanner.rb', line 50 def on_note &block @callback = block end |
#scan(source) ⇒ Object
Scan a source string
58 59 60 61 62 63 64 65 66 |
# File 'lib/notes/scanner.rb', line 58 def scan source return if .empty? || callback.nil? rxp = regexp source.split("\n").each_with_index do |line, i| if rxp =~ line callback.call Note.new($1, line, i + 1) end end end |
#scan_file(path) ⇒ Object
Scan a file
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/notes/scanner.rb', line 72 def scan_file path return if .empty? || callback.nil? rxp = regexp File.open(path, 'r') do |file| file.each_with_index do |line, i| if rxp =~ line callback.call Note.new($1, line, i + 1, path) end end end end |