1require 'mspec/runner/filters/match'
2
3# ActionFilter is a base class for actions that are triggered by
4# specs that match the filter. The filter may be specified by
5# strings that match spec descriptions or by tags for strings
6# that match spec descriptions.
7#
8# Unlike TagFilter and RegexpFilter, ActionFilter instances do
9# not affect the specs that are run. The filter is only used to
10# trigger the action.
11
12class ActionFilter
13  def initialize(tags=nil, descs=nil)
14    @tags = Array(tags)
15    descs = Array(descs)
16    @sfilter = descs.empty? ? nil : MatchFilter.new(nil, *descs)
17    @tfilter = nil
18  end
19
20  def ===(string)
21    @sfilter === string or @tfilter === string
22  end
23
24  def load
25    return if @tags.empty?
26
27    desc = MSpec.read_tags(@tags).map { |t| t.description }
28    return if desc.empty?
29
30    @tfilter = MatchFilter.new(nil, *desc)
31  end
32
33  def register
34    MSpec.register :load, self
35  end
36
37  def unregister
38    MSpec.unregister :load, self
39  end
40end
41