Module: Poise::Helpers::LWRPPolyfill::Resource::ClassMethods

Included in:
Poise::Helpers::LWRPPolyfill::Resource
Defined in:
lib/poise/helpers/lwrp_polyfill.rb

Overview

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#actionsArray<Symbol> #actions(*names) ⇒ Array<Symbol>

Overloads:

  • #actionsArray<Symbol>

    Get all actions allowed for this resource class. This includes any actions allowed on parent classes.

    Returns:

    • (Array<Symbol>)
  • #actions(*names) ⇒ Array<Symbol>

    Set actions as allowed for this resource class. These must correspond with action methods in the provider class(es).

    Examples:

    class MyApp < Chef::Resource
      include Poise
      actions(:install, :uninstall)
    end

    Parameters:

    • names (Array<Symbol>)

      One or more actions to set.

    Returns:

    • (Array<Symbol>)

Since:

  • 1.0.0



90
91
92
93
# File 'lib/poise/helpers/lwrp_polyfill.rb', line 90

def actions(*names)
  @actions ||= ( respond_to?(:superclass) && superclass.respond_to?(:actions) && superclass.actions.dup ) || ( respond_to?(:superclass) && superclass != Chef::Resource && superclass.respond_to?(:allowed_actions) && superclass.allowed_actions.dup ) || []
  (@actions << names).tap {|actions| actions.flatten!; actions.uniq! }
end

#attribute(name, opts = {}) Also known as: property

This method returns an undefined value.

Create a resource property (née attribute) on this resource class. This follows the same usage as the helper of the same name in Chef LWRPs.

Examples:

class MyApp < Chef::Resource
  include Poise
  attribute(:path, name_attribute: true)
  attribute(:port, kind_of: Integer, default: 8080)
end

Parameters:

  • name (Symbol)

    Name of the property.

  • opts (Hash<Symbol, Object>) (defaults to: {})

    Validation options and flags.

Since:

  • 1.0.0



108
109
110
111
112
113
114
115
116
# File 'lib/poise/helpers/lwrp_polyfill.rb', line 108

def attribute(name, opts={})
  # Freeze the default value. This is done upstream too in Chef 12.5+.
  opts[:default].freeze if opts && opts[:default]
  # Ruby 1.8 can go to hell.
  define_method(name) do |arg=nil, &block|
    arg = block if arg.nil? # Try to allow passing either.
    set_or_return(name, arg, opts)
  end
end

#default_actionArray<Symbol> #default_action(name) ⇒ Array<Symbol>

Overloads:

  • #default_actionArray<Symbol>

    Get the default action for this resource class. If no explicit default is set, the first action in the list will be used.

    Returns:

    • (Array<Symbol>)

    See Also:

  • #default_action(name) ⇒ Array<Symbol>
    Note:

    It is idiomatic to use #actions instead, with the first action specified being the default.

    Set the default action for this resource class. If this action is not already allowed, it will be added.

    Examples:

    class MyApp < Chef::Resource
      include Poise
      default_action(:install)
    end

    Parameters:

    • name (Symbol, Array<Symbol>)

      Name of the action(s).

    Returns:

    • (Array<Symbol>)

Since:

  • 1.0.0



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/poise/helpers/lwrp_polyfill.rb', line 59

def default_action(name=nil)
  if name
    name = Array(name).flatten.map(&:to_sym)
    @default_action = name
    actions(*name)
  end
  if @default_action
    @default_action
  elsif respond_to?(:superclass) && superclass != Chef::Resource && superclass.respond_to?(:default_action) && superclass.default_action && Array(superclass.default_action) != %i{nothing}
    superclass.default_action
  elsif first_non_nothing = actions.find {|action| action != :nothing }
    [first_non_nothing]
  else
    %i{nothing}
  end
end

#included(klass) ⇒ Object

Since:

  • 1.0.0



121
122
123
124
# File 'lib/poise/helpers/lwrp_polyfill.rb', line 121

def included(klass)
  super
  klass.extend(ClassMethods)
end