Class: Puppet::FileBucket::File

Inherits:
Object
  • Object
show all
Extended by:
Indirector
Defined in:
lib/puppet/file_bucket/file.rb

Defined Under Namespace

Classes: FileContents, StringContents

Constant Summary

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indirector

configure_routes, indirects

Constructor Details

#initialize(contents, options = {}) ⇒ File

Returns a new instance of File.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet/file_bucket/file.rb', line 24

def initialize(contents, options = {})
  case contents
  when String
    @contents = StringContents.new(contents)
  when Pathname
    @contents = FileContents.new(contents)
  else
    raise ArgumentError.new("contents must be a String or Pathname, got a #{contents.class}")
  end

  @bucket_path = options.delete(:bucket_path)
  @checksum_type = Puppet[:digest_algorithm].to_sym
  raise ArgumentError.new("Unknown option(s): #{options.keys.join(', ')}") unless options.empty?
end

Instance Attribute Details

#bucket_pathObject (readonly)



14
15
16
# File 'lib/puppet/file_bucket/file.rb', line 14

def bucket_path
  @bucket_path
end

Class Method Details

.default_formatObject



20
21
22
# File 'lib/puppet/file_bucket/file.rb', line 20

def self.default_format
  :binary
end

.from_binary(contents) ⇒ Object



77
78
79
# File 'lib/puppet/file_bucket/file.rb', line 77

def self.from_binary(contents)
  self.new(contents)
end

.from_data_hash(data) ⇒ Object



86
87
88
# File 'lib/puppet/file_bucket/file.rb', line 86

def self.from_data_hash(data)
  self.new(data["contents"])
end

.supported_formatsObject



16
17
18
# File 'lib/puppet/file_bucket/file.rb', line 16

def self.supported_formats
  [:binary]
end

Instance Method Details

#checksumObject



53
54
55
# File 'lib/puppet/file_bucket/file.rb', line 53

def checksum
  "{#{checksum_type}}#{checksum_data}"
end

#checksum_dataObject



57
58
59
# File 'lib/puppet/file_bucket/file.rb', line 57

def checksum_data
  @checksum_data ||= @contents.checksum_data(@checksum_type)
end

#checksum_typeObject



49
50
51
# File 'lib/puppet/file_bucket/file.rb', line 49

def checksum_type
  @checksum_type.to_s
end

#contentsObject



69
70
71
# File 'lib/puppet/file_bucket/file.rb', line 69

def contents
  to_binary
end

#nameObject



73
74
75
# File 'lib/puppet/file_bucket/file.rb', line 73

def name
  "#{checksum_type}/#{checksum_data}"
end

#sizeNum

Returns The size of the contents.

Returns:

  • (Num)

    The size of the contents



40
41
42
# File 'lib/puppet/file_bucket/file.rb', line 40

def size
  @contents.size()
end

#stream(&block) ⇒ IO

Returns A stream that reads the contents.

Returns:

  • (IO)

    A stream that reads the contents



45
46
47
# File 'lib/puppet/file_bucket/file.rb', line 45

def stream(&block)
  @contents.stream(&block)
end

#to_binaryObject



65
66
67
# File 'lib/puppet/file_bucket/file.rb', line 65

def to_binary
  @contents.to_binary
end

#to_data_hashObject



81
82
83
84
# File 'lib/puppet/file_bucket/file.rb', line 81

def to_data_hash
  # Note that this serializes the entire data to a string and places it in a hash.
  { "contents" => contents.to_binary }
end

#to_sObject



61
62
63
# File 'lib/puppet/file_bucket/file.rb', line 61

def to_s
  to_binary
end