Class: Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/sync-strings.rb

Instance Method Summary collapse

Constructor Details

#initialize(a_filename, b_filename) ⇒ Synchronizer

Returns a new instance of Synchronizer.



26
27
28
29
30
31
32
33
34
35
# File 'lib/sync-strings.rb', line 26

def initialize(a_filename, b_filename)
    @a_filename = a_filename
    @b_filename = b_filename

    @a_hash = Apfel.parse(@a_filename).to_hash(with_comments: false)
    @b_hash = Apfel.parse(@b_filename).to_hash(with_comments: false)

    @a_subtract_b = difference(@a_hash, @b_hash)
    @b_subtract_a = difference(@b_hash, @a_hash)
end

Instance Method Details

#key_differencesObject



44
45
46
# File 'lib/sync-strings.rb', line 44

def key_differences()
    @a_subtract_b + @b_subtract_a
end

#key_differences?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/sync-strings.rb', line 48

def key_differences?()
    !@a_subtract_b.empty? || !@b_subtract_a.empty?
end


37
38
39
40
41
42
# File 'lib/sync-strings.rb', line 37

def print_stats()
    puts "File A: #{@a_filename}"
    puts "File B: #{@b_filename}"
    puts "A - B = #{@a_subtract_b.length}"
    puts "B - A = #{@b_subtract_a.length}"
end

#syncObject



52
53
54
55
# File 'lib/sync-strings.rb', line 52

def sync()
    add(@b_subtract_a, @a_hash)
    add(@a_subtract_b, @b_hash)
end

#writeObject



57
58
59
60
61
# File 'lib/sync-strings.rb', line 57

def write()
    writer = HashStringFileWriter.new
    writer.write(@a_hash, @a_filename)
    writer.write(@b_hash, @b_filename)
end