Class: Tagfu::Tagger

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

Instance Method Summary collapse

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(options={})
	@add_tags    = options[:add_tags]
	@update_tags = options[:update_tags]
	@delete_tags = options[:delete_tags]
	@delete_all  = options[:delete_all]
	@path        = options[:path]
end

Instance Method Details

#add_tags(lines) ⇒ Object



101
102
103
104
105
# File 'lib/tagfu/tagger.rb', line 101

def add_tags(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
# File 'lib/tagfu/tagger.rb', line 56

def delete_all(lines)
	lines_to_delete = lines_to_delete(lines, "@")
	delete_lines(lines, lines_to_delete)
end

#delete_lines(lines, lines_to_delete) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/tagfu/tagger.rb', line 71

def delete_lines(lines, lines_to_delete)
	lines_to_delete = lines_to_delete.sort
	index_adjustment = 0
	lines_to_delete.each do |line_index|
		lines.delete_at(line_index - index_adjustment)
		index_adjustment += 1
	end
end

#delete_tags(lines) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tagfu/tagger.rb', line 80

def delete_tags(lines)
	lines_to_delete = []
	@delete_tags.each do |tag|
		lines.each_with_index do |line, line_index|
			if only_tag?(line, tag)
				lines_to_delete << line_index
			else	
				indent = lines[line_index][/^\s+/].to_s
				line = line.strip
				next if line.empty?
				lines[line_index] = indent + line.chomp.split(' ').delete_if {|word| word.match(/#{tag}/)}.join(' ') + "\n" if line.match(/^@/)
			end
		end
	end
	delete_lines(lines, lines_to_delete.flatten)
end

#find_index_of_feature(lines) ⇒ Object



107
108
109
110
111
# File 'lib/tagfu/tagger.rb', line 107

def find_index_of_feature(lines)
	lines.each_with_index do |line, index|
		return index if line.strip.match(/^Feature:/)
	end
end

#get_filesObject



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

#lines_to_delete(lines, regex) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/tagfu/tagger.rb', line 61

def lines_to_delete(lines, regex)
	tmp = []
	lines.each_with_index do |line, index|
		if line.match(/#{regex}/)
			tmp << index
		end
	end
	tmp
end

#only_tag?(line, tag) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/tagfu/tagger.rb', line 97

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?
		sanitize_tags(@delete_tags)
		delete_tags(lines)
	end
	unless @add_tags.nil?
		sanitize_tags(@add_tags)
		add_tags(lines)
	end
	unless @update_tags.nil?
		sanitize_tags(@update_tags)
		@update_tags = {:from_tag => @update_tags.first, :to_tag => @update_tags.last}
		update_tags(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 sanitize_tags(tags)
	tags.collect! {|tag| tag.match(/^@/) ? tag : "@#{tag}"}
end

#update_tags(lines) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tagfu/tagger.rb', line 113

def update_tags(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