Class: WatchedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/text_tunnel/watched_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data) ⇒ WatchedFile

Returns a new instance of WatchedFile.



12
13
14
15
16
17
18
19
20
# File 'lib/text_tunnel/watched_file.rb', line 12

def initialize(name, data)
  @name = sanitize_name(name)
  @data = data
  @id = SecureRandom.hex
  @local_dir = "#{Dir.tmpdir}/text-tunnel/#{id}"
  @local_path = "#{@local_dir}/#{@name}"

  write_temp_file
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/text_tunnel/watched_file.rb', line 9

def data
  @data
end

#hashObject (readonly)

Returns the value of attribute hash.



10
11
12
# File 'lib/text_tunnel/watched_file.rb', line 10

def hash
  @hash
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/text_tunnel/watched_file.rb', line 7

def id
  @id
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



8
9
10
# File 'lib/text_tunnel/watched_file.rb', line 8

def local_path
  @local_path
end

Instance Method Details

#pollObject



22
23
24
25
26
27
28
29
30
# File 'lib/text_tunnel/watched_file.rb', line 22

def poll
  old_mtime = @mtime
  @mtime = File.mtime(local_path)
  if @mtime != old_mtime
    @mtime = old_mtime
    @data = File.read(local_path)
    hash_data
  end
end