Class: Fontist::Indexes::DirectorySnapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/indexes/directory_snapshot.rb

Overview

Value object representing the state of a directory at a point in time Immutable - returns new instances for state changes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directory_pathObject (readonly)

Returns the value of attribute directory_path.



8
9
10
# File 'lib/fontist/indexes/directory_snapshot.rb', line 8

def directory_path
  @directory_path
end

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/fontist/indexes/directory_snapshot.rb', line 8

def files
  @files
end

#scanned_atObject (readonly)

Returns the value of attribute scanned_at.



8
9
10
# File 'lib/fontist/indexes/directory_snapshot.rb', line 8

def scanned_at
  @scanned_at
end

Class Method Details

.create(directory_path) ⇒ Object

Create a new snapshot by scanning a directory Returns: DirectorySnapshot instance



12
13
14
15
16
# File 'lib/fontist/indexes/directory_snapshot.rb', line 12

def self.create(directory_path)
  directory_path = directory_path.to_s
  files = IncrementalScanner.scan_directory(directory_path)
  new(directory_path, files, Time.now.to_i)
end

.from_hash(hash) ⇒ Object

Create from existing data (for cache restoration)



19
20
21
22
23
24
25
# File 'lib/fontist/indexes/directory_snapshot.rb', line 19

def self.from_hash(hash)
  new(
    hash[:directory_path].to_s,
    hash[:files],
    hash[:scanned_at],
  )
end

Instance Method Details

#file_countObject

Get count of files in snapshot



44
45
46
# File 'lib/fontist/indexes/directory_snapshot.rb', line 44

def file_count
  @files.length
end

#file_info(filename) ⇒ Object

Get file info for a specific filename Returns: Hash with file metadata or nil if not found



29
30
31
# File 'lib/fontist/indexes/directory_snapshot.rb', line 29

def file_info(filename)
  @files_by_filename[filename]
end

#has_file?(filename) ⇒ Boolean

Check if file exists in snapshot

Returns:

  • (Boolean)


39
40
41
# File 'lib/fontist/indexes/directory_snapshot.rb', line 39

def has_file?(filename)
  @files_by_filename.key?(filename)
end

#older_than?(seconds) ⇒ Boolean

Check if snapshot is older than given seconds

Returns:

  • (Boolean)


34
35
36
# File 'lib/fontist/indexes/directory_snapshot.rb', line 34

def older_than?(seconds)
  Time.now.to_i - @scanned_at > seconds
end

#to_hObject

Convert to hash for serialization



49
50
51
52
53
54
55
# File 'lib/fontist/indexes/directory_snapshot.rb', line 49

def to_h
  {
    directory_path: @directory_path,
    files: @files,
    scanned_at: @scanned_at,
  }
end