Class: Lumin::SourceSnapshot

Inherits:
Data
  • Object
show all
Defined in:
lib/lumin/source_snapshot.rb

Defined Under Namespace

Classes: ChangedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint

Returns:

  • (Object)

    the current value of fingerprint



4
5
6
# File 'lib/lumin/source_snapshot.rb', line 4

def fingerprint
  @fingerprint
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



4
5
6
# File 'lib/lumin/source_snapshot.rb', line 4

def source
  @source
end

Class Method Details

.fingerprint(path_or_stat) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lumin/source_snapshot.rb', line 18

def self.fingerprint(path_or_stat)
  stat = path_or_stat.is_a?(File::Stat) ? path_or_stat : File.stat(path_or_stat)
  [
    stat.size,
    stat.mtime.to_i,
    stat.mtime.nsec,
    stat.ctime.to_i,
    stat.ctime.nsec,
    stat.ino
  ].freeze
end

.read(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/lumin/source_snapshot.rb', line 7

def self.read(path)
  File.open(path, "rb") do |file|
    before = fingerprint(file.stat)
    source = file.read
    after = fingerprint(file.stat)
    raise ChangedError, "#{path} changed while it was being read" unless before == after

    new(source: source, fingerprint: after)
  end
end