Class: Prick::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/prick/diff.rb

Overview

Database diff

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db1, db2) ⇒ Diff

Returns a new instance of Diff.



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

def initialize(db1, db2)
  constrain db1, String
  constrain db2, String
  @db1, @db2 = db1, db2
  do_diff
  split_on_table_changes
end

Instance Attribute Details

#after_table_changesObject (readonly)

Returns the value of attribute after_table_changes.



10
11
12
# File 'lib/prick/diff.rb', line 10

def after_table_changes
  @after_table_changes
end

#before_table_changesObject (readonly)

Diff split into before/after table changes



8
9
10
# File 'lib/prick/diff.rb', line 8

def before_table_changes
  @before_table_changes
end

#diffObject (readonly)

Diff as a list of lines



5
6
7
# File 'lib/prick/diff.rb', line 5

def diff
  @diff
end

#table_changesObject (readonly)

Returns the value of attribute table_changes.



9
10
11
# File 'lib/prick/diff.rb', line 9

def table_changes
  @table_changes
end

Class Method Details

.same?(db1, db2) ⇒ Boolean

Returns:

  • (Boolean)


20
# File 'lib/prick/diff.rb', line 20

def self.same?(db1, db2) Diff.new(db1, db2).same? end

Instance Method Details

#same?Boolean

Return true if the two databases are equal. Named #same? to avoid name collision with the built-in #equal? method

Returns:

  • (Boolean)


24
# File 'lib/prick/diff.rb', line 24

def same?() diff.empty? end

#write(file1, file2 = nil, file3 = nil, mark: nil) ⇒ Object

:call-seq:

write(file, mark: true)
write(file1, file2, file3, mark: false)

Write the diff between the databases to the given file(s). Return true if the databases are equal



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prick/diff.rb', line 32

def write(file1, file2 = nil, file3 = nil, mark: nil)
  if file2.nil? && file3.nil?
    file2 = file3 = file1
    mark = mark.nil? ? true : mark
  elsif file2.nil? != file3.nil?
    Prick.failure "Either none or both of `file2` and `file3` should be nil"
  else
    mark = mark.nil? ? false : mark
  end
  write_segment(file1, :before_table_changes, mark: mark)
  write_segment(file2, :table_changes, mark: mark)
  write_segment(file3, :after_table_changes, mark: mark)
end