class EcomDev::ChefSpec::Resource::Matcher

Attributes

matchers[R]
possible_directories[RW]
runners[R]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

extend_api() click to toggle source
# 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

  runners.each do |runner|
    ::ChefSpec.define_matcher(runner.to_sym)
  end
end
load_matcher_file(file) click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 81
def load_matcher_file(file)
  DSL.load(file)
end
load_matchers() click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 76
def load_matchers
  files = search_patterns.map { |pattern| Dir.glob(pattern) }.flatten
  files.each {|file| load_matcher_file(file) }
end
matcher(resource_name, action) click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 52
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
register() click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 103
def register
  EcomDev::ChefSpec::Configuration.callback(self.class.instance)

  RSpec.configure do |config|
    config.include Helper
  end
end
runner(resource_name) click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 66
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_patterns() click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 85
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!() click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 29
def setup!
  load_matchers
  extend_api
end
teardown!() click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 34
def teardown!
  @matchers = {}
  @runners = []
  Helper.reset
end

Private Instance Methods

add_matcher(resource, action) click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 112
def add_matcher(resource, action)
  resource_name = resource.to_s
  action_name = action.to_s
  matcher_name = action_name + '_' + resource_name
  matcher = matcher_name.to_sym
  unless @matchers.key?(matcher)
    @matchers[matcher] = {action: action_name.to_sym, resource: resource_name.to_sym}
  end
end
add_runner(resource_name) click to toggle source
# File lib/ecomdev/chefspec/resource/matcher.rb, line 122
def add_runner(resource_name)
  resource = resource_name.to_sym
  @runners << resource unless @runners.include?(resource)
end