Method: Puppet::Util::FileType.newfiletype

Defined in:
lib/puppet/util/filetype.rb

.newfiletype(name, &block) ⇒ Object

Create a new filetype.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/puppet/util/filetype.rb', line 24

def self.newfiletype(name, &block)
  @filetypes ||= {}

  klass = genclass(
    name,
    :block => block,
    :prefix => "FileType",
    :hash => @filetypes
  )

  # Rename the read and write methods, so that we're sure they
  # maintain the stats.
  klass.class_eval do
    # Rename the read method
    define_method(:real_read, instance_method(:read))
    define_method(:read) do
      val = real_read
      @loaded = Time.now
      if val
        val.gsub(/# HEADER.*\n/, '')
      else
        ""
      end
    rescue Puppet::Error
      raise
    rescue => detail
      message = _("%{klass} could not read %{path}: %{detail}") % { klass: self.class, path: @path, detail: detail }
      Puppet.log_exception(detail, message)
      raise Puppet::Error, message, detail.backtrace
    end

    # And then the write method
    define_method(:real_write, instance_method(:write))
    define_method(:write) do |text|
      val = real_write(text)
      @synced = Time.now
      val
    rescue Puppet::Error
      raise
    rescue => detail
      message = _("%{klass} could not write %{path}: %{detail}") % { klass: self.class, path: @path, detail: detail }
      Puppet.log_exception(detail, message)
      raise Puppet::Error, message, detail.backtrace
    end
  end
end