Class: Tagfu::Tagger
- Inherits:
-
Object
- Object
- Tagfu::Tagger
- Defined in:
- lib/tagfu/tagger.rb
Instance Method Summary collapse
- #add_tags(lines) ⇒ Object
- #delete_all(lines) ⇒ Object
- #delete_tags(lines) ⇒ Object
- #find_index_of_feature(lines) ⇒ Object
- #get_files ⇒ Object
-
#initialize(options = {}) ⇒ Tagger
constructor
A new instance of Tagger.
- #only_tag?(line, tag) ⇒ Boolean
- #process_file(file) ⇒ Object
- #sanitize_tags(tags) ⇒ Object
- #update_tags(lines) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Tagger
Returns a new instance of Tagger.
4 5 6 7 8 9 10 |
# File 'lib/tagfu/tagger.rb', line 4 def initialize(={}) @add_tags = [:add_tags] @update_tags = [:update_tags] @delete_tags = [:delete_tags] @delete_all = [:delete_all] @path = [:path] end |
Instance Method Details
#add_tags(lines) ⇒ Object
89 90 91 92 93 |
# File 'lib/tagfu/tagger.rb', line 89 def (lines) feature_index = find_index_of_feature(lines) indent = lines[feature_index][/^\s+/].to_s lines.insert(feature_index, indent + @add_tags.join(' ') + "\n") end |
#delete_all(lines) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tagfu/tagger.rb', line 56 def delete_all(lines) lines_to_delete = [] lines.each_with_index do |line, index| if line.match(/@/) lines_to_delete << index end end running_index = 0 lines_to_delete.each do |line_index| lines.delete_at(line_index - running_index) running_index += 1 end end |
#delete_tags(lines) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tagfu/tagger.rb', line 70 def (lines) @delete_tags.each do |tag| lines.each_with_index do |line, index| if only_tag?(line, tag) lines.delete_at(index) else indent = lines[index][/^\s+/].to_s line = line.strip next if line.empty? lines[index] = indent + line.chomp.split(' ').delete_if {|word| word.match(/#{tag}/)}.join(' ') + "\n" if line.match(/^@/) end end end end |
#find_index_of_feature(lines) ⇒ Object
95 96 97 98 99 |
# File 'lib/tagfu/tagger.rb', line 95 def find_index_of_feature(lines) lines.each_with_index do |line, index| return index if line.strip.match(/^Feature:/) end end |
#get_files ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tagfu/tagger.rb', line 16 def get_files if File.directory?(@path) current_working_dir = Dir.pwd Dir.chdir(@path) files = Dir.glob("**/*.feature") raise ArgumentError, "No cucumber feature files in specified path" if files.empty? files.each {|file| process_file(file)} Dir.chdir(current_working_dir) elsif File.file?(@path) process_file(@path) else raise ArgumentError, "Path specified is either invalid or not a file/directory" end end |
#only_tag?(line, tag) ⇒ Boolean
85 86 87 |
# File 'lib/tagfu/tagger.rb', line 85 def only_tag?(line, tag) return line.strip == tag end |
#process_file(file) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tagfu/tagger.rb', line 31 def process_file(file) lines = File.readlines(file) if @delete_all delete_all(lines) end unless @delete_tags.nil? (@delete_tags) (lines) end unless @add_tags.nil? (@add_tags) (lines) end unless @update_tags.nil? (@update_tags) @update_tags = {:from_tag => @update_tags.first, :to_tag => @update_tags.last} (lines) end File.open(file, 'w') do |fh| lines.each do |line| fh.write line end end end |
#sanitize_tags(tags) ⇒ Object
12 13 14 |
# File 'lib/tagfu/tagger.rb', line 12 def () .collect! {|tag| tag.match(/^@/) ? tag : "@#{tag}"} end |
#update_tags(lines) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tagfu/tagger.rb', line 101 def (lines) lines.each_with_index do |line, index| indent = lines[index][/^\s+/].to_s if only_tag?(line, @update_tags[:from_tag]) lines.delete_at(index) lines.insert(index, indent + "#{@update_tags[:to_tag]}\n") else line = line.strip next if line.empty? lines[index] = indent + line.chomp.split(' ').map! {|word| word == "#{@update_tags[:from_tag]}" ? "#{@update_tags[:to_tag]}" : word}.join(' ') + "\n" if line.match(/^@/) end end end |