Class: TagPurgeAction

Inherits:
TagListAction show all
Defined in:
lib/extensions/mspec/mspec/runner/actions/tagpurge.rb

Overview

TagPurgeAction - removes all tags not matching any spec descriptions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TagListAction

#===, #include?

Constructor Details

#initializeTagPurgeAction

Returns a new instance of TagPurgeAction.



9
10
11
12
13
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 9

def initialize
  @matching = []
  @filter   = nil
  @tags     = nil
end

Instance Attribute Details

#matchingObject (readonly)

Returns the value of attribute matching.



7
8
9
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 7

def matching
  @matching
end

Instance Method Details

#after(state) ⇒ Object

Saves any matching tags



29
30
31
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 29

def after(state)
  @matching << state.description if self === state.description
end

#loadObject

Creates a MatchFilter for all tags.



21
22
23
24
25
26
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 21

def load
  @filter = nil
  @tags = MSpec.read_tags self
  desc = @tags.map { |t| t.description }
  @filter = MatchFilter.new(nil, *desc) unless desc.empty?
end

#registerObject



47
48
49
50
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 47

def register
  super
  MSpec.register :unload, self
end

#startObject

Prints a banner about purging tags.



16
17
18
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 16

def start
  print "\nRemoving tags not matching any specs\n\n"
end

#unloadObject

Rewrites any matching tags. Prints non-matching tags. Deletes the tag file if there were no tags (this cleans up empty or malformed tag files).



36
37
38
39
40
41
42
43
44
45
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 36

def unload
  if @filter
    matched = @tags.select { |t| @matching.any? { |s| s == t.description } }
    MSpec.write_tags matched

    (@tags - matched).each { |t| print t.description, "\n" }
  else
    MSpec.delete_tags
  end
end

#unregisterObject



52
53
54
55
# File 'lib/extensions/mspec/mspec/runner/actions/tagpurge.rb', line 52

def unregister
  super
  MSpec.unregister :unload, self
end