Class: Grass::FileSync::FileProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/grass/file_sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ FileProxy

Returns a new instance of FileProxy.



9
10
11
12
13
# File 'lib/grass/file_sync.rb', line 9

def initialize source
  @source = source
  FileUtils.mkdir_p File.dirname(@source.filepath)
  exists? ? read : write(@source.raw)
end

Instance Method Details

#deleteObject



36
37
38
# File 'lib/grass/file_sync.rb', line 36

def delete
  File.delete(@source.filepath) rescue nil
end

#dirty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/grass/file_sync.rb', line 40

def dirty?
  @source.raw.nil? || File.mtime(@source.filepath).to_i > @source.updated_at.to_i
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/grass/file_sync.rb', line 15

def exists?
  File.exists? @source.filepath
end

#readObject



19
20
21
22
23
24
25
# File 'lib/grass/file_sync.rb', line 19

def read
  begin
    value = File.read(@source.filepath)
    @source.update(raw: value) if dirty?
    value
  end rescue nil
end

#write(value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/grass/file_sync.rb', line 27

def write value
  begin
    File.open(@source.filepath,File::RDWR|File::CREAT){ |f| 
      f.truncate(0); f.rewind; f.write(value)
    }
    value
  end rescue nil
end