Class: Blockbuster::Comparator

Inherits:
Object
  • Object
show all
Includes:
FileHelpers, OutputHelpers
Defined in:
lib/blockbuster/comparator.rb

Overview

Data store for files, sources, states, and checksums

Constant Summary collapse

CONTENT =
'content'.freeze
SOURCE =
'source'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OutputHelpers

#silent_puts

Methods included from FileHelpers

#file_digest, #tar_digest

Constructor Details

#initialize(configuration) ⇒ Comparator

Returns a new instance of Comparator.



12
13
14
15
16
17
18
# File 'lib/blockbuster/comparator.rb', line 12

def initialize(configuration)
  @configuration       = configuration
  @inventory           = {}
  @edited              = []
  @current_delta_files = []
  @deleted             = []
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



10
11
12
# File 'lib/blockbuster/comparator.rb', line 10

def configuration
  @configuration
end

#current_delta_filesObject (readonly)

Returns the value of attribute current_delta_files.



10
11
12
# File 'lib/blockbuster/comparator.rb', line 10

def current_delta_files
  @current_delta_files
end

#deletedObject (readonly)

Returns the value of attribute deleted.



10
11
12
# File 'lib/blockbuster/comparator.rb', line 10

def deleted
  @deleted
end

#editedObject (readonly)

Returns the value of attribute edited.



10
11
12
# File 'lib/blockbuster/comparator.rb', line 10

def edited
  @edited
end

#inventoryObject (readonly)

Returns the value of attribute inventory.



10
11
12
# File 'lib/blockbuster/comparator.rb', line 10

def inventory
  @inventory
end

Instance Method Details

#add(key, value, source) ⇒ Object



20
21
22
# File 'lib/blockbuster/comparator.rb', line 20

def add(key, value, source)
  inventory[key] = { CONTENT => value, SOURCE => source }
end

#any_deleted?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/blockbuster/comparator.rb', line 78

def any_deleted?
  if configuration.deltas_disabled? && !@deleted.empty?
    silent_puts "Cassettes deleted: #{@deleted}"
    return true
  elsif configuration.deltas_enabled? && !(current_delta_files & @deleted).empty?
    silent_puts "Cassettes deleted: #{@deleted}"
    return true
  end

  false
end

#compare(key, new_digest) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/blockbuster/comparator.rb', line 47

def compare(key, new_digest)
  digest = inventory[key]

  if digest.nil?
    silent_puts "New cassette: #{key}"
    return true
  elsif digest[CONTENT] != new_digest
    silent_puts "Cassette changed: #{key}"
    return true
  end
end

#delete(key) ⇒ Object



24
25
26
# File 'lib/blockbuster/comparator.rb', line 24

def delete(key)
  inventory.delete(key)
end

#edited?(file) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/blockbuster/comparator.rb', line 36

def edited?(file)
  (edited + current_delta_files).include?(file)
end

#keysObject



28
29
30
# File 'lib/blockbuster/comparator.rb', line 28

def keys
  inventory.keys
end

#present?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/blockbuster/comparator.rb', line 32

def present?
  !keys.empty?
end

#rewind?(files) ⇒ Boolean

rubocop:disable Metrics/AbcSize

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/blockbuster/comparator.rb', line 59

def rewind?(files) # rubocop:disable Metrics/AbcSize
  base_files = []

  files.each do |file|
    next unless File.file?(file)

    key = configuration.key_from_path(file)
    base_files << key

    edited << key if compare(key, file_digest(file))
  end

  @deleted = keys - base_files

  return true if any_deleted?

  !edited.empty?
end

#store_current_delta_filesObject



40
41
42
43
44
45
# File 'lib/blockbuster/comparator.rb', line 40

def store_current_delta_files
  inventory.each do |k, v|
    scrubbed = Blockbuster::Delta.file_name_without_timestamp(v[SOURCE])
    current_delta_files << k if scrubbed == configuration.current_delta_name
  end
end