1require 'mspec/runner/actions/filter' 2require 'mspec/runner/actions/taglist' 3 4# TagPurgeAction - removes all tags not matching any spec 5# descriptions. 6class TagPurgeAction < TagListAction 7 attr_reader :matching 8 9 def initialize 10 @matching = [] 11 @filter = nil 12 @tags = nil 13 end 14 15 # Prints a banner about purging tags. 16 def start 17 print "\nRemoving tags not matching any specs\n\n" 18 end 19 20 # Creates a MatchFilter for all tags. 21 def load 22 @filter = nil 23 @tags = MSpec.read_tags self 24 desc = @tags.map { |t| t.description } 25 @filter = MatchFilter.new(nil, *desc) unless desc.empty? 26 end 27 28 # Saves any matching tags 29 def after(state) 30 @matching << state.description if self === state.description 31 end 32 33 # Rewrites any matching tags. Prints non-matching tags. 34 # Deletes the tag file if there were no tags (this cleans 35 # up empty or malformed tag files). 36 def unload 37 if @filter 38 matched = @tags.select { |t| @matching.any? { |s| s == t.description } } 39 MSpec.write_tags matched 40 41 (@tags - matched).each { |t| print t.description, "\n" } 42 else 43 MSpec.delete_tags 44 end 45 end 46 47 def register 48 super 49 MSpec.register :unload, self 50 end 51 52 def unregister 53 super 54 MSpec.unregister :unload, self 55 end 56end 57