Class: Puppet::FileServing::Base
- Defined in:
- lib/puppet/file_serving/base.rb
Overview
The base class for Content and Metadata; provides common functionality like the behaviour around links.
Instance Attribute Summary collapse
-
#links ⇒ Object
Determine how we deal with links.
-
#path ⇒ Object
Set our base path.
-
#relative_path ⇒ Object
Set a relative path; this is used for recursion, and sets the file’s path relative to the initial recursion point.
-
#source ⇒ Object
This is for external consumers to store the source that was used to retrieve the metadata.
Class Method Summary collapse
Instance Method Summary collapse
-
#exist? ⇒ Boolean
Does our file exist?.
-
#full_path ⇒ Object
Return the full path to our file.
-
#initialize(path, links: nil, relative_path: nil, source: nil) ⇒ Base
constructor
A new instance of Base.
-
#stat ⇒ Object
Stat our file, using the appropriate link-sensitive method.
- #to_data_hash ⇒ Object
Constructor Details
#initialize(path, links: nil, relative_path: nil, source: nil) ⇒ Base
Returns a new instance of Base.
35 36 37 38 39 40 41 42 |
# File 'lib/puppet/file_serving/base.rb', line 35 def initialize(path, links: nil, relative_path: nil, source: nil) self.path = path @links = :manage self.links = links if links self.relative_path = relative_path if relative_path self.source = source if source end |
Instance Attribute Details
#links ⇒ Object
Determine how we deal with links.
45 46 47 |
# File 'lib/puppet/file_serving/base.rb', line 45 def links @links end |
#path ⇒ Object
Set our base path.
55 56 57 |
# File 'lib/puppet/file_serving/base.rb', line 55 def path @path end |
#relative_path ⇒ Object
Set a relative path; this is used for recursion, and sets the file’s path relative to the initial recursion point.
63 64 65 |
# File 'lib/puppet/file_serving/base.rb', line 63 def relative_path @relative_path end |
#source ⇒ Object
This is for external consumers to store the source that was used to retrieve the metadata.
9 10 11 |
# File 'lib/puppet/file_serving/base.rb', line 9 def source @source end |
Class Method Details
.absolute?(path) ⇒ Boolean
83 84 85 |
# File 'lib/puppet/file_serving/base.rb', line 83 def self.absolute?(path) Puppet::Util.absolute_path?(path, :posix) || (Puppet::Util::Platform.windows? && Puppet::Util.absolute_path?(path, :windows)) end |
Instance Method Details
#exist? ⇒ Boolean
Does our file exist?
12 13 14 15 16 17 |
# File 'lib/puppet/file_serving/base.rb', line 12 def exist? stat return true rescue return false end |
#full_path ⇒ Object
Return the full path to our file. Fails if there’s no path set.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/puppet/file_serving/base.rb', line 20 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::Util::Platform.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 |
#stat ⇒ Object
Stat our file, using the appropriate link-sensitive method.
70 71 72 73 |
# File 'lib/puppet/file_serving/base.rb', line 70 def stat @stat_method ||= self.links == :manage ? :lstat : :stat Puppet::FileSystem.send(@stat_method, full_path) end |
#to_data_hash ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/puppet/file_serving/base.rb', line 75 def to_data_hash { 'path' => @path, 'relative_path' => @relative_path, 'links' => @links.to_s } end |