Class: Puppet::FileServing::Base

Inherits:
Object
  • Object
show all
Includes:
Util::MethodHelper
Defined in:
lib/puppet/file_serving/base.rb

Overview

The base class for Content and Metadata; provides common functionality like the behaviour around links.

Direct Known Subclasses

Content, Metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Constructor Details

#initialize(path, options = {}) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
# File 'lib/puppet/file_serving/base.rb', line 38

def initialize(path, options = {})
  self.path = path
  @links = :manage
  set_options(options)
end

Instance Attribute Details

Determine how we deal with links.



45
46
47
# File 'lib/puppet/file_serving/base.rb', line 45

def links
  @links
end

#pathObject

Set our base path.



54
55
56
# File 'lib/puppet/file_serving/base.rb', line 54

def path
  @path
end

#relative_pathObject

Set a relative path; this is used for recursion, and sets the file’s path relative to the initial recursion point.



62
63
64
# File 'lib/puppet/file_serving/base.rb', line 62

def relative_path
  @relative_path
end

#sourceObject

This is for external consumers to store the source that was used to retrieve the metadata.



12
13
14
# File 'lib/puppet/file_serving/base.rb', line 12

def source
  @source
end

Class Method Details

.absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/puppet/file_serving/base.rb', line 82

def self.absolute?(path)
  Puppet::Util.absolute_path?(path, :posix) or (Puppet.features.microsoft_windows? and Puppet::Util.absolute_path?(path, :windows))
end

Instance Method Details

#exist?Boolean

Does our file exist?

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/puppet/file_serving/base.rb', line 15

def exist?
    stat
    return true
rescue
    return false
end

#full_pathObject

Return the full path to our file. Fails if there’s no path set.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/file_serving/base.rb', line 23

def full_path
  if relative_path.nil? or relative_path == "" or relative_path == "."
     full_path = path
   else
     full_path = File.join(path, relative_path)
   end

   if Puppet.features.microsoft_windows?
     # Replace multiple slashes as long as they aren't at the beginning of a filename
     full_path.gsub(%r{(./)/+}, '\1')
   else
     full_path.gsub(%r{//+}, '/')
   end
end

#statObject

Stat our file, using the appropriate link-sensitive method.



69
70
71
72
# File 'lib/puppet/file_serving/base.rb', line 69

def stat
  @stat_method ||= self.links == :manage ? :lstat : :stat
  Puppet::FileSystem.send(@stat_method, full_path)
end

#to_data_hashObject



74
75
76
77
78
79
80
# File 'lib/puppet/file_serving/base.rb', line 74

def to_data_hash
  {
    'path'          => @path,
    'relative_path' => @relative_path,
    'links'         => @links.to_s
  }
end