Module: Garcon::Resource::BaseDSL::ClassMethods

Included in:
Garcon::Resource::BaseDSL
Defined in:
lib/garcon/chef/resource/base_dsl.rb

Instance Method Summary collapse

Instance Method Details

#actions(*names) ⇒ undefined

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

Parameters:

Returns:

  • (undefined)


102
103
104
105
106
107
# 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) ⇒ undefined Also known as: property

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

Parameters:

Returns:

  • (undefined)


152
153
154
155
156
157
158
159
160
161
162
163
# 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

#basic(source = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ undefined

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

Parameters:

  • name (Symbol, String) (defaults to: nil)

    The default action.

Returns:

  • (undefined)


84
85
86
87
88
89
90
91
92
# 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_nameObject

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



72
73
74
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ self

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

Parameters:

  • descendant (Module)

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

Returns:

  • (self)


174
175
176
177
# File 'lib/garcon/chef/resource/base_dsl.rb', line 174

def included(descendant)
  super
  descendant.extend ClassMethods
end

#provides(name) ⇒ undefined

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).

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.

Parameters:

  • name (Symbol)

    Name of a Chef resource/provider to map to.

Returns:

  • (undefined)


42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Symbol

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

Parameters:

  • auto (Boolean) (defaults to: true)

    Try to auto-detect based on class name.

Returns:



61
62
63
64
65
66
67
68
# 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