module Garcon::Resource::BaseDSL::ClassMethods

Public Instance Methods

actions(*names) click to toggle source

Imitate the behavior of the ‘Chef::Resource::LWRPBase` LWRP DSL providing a `action` method.

@param [Array<String, Symbol>] name

The default action.

@return [undefined]

# File lib/garcon/chef/resource/base_dsl.rb, line 102
def actions(*names)
  @actions ||= superclass.respond_to?(:actions) ?
               superclass.actions.dup : []
  (@actions << names).flatten!.uniq!
  @actions
end
attribute(name, opts) click to toggle source

Imitate the behavior of the ‘Chef::Resource::LWRPBase` LWRP DSL providing a `attribute` method.

@param [Symbol] name

@param [Hash] opts

@return [undefined]

# File lib/garcon/chef/resource/base_dsl.rb, line 152
def attribute(name, opts)
  coerce = opts.delete(:coerce)
  define_method(name) do |arg = nil, &block|
    if coerce && !arg.nil?
      arg = Garcon.coercer.coerce(arg, &coerce)
      # arg = instance_exec(arg, &coerce)
    else
      arg = block if arg.nil?
    end
    set_or_return(name, arg, opts)
  end
end
Also aliased as: property
basic(source = nil) click to toggle source
# File lib/garcon/chef/resource/base_dsl.rb, line 109
def basic(source = nil)
  source ||= self
  hash    = Hash.new
  pattern = Regexp.new('^_set_or_return_(.+)$')
  source.public_methods(false).each do |method|
    pattern.match(method) do |m|
      attribute = m[1].to_sym
      hash[attribute] = send(attribute)
    end
  end
  Attribute.from_hash(hash)
end
default_action(name = nil) click to toggle source

Imitate the behavior of the ‘Chef::Resource::LWRPBase` DSL providing a `default_action` method.

@param [Symbol, String] name

The default action.

@return [undefined]

# File lib/garcon/chef/resource/base_dsl.rb, line 84
def default_action(name = nil)
  if name
    @default_action = name
    actions(name)
  end
  @default_action || (superclass.respond_to?(:default_action) &&
                      superclass.default_action) ||
                      :actions.first || :nothing
end
dsl_name() click to toggle source

Used by Resource#to_text to find the human name for the resource.

# File lib/garcon/chef/resource/base_dsl.rb, line 72
def dsl_name
  resource_name.to_s
end
full(attribute = nil, source = nil, recursion = 3) click to toggle source
# File lib/garcon/chef/resource/base_dsl.rb, line 122
def full(attribute = nil, source = nil, recursion = 3)
  source ||= self
  if attribute && (attribute.is_a?(Hash) || attribute.is_a?(Mash))
    data = attribute
  else
    data = Hash.new
  end
  data = Mash.from_hash(data) unless data.is_a?(Mash)
  data.merge!(attr_mash(source))
  data = data.symbolize_keys
  data.each do |k,v|
    next unless v.is_a? String

    for i in 1..recursion
      v2 = v % data
      v2 == v ? break : data[k] = v = v2
    end
  end
  Attribute.from_hash(data)
end
included(descendant) click to toggle source

Hook called when module is included, extends a descendant with class and instance methods.

@param [Module] descendant

the module or class including Garcon::Resource::BaseDSL

@return [self]

Calls superclass method
# File lib/garcon/chef/resource/base_dsl.rb, line 174
def included(descendant)
  super
  descendant.extend ClassMethods
end
property(name, opts)
Alias for: attribute
provides(name) click to toggle source

Maps a resource/provider (and optionally a platform and version) to a Chef resource/provider. This allows finer grained per platform resource attributes and the end of overloaded resource definitions.

@note

The provides method must be defined in both the custom resource and
custom provider files and both files must have identical provides
statement(s).

@param [Symbol] name

Name of a Chef resource/provider to map to.

@return [undefined]

Calls superclass method
# File lib/garcon/chef/resource/base_dsl.rb, line 42
def provides(name)
  if self.name && respond_to?(:constantize)
    old_constantize = instance_method :constantize
    define_singleton_method :constantize do |name|
      name == self.name ? self : old_constantize.bind(self).call(name)
    end
  end
  @provides_name = name
  super if defined? super
end
resource_name(auto = true) click to toggle source

Return the Snake case name of the current resource class. If not set explicitly it will be introspected based on the class name.

@param [Boolean] auto

Try to auto-detect based on class name.

@return [Symbol]

# File lib/garcon/chef/resource/base_dsl.rb, line 61
def resource_name(auto = true)
  return @provides_name if @provides_name
  @provides_name || if name && name.start_with?('Chef::Resource')
    Garcon::Inflections.snakeify(name, 'Chef::Resource').to_sym
  elsif name
    Garcon::Inflections.snakeify(name.split('::').last).to_sym
  end
end