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



89
90
91
92
93
# File 'lib/tagfu/tagger.rb', line 89

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
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 delete_tags(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_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

#only_tag?(line, tag) ⇒ Boolean

Returns:

  • (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?
		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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tagfu/tagger.rb', line 101

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