class ChefCompat::CopiedFromChef::Chef::Resource

Constants

FORBIDDEN_IVARS
HIDDEN_IVARS

Attributes

allowed_actions[RW]
resource_initializing[R]

Public Class Methods

action(action, &recipe_block) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 153
def self.action(action, &recipe_block)
  action = action.to_sym
  declare_action_class
  action_class.action(action, &recipe_block)
  self.allowed_actions += [ action ]
  default_action action if Array(default_action) == [:nothing]
end
action_class(&block) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 166
def self.action_class(&block)
  return @action_class if @action_class && !block
  # If the superclass needed one, then we need one as well.
  if block || (superclass.respond_to?(:action_class) && superclass.action_class)
    @action_class = declare_action_class(&block)
  end
  @action_class
end
allowed_actions(*actions) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 124
def self.allowed_actions(*actions)
  @allowed_actions ||=
    if superclass.respond_to?(:allowed_actions)
      superclass.allowed_actions.dup
    else
      [ :nothing ]
    end
  @allowed_actions |= actions.flatten
end
allowed_actions=(value) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 133
def self.allowed_actions=(value)
  @allowed_actions = value.uniq
end
declare_action_class(&block) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 174
def self.declare_action_class(&block)
  @action_class ||= begin
                      if superclass.respond_to?(:action_class)
                        base_provider = superclass.action_class
                      end
                      base_provider ||= Chef::Provider

                      resource_class = self
                      Class.new(base_provider) do
                        include ActionClass
                        self.resource_class = resource_class
                      end
                    end
  @action_class.class_eval(&block) if block
  @action_class
end
default_action(action_name = NOT_PASSED) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 136
def self.default_action(action_name = NOT_PASSED)
  unless action_name.equal?(NOT_PASSED)
    @default_action = Array(action_name).map(&:to_sym)
    self.allowed_actions |= @default_action
  end

  if @default_action
    @default_action
  elsif superclass.respond_to?(:default_action)
    superclass.default_action
  else
    [:nothing]
  end
end
default_action=(action_name) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 150
def self.default_action=(action_name)
  default_action action_name
end
identity_property(name = nil) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 103
def self.identity_property(name = nil)
  result = identity_properties(*Array(name))
  if result.size > 1
    raise Chef::Exceptions::MultipleIdentityError, "identity_property cannot be called on an object with more than one identity property (#{result.map { |r| r.name }.join(", ")})."
  end
  result.first
end
load_current_value(&load_block) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 160
def self.load_current_value(&load_block)
  define_method(:load_current_value!, &load_block)
end
new(name, run_context = nil) click to toggle source
Calls superclass method
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 17
    def initialize(name, run_context = nil)
super if defined?(::Chef::Resource)
      name(name) unless name.nil?
      @run_context = run_context
      @noop = nil
      @before = nil
      @params = Hash.new
      @provider = nil
      @allowed_actions = self.class.allowed_actions.to_a
      @action = self.class.default_action
      @updated = false
      @updated_by_last_action = false
      @supports = {}
      @ignore_failure = false
      @retries = 0
      @retry_delay = 2
      @not_if = []
      @only_if = []
      @source_line = nil
      # We would like to raise an error when the user gives us a guard
      # interpreter and a ruby_block to the guard. In order to achieve this
      # we need to understand when the user overrides the default guard
      # interpreter. Therefore we store the default separately in a different
      # attribute.
      @guard_interpreter = nil
      @default_guard_interpreter = :default
      @elapsed_time = 0
      @sensitive = false
    end
use_automatic_resource_name() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 120
def self.use_automatic_resource_name
  automatic_name = convert_to_snake_case(self.name.split("::")[-1])
  resource_name automatic_name
end

Public Instance Methods

action(arg = nil) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 46
def action(arg = nil)
  if arg
    arg = Array(arg).map(&:to_sym)
    arg.each do |action|
      validate(
        { action: action },
        { action: { kind_of: Symbol, equal_to: allowed_actions } }
      )
    end
    @action = arg
  else
    @action
  end
end
Also aliased as: action=
action=(arg = nil)
Alias for: action
current_value_does_not_exist!() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 163
def current_value_does_not_exist!
  raise Chef::Exceptions::CurrentValueDoesNotExist
end
identity() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 72
def identity
  result = {}
  identity_properties = self.class.identity_properties
  identity_properties.each do |property|
    result[property.name] = send(property.name)
  end
  return result.values.first if identity_properties.size == 1
  result
end
resource_initializing=(value) click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 82
def resource_initializing=(value)
  if value
    @resource_initializing = true
  else
    remove_instance_variable(:@resource_initializing)
  end
end
resource_name() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 117
def resource_name
  @resource_name || self.class.resource_name
end
state()
state_for_resource_reporter() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 61
def state_for_resource_reporter
  state = {}
  state_properties = self.class.state_properties
  state_properties.each do |property|
    if property.identity? || property.is_set?(self)
      state[property.name] = send(property.name)
    end
  end
  state
end
Also aliased as: state
to_hash() click to toggle source
# File files/lib/chef_compat/copied_from_chef/chef/resource.rb, line 89
def to_hash
  # Grab all current state, then any other ivars (backcompat)
  result = {}
  self.class.state_properties.each do |p|
    result[p.name] = p.get(self)
  end
  safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS
  safe_ivars.each do |iv|
    key = iv.to_s.sub(/^@/, "").to_sym
    next if result.has_key?(key)
    result[key] = instance_variable_get(iv)
  end
  result
end