Class: Puppet::FileServing::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/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

Constructor Details

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

Returns a new instance of Base.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vendor/puppet/file_serving/base.rb', line 28

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

  options.each do |param, value|
    begin
      send param.to_s + "=", value
    rescue NoMethodError
      raise ArgumentError, "Invalid option #{param} for #{self.class}"
    end
  end
end

Instance Attribute Details

Determine how we deal with links.



42
43
44
# File 'lib/vendor/puppet/file_serving/base.rb', line 42

def links
  @links
end

#pathObject

Set our base path.



51
52
53
# File 'lib/vendor/puppet/file_serving/base.rb', line 51

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.



59
60
61
# File 'lib/vendor/puppet/file_serving/base.rb', line 59

def relative_path
  @relative_path
end

#sourceObject

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



9
10
11
# File 'lib/vendor/puppet/file_serving/base.rb', line 9

def source
  @source
end

Class Method Details

.absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/vendor/puppet/file_serving/base.rb', line 85

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)


12
13
14
15
16
17
# File 'lib/vendor/puppet/file_serving/base.rb', line 12

def exist?
    stat
    return true
rescue => detail
    return false
end

#full_path(dummy_argument = :work_arround_for_ruby_GC_bug) ⇒ Object

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



20
21
22
23
24
25
26
# File 'lib/vendor/puppet/file_serving/base.rb', line 20

def full_path(dummy_argument=:work_arround_for_ruby_GC_bug)
  (if relative_path.nil? or relative_path == "" or relative_path == "."
     path
   else
     File.join(path, relative_path)
   end).gsub(%r{//+}, "/")
end

#statObject

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



66
67
68
69
# File 'lib/vendor/puppet/file_serving/base.rb', line 66

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

#to_pson_data_hashObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vendor/puppet/file_serving/base.rb', line 71

def to_pson_data_hash
  {
    # No 'document_type' since we don't send these bare
    'data'       => {
      'path'          => @path,
      'relative_path' => @relative_path,
      'links'         => @links
      },
    'metadata' => {
      'api_version' => 1
      }
  }
end