Class: Fontist::Indexes::DirectoryChange

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

Overview

Value object representing a single change detected in a directory Immutable - describes what changed between two snapshots

Constant Summary collapse

ADDED =

Change types

:added
MODIFIED =
:modified
REMOVED =
:removed
UNCHANGED =
:unchanged

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(change_type, filename, old_info, new_info) ⇒ DirectoryChange

Instance methods



76
77
78
79
80
81
82
# File 'lib/fontist/indexes/directory_change.rb', line 76

def initialize(change_type, filename, old_info, new_info)
  @change_type = change_type
  @filename = filename
  @old_info = old_info
  @new_info = new_info
  freeze # Immutable
end

Instance Attribute Details

#change_typeObject (readonly)

Returns the value of attribute change_type.



6
7
8
# File 'lib/fontist/indexes/directory_change.rb', line 6

def change_type
  @change_type
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/fontist/indexes/directory_change.rb', line 6

def filename
  @filename
end

#new_infoObject (readonly)

Returns the value of attribute new_info.



6
7
8
# File 'lib/fontist/indexes/directory_change.rb', line 6

def new_info
  @new_info
end

#old_infoObject (readonly)

Returns the value of attribute old_info.



6
7
8
# File 'lib/fontist/indexes/directory_change.rb', line 6

def old_info
  @old_info
end

Class Method Details

.added(filename, new_info) ⇒ Object

Create an added file change



17
18
19
# File 'lib/fontist/indexes/directory_change.rb', line 17

def added(filename, new_info)
  new(ADDED, filename, nil, new_info)
end

.diff(old_snapshot, new_snapshot) ⇒ Object

Compare two snapshots and detect changes Returns: Array of DirectoryChange objects



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fontist/indexes/directory_change.rb', line 38

def diff(old_snapshot, new_snapshot)
  changes = []

  # Check for added and modified files
  new_snapshot.files.each do |new_file|
    filename = new_file[:filename]
    old_file = old_snapshot.file_info(filename)

    if old_file.nil?
      changes << added(filename, new_file)
    elsif file_modified?(old_file, new_file)
      changes << modified(filename, old_file, new_file)
    end
  end

  # Check for removed files
  old_snapshot.files.each do |old_file|
    filename = old_file[:filename]

    unless new_snapshot.has_file?(filename)
      changes << removed(filename, old_file)
    end
  end

  changes
end

.modified(filename, old_info, new_info) ⇒ Object

Create a modified file change



22
23
24
# File 'lib/fontist/indexes/directory_change.rb', line 22

def modified(filename, old_info, new_info)
  new(MODIFIED, filename, old_info, new_info)
end

.removed(filename, old_info) ⇒ Object

Create a removed file change



27
28
29
# File 'lib/fontist/indexes/directory_change.rb', line 27

def removed(filename, old_info)
  new(REMOVED, filename, old_info, nil)
end

.unchanged(filename, info) ⇒ Object

Create an unchanged file change



32
33
34
# File 'lib/fontist/indexes/directory_change.rb', line 32

def unchanged(filename, info)
  new(UNCHANGED, filename, info, info)
end

Instance Method Details

#added?Boolean

Query methods for change type

Returns:

  • (Boolean)


85
86
87
# File 'lib/fontist/indexes/directory_change.rb', line 85

def added?
  @change_type == ADDED
end

#modified?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/fontist/indexes/directory_change.rb', line 89

def modified?
  @change_type == MODIFIED
end

#removed?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/fontist/indexes/directory_change.rb', line 93

def removed?
  @change_type == REMOVED
end

#to_hObject

Convert to hash for serialization



102
103
104
105
106
107
108
109
# File 'lib/fontist/indexes/directory_change.rb', line 102

def to_h
  {
    change_type: @change_type,
    filename: @filename,
    old_info: @old_info,
    new_info: @new_info,
  }
end

#unchanged?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/fontist/indexes/directory_change.rb', line 97

def unchanged?
  @change_type == UNCHANGED
end