Class: Wright::Provider::Directory

Inherits:
Wright::Provider show all
Defined in:
lib/wright/provider/directory.rb

Overview

Directory provider. Used as a provider for Resource::Directory.

Constant Summary

Constants inherited from Wright::Provider

PROVIDER_DIR

Instance Method Summary collapse

Methods inherited from Wright::Provider

#env, #exec_or_fail, #initialize, #unless_dry_run, #unless_uptodate, #updated?

Constructor Details

This class inherits a constructor from Wright::Provider

Instance Method Details

#createvoid

This method returns an undefined value.

Creates or updates the directory.


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wright/provider/directory.rb', line 14

def create
  fail Errno::EEXIST, dirname_expanded if regular_file?

  dir_permissions = permissions
  unless_uptodate(:create, "directory already created: '#{dir_name}'") do
    unless_dry_run("create directory: '#{dir_name}'") do
      FileUtils.mkdir_p(dirname_expanded)
      dir_permissions.update unless dir_permissions.uptodate?
    end
  end
end

#dir_nameObject (private)


44
45
46
# File 'lib/wright/provider/directory.rb', line 44

def dir_name
  @resource.name
end

#dirname_expandedObject (private)


67
68
69
# File 'lib/wright/provider/directory.rb', line 67

def dirname_expanded
  ::File.expand_path(dir_name)
end

#permissionsObject (private)


48
49
50
51
# File 'lib/wright/provider/directory.rb', line 48

def permissions
  Wright::Util::FilePermissions.create_from_resource(@resource,
                                                     :directory)
end

#regular_file?Boolean (private)

Returns:

  • (Boolean)

63
64
65
# File 'lib/wright/provider/directory.rb', line 63

def regular_file?
  ::File.exist?(dirname_expanded) && !::File.directory?(dirname_expanded)
end

#removevoid

This method returns an undefined value.

Removes the directory.


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wright/provider/directory.rb', line 29

def remove
  if ::File.exist?(dirname_expanded) &&
     !::File.directory?(dirname_expanded)
    fail "'#{dirname_expanded}' exists but is not a directory"
  end

  unless_uptodate(:remove, "directory already removed: '#{dir_name}'") do
    unless_dry_run("remove directory: '#{dir_name}'") do
      FileUtils.rmdir(dirname_expanded)
    end
  end
end

#uptodate?(action) ⇒ Boolean (private)

Returns:

  • (Boolean)

53
54
55
56
57
58
59
60
61
# File 'lib/wright/provider/directory.rb', line 53

def uptodate?(action)
  case action
  when :create
    ::File.directory?(dirname_expanded) && permissions.uptodate?
  when :remove
    !::File.exist?(dirname_expanded) &&
      !::File.directory?(dirname_expanded)
  end
end