Class: Wright::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/resource.rb,
lib/wright/resource/file.rb,
lib/wright/resource/user.rb,
lib/wright/resource/group.rb,
lib/wright/resource/package.rb,
lib/wright/resource/symlink.rb,
lib/wright/resource/directory.rb

Overview

Resource base class.

Direct Known Subclasses

Directory, File, Group, Package, Symlink, User

Defined Under Namespace

Classes: Directory, File, Group, Package, Symlink, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, args = {}) ⇒ Resource

Initializes a Resource.

Parameters:

  • name (String) (defaults to: nil)

    the name of the resource


12
13
14
15
16
17
18
19
# File 'lib/wright/resource.rb', line 12

def initialize(name = nil, args = {})
  @name           = name
  @action         = args.fetch(:action, nil)
  @ignore_failure = args.fetch(:ignore_failure, false)
  self.on_update  = args.fetch(:on_update, nil)
  @resource_name  = Util.class_to_resource_name(self.class).to_sym
  @provider       = provider_for_resource
end

Instance Attribute Details

#actionSymbol

Returns the name of the method to be run by #run_action.

Returns:

  • (Symbol)

    the name of the method to be run by #run_action


22
23
24
# File 'lib/wright/resource.rb', line 22

def action
  @action
end

#ignore_failureBool

Returns the ignore_failure attribute.

Returns:

  • (Bool)

    the ignore_failure attribute


25
26
27
# File 'lib/wright/resource.rb', line 25

def ignore_failure
  @ignore_failure
end

#nameString

Returns the resource's name attribute.

Examples:

foo = Wright::Resource::Symlink.new('/tmp/fstab')
foo.name
# => "/tmp/fstab"

bar = Wright::Resource::Symlink.new
bar.name = '/tmp/passwd'
bar.name
# => "/tmp/passwd"

Returns:

  • (String)

    the resource's name attribute


38
39
40
# File 'lib/wright/resource.rb', line 38

def name
  @name
end

#on_update=(on_update) ⇒ void

This method returns an undefined value.

Sets an update action for a resource.

Parameters:

  • on_update (Proc, #call)

    the block that is called when the resource is updated.

Raises:

  • (ArgumentError)

    if on_update is not callable


55
56
57
58
59
60
61
# File 'lib/wright/resource.rb', line 55

def on_update=(on_update)
  if on_update.respond_to?(:call) || on_update.nil?
    @on_update = on_update
  else
    fail ArgumentError, "#{on_update} is not callable"
  end
end

#resource_nameSymbol (readonly)

Returns a compact resource name.

Examples:

foo = Wright::Resource::Symlink.new
foo.resource_name
# => :symlink

Returns:

  • (Symbol)

    a compact resource name


46
47
48
# File 'lib/wright/resource.rb', line 46

def resource_name
  @resource_name
end

Instance Method Details

#run_actionObject

Runs the resource's current action.

Examples:

fstab = Wright::Resource::Symlink.new('/tmp/fstab')
fstab.action = :remove
fstab.run_action

Returns:

  • the return value of the current action


71
72
73
# File 'lib/wright/resource.rb', line 71

def run_action
  send action if action
end