chef / 17.9.18 / helpers / index.html /

Helpers

[edit on GitHub]

Helper classes enable users to share code between Custom Resources. Rspec method test are possible when code is abstracted into plain Ruby files.

Example

To include the following helper from libraries/helpers.rb

#libraries/helpers.rb
module Haproxy
  module cookbook
    module ResourceHelpers
      def haproxy_version
        version = Mixlib::ShellOut.new("haproxy -v | grep version | awk '{ print $3 }'")
        version.run_command.stdout.to_f
      end
    end
  end
end

Within a Custom Resource file, use the include directive in the action_class, to include all methods in the specified module.

action_class do
  include Haproxy::Cookbook::ResourceHelpers
end

© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/helpers/