Class: Whisper::CachedFile

Inherits:
Object
  • Object
show all
Includes:
Dependency, Loggy
Defined in:
lib/whisper/cached_file.rb

Overview

just a simple ol’ thing that just holds a lazily-loaded cached copy of a File in memory, updates itself upon command, and exposes a timestamp for dependeny calculation purposes.

TODO: do we actually need min_scan_interval? might be taken care of by Dependency.min_refresh_interval. Liekwise for DirScanner.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dependency

#content, #dependency_init, #refresh!, #timestamp

Constructor Details

#initialize(path) ⇒ CachedFile

Returns a new instance of CachedFile.



19
20
21
22
23
24
25
26
27
# File 'lib/whisper/cached_file.rb', line 19

def initialize path
  @path = path
  @content_type = nil
  @mtime = File.mtime @path
  @last_check = Time.at 0

  dependency_init
  CachedFile.min_scan_interval ||= 10
end

Class Attribute Details

.min_scan_intervalObject

Returns the value of attribute min_scan_interval.



17
18
19
# File 'lib/whisper/cached_file.rb', line 17

def min_scan_interval
  @min_scan_interval
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/whisper/cached_file.rb', line 15

def path
  @path
end

Class Method Details

.content_type_for(path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/whisper/cached_file.rb', line 52

def self.content_type_for path
  type = MIME::Types.type_for(path).first
  if type
    type.content_type
  elsif path =~ /\.js$/i
    "application/x-javascript"
  else
    warn "unknown MIME type for #{path}"
    "application/binary"
  end
end

Instance Method Details

#bodyObject



48
# File 'lib/whisper/cached_file.rb', line 48

def body; content end

#build(old_content) ⇒ Object



30
31
32
33
34
# File 'lib/whisper/cached_file.rb', line 30

def build old_content
  debug "reading #{@path} from disk"
  @mtime = File.mtime(@path)
  IO.read @path
end

#checkObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/whisper/cached_file.rb', line 35

def check
  return false unless @last_check + CachedFile.min_scan_interval < Time.now

  changed = begin
    File.mtime(@path) > @mtime
  rescue SystemCallError => e
    warn e.message
    true
  end
  @last_check = Time.now
  #debug "checking mtime for #{@path}: #{changed}"
  changed
end

#content_typeObject



50
# File 'lib/whisper/cached_file.rb', line 50

def content_type; @content_type ||= CachedFile.content_type_for @path end

#dependenciesObject



29
# File 'lib/whisper/cached_file.rb', line 29

def dependencies; [] end