module Poise::Helpers::DefinedIn::ClassMethods

@!classmethods

Public Instance Methods

included(klass) click to toggle source
Calls superclass method
# File lib/poise/helpers/defined_in.rb, line 119
def included(klass)
  super
  klass.extend(ClassMethods)
  klass.poise_defined!(caller)
end
inherited(klass) click to toggle source

@api private

Calls superclass method
# File lib/poise/helpers/defined_in.rb, line 114
def inherited(klass)
  super
  klass.poise_defined!(caller)
end
poise_defined!(caller_array) click to toggle source

Record that the class/module was defined. Called automatically by Ruby for all normal cases.

@param caller_array [Array<String>] A strack trace returned by caller. @return [void]

# File lib/poise/helpers/defined_in.rb, line 95
def poise_defined!(caller_array)
  # Only try to set this once.
  return if @poise_defined_in
  # Parse out just the filenames.
  caller_paths = caller_array.map {|line| line[CALLER_REGEXP, 1] }
  # Find the first non-poise, non-chef line. This assumes Halite
  # transformation which I'm not thrilled about.
  caller_path = caller_paths.find do |line|
    line && !line.start_with?(POISE_LIB_ROOT) && !line.start_with?(CHEF_LIB_ROOT)
  end
  raise Poise::Error.new("Unable to find a caller path for: #{caller_array.inspect}") unless caller_path
  if ::File::ALT_SEPARATOR
    caller_path.gsub!(::File::ALT_SEPARATOR, ::File::SEPARATOR)
  end
  Chef::Log.debug("[#{self.name}] Recording poise_defined_in as #{caller_path}")
  @poise_defined_in = caller_path
end
poise_defined_in() click to toggle source

The file this class or module was defined in, or nil if it isn't found.

@return [String]

# File lib/poise/helpers/defined_in.rb, line 70
def poise_defined_in
  raise Poise::Error.new("Unable to determine location of #{self.name}") unless @poise_defined_in
  @poise_defined_in
end
poise_defined_in_cookbook(run_context, file=nil) click to toggle source

The cookbook this class or module was defined in. Can pass a file to check that instead.

@param run_context [Chef::RunContext] Run context to check cookbooks in. @param file [String, nil] Optional file path to check instead of the

path this class was defined in.

@return [String]

# File lib/poise/helpers/defined_in.rb, line 82
def poise_defined_in_cookbook(run_context, file=nil)
  file ||= poise_defined_in
  Poise.debug("[#{self.name}] Checking cookbook name for #{file}")
  Poise::Utils.find_cookbook_name(run_context, file).tap do |cookbook|
    Poise.debug("[#{self.name}] found cookbook #{cookbook.inspect}")
  end
end