Class: EcomDev::ChefSpec::Resource::Matcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/ecomdev/chefspec/resource/matcher.rb,
lib/ecomdev/chefspec/resource/matcher/dsl.rb,
lib/ecomdev/chefspec/resource/matcher/helper.rb

Defined Under Namespace

Modules: Helper Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatcher

Returns a new instance of Matcher.



18
19
20
21
22
23
24
25
26
27
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 18

def initialize
  @matchers = {}
  @runners = []
  @possible_directories = [
      File.join('spec', 'matchers.rb'),
      File.join('spec', 'matchers', '*.rb'),
      File.join('spec', 'libraries', 'matchers.rb'),
      File.join('spec', 'libraries', 'matchers/*.rb')
  ]
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



15
16
17
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 15

def matchers
  @matchers
end

#possible_directoriesObject

Returns the value of attribute possible_directories.



16
17
18
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 16

def possible_directories
  @possible_directories
end

#runnersObject (readonly)

Returns the value of attribute runners.



15
16
17
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 15

def runners
  @runners
end

Instance Method Details

#extend_apiObject



40
41
42
43
44
45
46
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 40

def extend_api
  matchers.each do |method, info|
    Helper.add(method) do |identity|
      ::ChefSpec::Matchers::ResourceMatcher.new(info[:resource], info[:action], identity)
    end
  end
end

#load_matcher_file(file) ⇒ Object



77
78
79
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 77

def load_matcher_file(file)
  DSL.load(file)
end

#load_matchersObject



72
73
74
75
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 72

def load_matchers
  files = search_patterns.map { |pattern| Dir.glob(pattern) }.flatten
  files.each {|file| load_matcher_file(file) }
end

#matcher(resource_name, action) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 48

def matcher(resource_name, action)
  if resource_name.is_a?(Array)
    resource_name.each do |r|
      matcher(r, action)
    end
  elsif action.is_a?(Array)
    action.each do |a|
      matcher(resource_name, a)
    end
  else
    add_matcher(resource_name, action)
  end
end

#registerObject



99
100
101
102
103
104
105
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 99

def register
  EcomDev::ChefSpec::Configuration.callback(self.class.instance)

  RSpec.configure do |config|
    config.include Helper
  end
end

#runner(resource_name) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 62

def runner(resource_name)
  if resource_name.is_a?(Array)
    resource_name.each do |r|
      runner(r)
    end
  else
    add_runner(resource_name)
  end
end

#search_patternsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 81

def search_patterns
  cookbook_path = RSpec.configuration.cookbook_path
  if cookbook_path.nil? ||
      cookbook_path.is_a?(String) && cookbook_path.empty?
    return []
  end

  cookbook_path = [cookbook_path] if cookbook_path.is_a?(String)

  search_patterns = []
  possible_directories.each do |directory|
    cookbook_path.each do |cookbook_path|
      search_patterns << File.join(cookbook_path, '*', directory)
    end
  end
  search_patterns
end

#setup!Object



29
30
31
32
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 29

def setup!
  load_matchers
  extend_api
end

#teardown!Object



34
35
36
37
38
# File 'lib/ecomdev/chefspec/resource/matcher.rb', line 34

def teardown!
  @matchers = {}
  @runners = []
  Helper.reset
end