Class: Vanagon::Common::Pathname

Inherits:
Object
  • Object
show all
Includes:
HashableAttributes
Defined in:
lib/vanagon/common/pathname.rb,
lib/vanagon/extensions/hashable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashableAttributes

#to_hash, #to_json

Constructor Details

#initialize(path, mode: nil, owner: nil, group: nil, config: false) ⇒ Vanagon::Common::Pathname

Each Vanagon::Common::Pathname requires a filesystem path, and has many optional properties that may be set at initialization time.

Parameters:

  • mode (String, Integer) (defaults to: nil)

    the UNIX Octal permission string to use when this file is archived

  • owner (String, Integer) (defaults to: nil)

    the username or UID to use when this file is archived

  • group (String, Integer) (defaults to: nil)

    the groupname or GID to use when this file is archived

  • config (Boolean) (defaults to: false)

    mark this file as a configuration file, stored as private state and exposed through the #configfile? method.



32
33
34
35
36
37
38
# File 'lib/vanagon/common/pathname.rb', line 32

def initialize(path, mode: nil, owner: nil, group: nil, config: false)
  @path = ::Pathname.new(path).cleanpath.to_s
  @mode ||= mode
  @owner ||= owner
  @group ||= group
  @config ||= config
end

Instance Attribute Details

#groupString, Integer

Returns the numeric group id or string representing the group name of the owner of self.

Returns:

  • (String, Integer)

    Returns the numeric group id or string representing the group name of the owner of self.



22
# File 'lib/vanagon/common/pathname.rb', line 22

attr_accessor :path, :mode, :owner, :group

#modeString, Integer

Returns an integer representing the permission bits of self. The meaning of the bits is platform dependent; on Unix systems, see stat(2).

Returns:

  • (String, Integer)

    Returns an integer representing the permission bits of self. The meaning of the bits is platform dependent; on Unix systems, see stat(2).



22
# File 'lib/vanagon/common/pathname.rb', line 22

attr_accessor :path, :mode, :owner, :group

#ownerString, Integer

Returns the numeric user id or string representing the user name of the owner of self.

Returns:

  • (String, Integer)

    Returns the numeric user id or string representing the user name of the owner of self.



22
# File 'lib/vanagon/common/pathname.rb', line 22

attr_accessor :path, :mode, :owner, :group

#pathString

Returns clean pathname of self with consecutive slashes and useless dots removed. The filesystem is not accessed.

Returns:

  • (String)

    Returns clean pathname of self with consecutive slashes and useless dots removed. The filesystem is not accessed.



22
23
24
# File 'lib/vanagon/common/pathname.rb', line 22

def path
  @path
end

Class Method Details

.configfile(path, **args) ⇒ Object

An alias to Vanagon::Common::Pathname‘s constructor method,

which returns a new {Vanagon::Common::Pathname}, explicitly marked as a configuration file

Examples:

Create a new configuration file, marked as a configuration file.

Vanagon::Common::Pathname.configfile('/etc/puppet/puppet/puppet.conf')

See Also:



56
57
58
# File 'lib/vanagon/common/pathname.rb', line 56

def self.configfile(path, **args)
  new(path, **args.merge!({ config: true }))
end

.file(path, **args) ⇒ Object

An alias to Vanagon::Common::Pathname‘s constructor method,

which returns a new {Vanagon::Common::Pathname}, explicitly marked as a file

Examples:

Create a new Vanagon::Common::Pathname, marked as a file.

Vanagon::Common::Pathname.file('/etc/puppet/puppet/puppet.conf')

See Also:



46
47
48
# File 'lib/vanagon/common/pathname.rb', line 46

def self.file(path, **args)
  new(path, **args.merge!({ config: false }))
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Equality – Two instances of Vanagon::Common::Pathname are equal if they

contain the same number attributes and if each attribute is equal to
(according to `Object#==`) the corresponding attribute in other_pathname.

Returns:

  • (Boolean)

    true if all attributes have equal values, or otherwise false.



77
78
79
# File 'lib/vanagon/common/pathname.rb', line 77

def ==(other)
  other.hash == hash
end

#configfile?Boolean

Returns true if a self is marked as a configuration file.

Returns:

  • (Boolean)

    true if a self is marked as a configuration file.



61
62
63
# File 'lib/vanagon/common/pathname.rb', line 61

def configfile?
  !!@config
end

#has_overrides?Boolean

Simple test to see if any of the non-required attributes have been set in this object.

Returns:

  • (Boolean)

    whether or not mode, owner or group has been set for the object



68
69
70
# File 'lib/vanagon/common/pathname.rb', line 68

def has_overrides?
  !!(@mode || @owner || @group)
end

#hashFixnum

Compute a hash-code for self, derived from its attributes. Two Vanagon::Common::Pathname objects with the same content will have the same hash code (and will compare as equal using #eql?)

Returns:

  • (Fixnum)


87
88
89
# File 'lib/vanagon/common/pathname.rb', line 87

def hash
  instance_variables.map { |v| instance_variable_get(v) }.hash
end