Module: Poise::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/poise/utils.rb,
lib/poise/utils/resource_provider_mixin.rb

Defined Under Namespace

Modules: ResourceProviderMixin

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_cookbook_name(run_context, filename) ⇒ String

Find the cookbook name for a given filename. The can used to find the cookbook that corresponds to a caller of a file.

Examples:

def my_thing
  caller_filename = caller.first.split(':').first
  cookbook = Poise::Utils.find_cookbook_name(run_context, caller_filename)
  # ...
end

Parameters:

  • run_context (Chef::RunContext)

    Context to check.

  • filename (String)

    Absolute filename to check for.

Returns:

  • (String)

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/poise/utils.rb', line 38

def find_cookbook_name(run_context, filename)
  run_context.cookbook_collection.each do |name, ver|
    # This special method is added by Halite::Gem#as_cookbook_version.
    if ver.respond_to?(:halite_root)
      # The join is there because ../poise-ruby/lib starts with ../poise so
      # we want a trailing /.
      if filename.start_with?(File.join(ver.halite_root, ''))
        return name
      end
    else
      Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |seg|
        ver.segment_filenames(seg).each do |file|
          if file == filename
            return name
          end
        end
      end
    end
  end
  raise Poise::Error.new("Unable to find cookbook for file #{filename.inspect}")
end

Instance Method Details

#find_cookbook_name(run_context, filename) ⇒ String

Find the cookbook name for a given filename. The can used to find the cookbook that corresponds to a caller of a file.

Examples:

def my_thing
  caller_filename = caller.first.split(':').first
  cookbook = Poise::Utils.find_cookbook_name(run_context, caller_filename)
  # ...
end

Parameters:

  • run_context (Chef::RunContext)

    Context to check.

  • filename (String)

    Absolute filename to check for.

Returns:

  • (String)

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/poise/utils.rb', line 38

def find_cookbook_name(run_context, filename)
  run_context.cookbook_collection.each do |name, ver|
    # This special method is added by Halite::Gem#as_cookbook_version.
    if ver.respond_to?(:halite_root)
      # The join is there because ../poise-ruby/lib starts with ../poise so
      # we want a trailing /.
      if filename.start_with?(File.join(ver.halite_root, ''))
        return name
      end
    else
      Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |seg|
        ver.segment_filenames(seg).each do |file|
          if file == filename
            return name
          end
        end
      end
    end
  end
  raise Poise::Error.new("Unable to find cookbook for file #{filename.inspect}")
end