Class: SourceAnnotationExtractor

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

Defined Under Namespace

Classes: Annotation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ SourceAnnotationExtractor

Returns a new instance of SourceAnnotationExtractor.



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

def initialize(tag)
  @tag = tag
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



15
16
17
# File 'lib/source_annotation_extractor.rb', line 15

def tag
  @tag
end

Class Method Details

.enumerate(tag, options = {}) ⇒ Object



10
11
12
13
# File 'lib/source_annotation_extractor.rb', line 10

def self.enumerate(tag, options={})
  extractor = new(tag)
  extractor.display(extractor.find, options)
end

Instance Method Details

#display(results, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/source_annotation_extractor.rb', line 53

def display(results, options={})
  results.keys.sort.each do |file|
    puts "#{file}:"
    results[file].each do |note|
      puts "  * #{note.to_s(options)}"
    end
    puts
  end
end

#extract_annotations_from(file, pattern) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/source_annotation_extractor.rb', line 43

def extract_annotations_from(file, pattern)
  lineno = 0
  result = File.readlines(file).inject([]) do |list, line|
    lineno += 1
    next list unless line =~ pattern
    list << Annotation.new(lineno, $1, $2)
  end
  result.empty? ? {} : { file => result }
end

#find(dirs = %w(app lib test)) ⇒ Object



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

def find(dirs=%w(app lib test))
  dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
end

#find_in(dir) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/source_annotation_extractor.rb', line 25

def find_in(dir)
  results = {}

  Dir.glob("#{dir}/*") do |item|
    next if File.basename(item)[0] == ?.

    if File.directory?(item)
      results.update(find_in(item))
    elsif item =~ /\.(builder|(r(?:b|xml|js)))$/
      results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/))
    elsif item =~ /\.(rhtml|erb)$/
      results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/))
    end
  end

  results
end