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.
Constant Summary collapse
- POISE_LIB_ROOT =
Path to the root of Poise's code.
::File.('../..', __FILE__)
- CHEF_LIB_ROOT =
Path to the root of Chef's code.
::Gem::Specification.find_by_name('chef').gem_dir
Class Method Summary collapse
-
.poise_defined!(caller_array)
Record that the class/module was defined.
-
.poise_defined_in ⇒ String
The file this class or module was defined in, or nil if it isn't found.
-
.poise_defined_in_cookbook(run_context, file = nil) ⇒ String
The cookbook this class or module was defined in.
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.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/poise/helpers/defined_in.rb', line 87 def poise_defined!(caller_array) # Only try to set this once. return if @poise_defined_in # Parse out just the filenames. caller_array = caller_array.map {|line| line.split(/:/, 2).first } # Find the first non-poise, non-chef line. This assumes Halite # transformation which I'm not thrilled about. caller_path = caller_array.find do |line| !line.start_with?(POISE_LIB_ROOT) && !line.start_with?(CHEF_LIB_ROOT) end Chef::Log.debug("[#{self.name}] Recording poise_defined_in as #{caller_path}") @poise_defined_in = caller_path end |
.poise_defined_in ⇒ String
The file this class or module was defined in, or nil if it isn't found.
62 63 64 65 |
# File 'lib/poise/helpers/defined_in.rb', line 62 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.
74 75 76 77 78 79 80 |
# File 'lib/poise/helpers/defined_in.rb', line 74 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 |