1require 'mspec/runner/actions/filter'
2
3# TagListAction - prints out the descriptions for any specs
4# tagged with +tags+. If +tags+ is an empty list, prints out
5# descriptions for any specs that are tagged.
6class TagListAction
7  def initialize(tags=nil)
8    @tags = tags.nil? || tags.empty? ? nil : Array(tags)
9    @filter = nil
10  end
11
12  # Returns true. This enables us to match any tag when loading
13  # tags from the file.
14  def include?(arg)
15    true
16  end
17
18  # Returns true if any tagged descriptions matches +string+.
19  def ===(string)
20    @filter === string
21  end
22
23  # Prints a banner about matching tagged specs.
24  def start
25    if @tags
26      print "\nListing specs tagged with #{@tags.map { |t| "'#{t}'" }.join(", ") }\n\n"
27    else
28      print "\nListing all tagged specs\n\n"
29    end
30  end
31
32  # Creates a MatchFilter for specific tags or for all tags.
33  def load
34    @filter = nil
35    desc = MSpec.read_tags(@tags || self).map { |t| t.description }
36    @filter = MatchFilter.new(nil, *desc) unless desc.empty?
37  end
38
39  # Prints the spec description if it matches the filter.
40  def after(state)
41    return unless self === state.description
42    print state.description, "\n"
43  end
44
45  def register
46    MSpec.register :start, self
47    MSpec.register :load,  self
48    MSpec.register :after, self
49  end
50
51  def unregister
52    MSpec.unregister :start, self
53    MSpec.unregister :load,  self
54    MSpec.unregister :after, self
55  end
56end
57