Class: Wright::Util::FilePermissions
- Inherits:
-
Object
- Object
- Wright::Util::FilePermissions
- Defined in:
- lib/wright/util/file_permissions.rb
Overview
Internal: Helper class to manage file permissions.
Constant Summary collapse
- VALID_FILETYPES =
Internal: Supported filetypes.
[:file, :directory]
Instance Attribute Summary collapse
-
#filename ⇒ Object
Internal: Get/Set the target file’s name.
-
#group ⇒ Object
Internal: Get/Set the file’s target group.
-
#mode ⇒ Object
Internal: Get/Set the file’s target mode.
-
#owner ⇒ Object
Internal: Get the file’s target owner.
Class Method Summary collapse
-
.create_from_resource(resource, filetype) ⇒ Object
Internal: Create a FilePermissions from a Wright::Resource.
Instance Method Summary collapse
-
#current_group ⇒ Object
Internal: Get the file’s current group.
-
#current_mode ⇒ Object
Internal: Get the file’s current mode.
-
#current_owner ⇒ Object
Internal: Get the file’s current owner.
-
#initialize(filename, filetype) ⇒ FilePermissions
constructor
Internal: Initialize a FilePermissions object.
-
#update ⇒ Object
Internal: Update the file’s owner, group and mode.
-
#uptodate? ⇒ Boolean
Internal: Check if the file’s owner, group and mode are up-to-date.
Constructor Details
#initialize(filename, filetype) ⇒ FilePermissions
Internal: Initialize a FilePermissions object.
filename - The file’s name. filetype - The file’s type, typically :file or :directory.
42 43 44 45 46 47 48 |
# File 'lib/wright/util/file_permissions.rb', line 42 def initialize(filename, filetype) unless VALID_FILETYPES.include?(filetype) fail ArgumentError, "Invalid filetype '#{filetype}'" end @filename = filename @filetype = filetype end |
Instance Attribute Details
#filename ⇒ Object
Internal: Get/Set the target file’s name.
24 25 26 |
# File 'lib/wright/util/file_permissions.rb', line 24 def filename @filename end |
#group ⇒ Object
Internal: Get/Set the file’s target group.
27 28 29 |
# File 'lib/wright/util/file_permissions.rb', line 27 def group @group end |
#mode ⇒ Object
Internal: Get/Set the file’s target mode.
30 31 32 |
# File 'lib/wright/util/file_permissions.rb', line 30 def mode @mode end |
#owner ⇒ Object
Internal: Get the file’s target owner.
33 34 35 |
# File 'lib/wright/util/file_permissions.rb', line 33 def owner @owner end |
Class Method Details
.create_from_resource(resource, filetype) ⇒ Object
Internal: Create a FilePermissions from a Wright::Resource.
resource - The resource object. filetype - The file’s type, typically :file or :directory.
Returns a Wright::Util::FilePermissions object.
14 15 16 17 18 19 20 21 |
# File 'lib/wright/util/file_permissions.rb', line 14 def self.create_from_resource(resource, filetype) filepath = ::File.(resource.name) p = Wright::Util::FilePermissions.new(filepath, filetype) p.owner = resource.owner p.group = resource.group p.mode = resource.mode p end |
Instance Method Details
#current_group ⇒ Object
Internal: Get the file’s current group.
101 102 103 |
# File 'lib/wright/util/file_permissions.rb', line 101 def current_group Wright::Util::File.file_group(@filename) end |
#current_mode ⇒ Object
Internal: Get the file’s current mode.
91 92 93 |
# File 'lib/wright/util/file_permissions.rb', line 91 def current_mode Wright::Util::File.file_mode(@filename) end |
#current_owner ⇒ Object
Internal: Get the file’s current owner.
96 97 98 |
# File 'lib/wright/util/file_permissions.rb', line 96 def current_owner Wright::Util::File.file_owner(@filename) end |
#update ⇒ Object
Internal: Update the file’s owner, group and mode.
85 86 87 88 |
# File 'lib/wright/util/file_permissions.rb', line 85 def update ::File.chmod(@mode, @filename) if @mode ::File.chown(@owner, @group, @filename) if @owner || @group end |
#uptodate? ⇒ Boolean
Internal: Check if the file’s owner, group and mode are up-to-date.
76 77 78 79 80 81 82 |
# File 'lib/wright/util/file_permissions.rb', line 76 def uptodate? if ::File.exist?(@filename) owner_uptodate? && group_uptodate? && mode_uptodate? else false end end |