Module: Poise::Helpers::ResourceName
- Included in:
- ChefspecMatchers, ResourceSubclass, Resource
- Defined in:
- lib/poise/helpers/resource_name.rb
Overview
A resource mixin to automatically set @resource_name.
Class Method Summary collapse
-
.provides(name, *args, &block)
Set the DSL name for the the resource class.
-
.resource_name(auto = true) ⇒ Symbol
Retreive the DSL name for the resource class.
Instance Method Summary collapse
Class Method Details
.provides(name, *args, &block)
This method returns an undefined value.
Set the DSL name for the the resource class.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/poise/helpers/resource_name.rb', line 54 def provides(name, *args, &block) # Patch self.constantize so this can cope with anonymous classes. # This does require that the anonymous class define self.name though. if self.name && respond_to?(:constantize) old_constantize = instance_method(:constantize) define_singleton_method(:constantize) do |const_name| ( const_name == self.name ) ? self : old_constantize.bind(self).call(const_name) end end # Store the name for later. @provides_name ||= name # Call the original if present. The defined? is for old Chef. super(name, *args, &block) if defined?(super) end |
.resource_name(auto = true) ⇒ Symbol
Retreive the DSL name for the resource class. If not set explicitly via provides this will try to auto-detect based on the class name.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/poise/helpers/resource_name.rb', line 74 def resource_name(auto=true) # In 12.4+ we need to proxy through the super class for setting. return super(auto) if defined?(super) && (auto.is_a?(Symbol) || auto.is_a?(String)) return @provides_name unless auto @provides_name || if name mode = if name.start_with?('Chef::Resource') [name, 'Chef::Resource'] else [name.split('::').last] end Chef::Mixin::ConvertToClassName.convert_to_snake_case(*mode).to_sym elsif defined?(super) # No name on 12.4+ probably means this is an LWRP, use super(). super() end end |
Instance Method Details
#initialize(*args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/poise/helpers/resource_name.rb', line 31 def initialize(*args) super # If provides() was explicitly set, unconditionally set @resource_name. # This helps when subclassing core Chef resources which set it # themselves in #initialize. if self.class.resource_name(false) @resource_name = self.class.resource_name else @resource_name ||= self.class.resource_name end end |