Module: Poise::Helpers::DefinedIn

Included in:
Inversion::Provider, Provider, Resource
Defined in:
lib/poise/helpers/defined_in.rb

Overview

A mixin to track where a resource or provider was defined. This can provide either the filename of the class or the cookbook it was defined in.

Examples:

class MyProvider < Chef::provider
  include Poise::Helpers::DefinedIn

  def action_create
    template '...' do
      # ...
      cookbook new_resource.poise_defined_in
    end
  end
end

Since:

  • 2.0.0

Class Method Summary collapse

Class Method Details

.poise_defined!(caller_array)

This method returns an undefined value.

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

Parameters:

  • caller_array (Array<String>)

    A strack trace returned by #caller.

Since:

  • 2.0.0



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/poise/helpers/defined_in.rb', line 79

def poise_defined!(caller_array)
  # Only try to set this once.
  return if @poise_defined_in
  # Path to ignore, assumes Halite transformation which I'm not thrilled
  # about.
  poise_libraries = File.expand_path('../..', __FILE__)
  # Parse out just the filenames.
  caller_array = caller_array.map {|line| line.split(/:/, 2).first }
  # Find the first non-poise line.
  caller_path = caller_array.find do |line|
    !line.start_with?(poise_libraries)
  end
  Chef::Log.debug("[#{self.name}] Recording poise_defined_in as #{caller_path}")
  @poise_defined_in = caller_path
end

.poise_defined_inString

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

Returns:

  • (String)

Raises:

Since:

  • 2.0.0



54
55
56
57
# File 'lib/poise/helpers/defined_in.rb', line 54

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) ⇒ String

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

Parameters:

  • run_context (Chef::RunContext)

    Run context to check cookbooks in.

  • file (String, nil) (defaults to: nil)

    Optional file path to check instead of the path this class was defined in.

Returns:

  • (String)

Since:

  • 2.0.0



66
67
68
69
70
71
72
# File 'lib/poise/helpers/defined_in.rb', line 66

def poise_defined_in_cookbook(run_context, file=nil)
  file ||= poise_defined_in
  Chef::Log.debug("[#{self.name}] Checking cookbook name for #{file}")
  Poise::Utils.find_cookbook_name(run_context, file).tap do |cookbook|
    Chef::Log.debug("[#{self.name}] found cookbook #{cookbook.inspect}")
  end
end