class Chef::Resource::LWRPBase

Chef::Resource::LWRPBase

Base class for LWRP resources. Adds DSL sugar on top of Chef::Resource, so attributes, default action, etc. can be defined with pleasing syntax.

Attributes

loaded_lwrps[W]
run_context[RW]

Set the run context on the class. Used to provide access to the node during class definition.

Public Class Methods

actions(*action_names) click to toggle source

Adds action_names to the list of valid actions for this resource. Does not include superclass's action list when appending.

# File lib/chef/resource/lwrp_base.rb, line 79
def actions(*action_names)
  action_names = action_names.flatten
  if !action_names.empty? && !@allowed_actions
    self.allowed_actions = ([ :nothing ] + action_names).uniq
  else
    allowed_actions(*action_names)
  end
end
build_from_file(cookbook_name, filename, run_context) click to toggle source
# File lib/chef/resource/lwrp_base.rb, line 44
def build_from_file(cookbook_name, filename, run_context)
  if LWRPBase.loaded_lwrps[filename]
    Chef::Log.trace("Custom resource #{filename} from cookbook #{cookbook_name} has already been loaded!  Skipping the reload.")
    return loaded_lwrps[filename]
  end

  resource_name = filename_to_qualified_string(cookbook_name, filename)

  resource_class = Class.new(self)
  resource_class.run_context = run_context
  resource_class.class_from_file(filename)

  # Make a useful string for the class (rather than <Class:312894723894>)
  resource_class.instance_eval do
    define_singleton_method(:to_s) do
      "Custom resource #{resource_name} from cookbook #{cookbook_name}"
    end
    define_singleton_method(:inspect) { to_s }
  end

  Chef::Log.trace("Loaded contents of #{filename} into resource #{resource_name} (#{resource_class})")

  LWRPBase.loaded_lwrps[filename] = true

  # wire up the default resource name after the class is parsed only if we haven't declared one.
  # (this ordering is important for MapCollision deprecation warnings)
  resource_class.resource_name resource_name.to_sym if resource_class.resource_name.nil?

  resource_class
end
node() click to toggle source
# File lib/chef/resource/lwrp_base.rb, line 99
def node
  run_context ? run_context.node : nil
end
valid_actions(*args) click to toggle source

@deprecated

# File lib/chef/resource/lwrp_base.rb, line 90
def valid_actions(*args)
  Chef::Log.warn("`valid_actions' is deprecated, please use allowed_actions `instead'!")
  allowed_actions(*args)
end

Protected Class Methods

loaded_lwrps() click to toggle source
# File lib/chef/resource/lwrp_base.rb, line 106
def loaded_lwrps
  @loaded_lwrps ||= {}
end

Private Class Methods

from_superclass(m, default = nil) click to toggle source

Get the value from the superclass, if it responds, otherwise return nil. Since class instance variables are *not* inherited upon subclassing, this is a required check to ensure Chef pulls the default_action and other DSL-y methods when extending LWRP::Base.

# File lib/chef/resource/lwrp_base.rb, line 116
def from_superclass(m, default = nil)
  return default if superclass == Chef::Resource::LWRPBase
  superclass.respond_to?(m) ? superclass.send(m) : default
end