Class: Websitary::FileMTimes

Inherits:
Object
  • Object
show all
Defined in:
lib/websitary/filemtimes.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ FileMTimes

Returns a new instance of FileMTimes.



13
14
15
16
17
18
# File 'lib/websitary/filemtimes.rb', line 13

def initialize(configuration)
    @configuration = configuration
    @store = File.join(@configuration.cfgdir, 'mtime.yml')
    @data  = {}
    swap_in
end

Instance Method Details

#mtime(filename) ⇒ Object



37
38
39
40
# File 'lib/websitary/filemtimes.rb', line 37

def mtime(filename)
    filenamec = @configuration.canonic_filename(filename)
    @data[filenamec] ||= set(filename)
end

#set(filename, mtime = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/websitary/filemtimes.rb', line 42

def set(filename, mtime=nil)
    if File.exist?(filename)
        mtime ||= File.mtime(filename)
        filenamec = @configuration.canonic_filename(filename)
        @data[filenamec] = mtime
        $logger.debug "Set mtime: #{filename} -> #{mtime.to_s}"
        mtime
    else
        nil
    end
end

#swap_inObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/websitary/filemtimes.rb', line 20

def swap_in
    if File.exist?(@store)
        @data = YAML.load_file(@store)
        case @data
        when Hash
        else
            $logger.error 'mtime.yml stored malformed data'
            @data = {}
        end
        File.delete(@store)
    end
end

#swap_outObject



33
34
35
# File 'lib/websitary/filemtimes.rb', line 33

def swap_out
    File.open(@store, 'w') {|f| YAML.dump(@data, f)}
end