Class: Virginity::Vcard::Patching::Diff

Inherits:
Patch
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/virginity/vcard/patching.rb

Overview

a Diff is a collection of changes, I think we can even nest them which might be a cool way of combining them

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Patch

#field_from_query, normalize_vcard!, query_for_line, query_from_string

Constructor Details

#initialize(*changes) ⇒ Diff

Returns a new instance of Diff.



93
94
95
# File 'lib/virginity/vcard/patching.rb', line 93

def initialize(*changes)
  @changes = changes
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



89
90
91
# File 'lib/virginity/vcard/patching.rb', line 89

def changes
  @changes
end

Class Method Details

.diff(before_card, after_card) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/virginity/vcard/patching.rb', line 97

def self.diff(before_card, after_card)
  patch = Diff.new
  before, after = before_card.deep_copy, after_card.deep_copy
  normalize_vcard!(before)
  normalize_vcard!(after)
  before.lines.each do |line|
    # if the exact same line is in after then we can stop processing this line
    # this will of course always happen for begin:vcard, end:vcard
    next unless after.delete(line).empty?
    q = query_from_string(line)
    if x = after.find_first(q)
      patch << Update.diff_lines(line, x, :query => q)
      after.delete(x)
    #else if "there is a line with just one or 2 characters difference in the value" or "only insignificant characters changed, like dashes in telephone numbers"
      # patch << Update.diff_lines(line, x, :query => q)
      # after.delete(x)
    else
      patch << Remove.new(q)
    end
  end
  # what is left in after should be added
  after.lines.each { |line| patch << Add.new(line.to_s) }
  patch
end

Instance Method Details

#apply(vcard) ⇒ Object



122
123
124
125
# File 'lib/virginity/vcard/patching.rb', line 122

def apply(vcard)
  Patch::normalize_vcard!(vcard)
  @changes.each { |change| change.apply(vcard) }
end

#pretty_print(q) ⇒ Object



131
132
133
134
135
# File 'lib/virginity/vcard/patching.rb', line 131

def pretty_print(q)
  @changes.each do |change|
    pp change
  end
end

#to_sObject



127
128
129
# File 'lib/virginity/vcard/patching.rb', line 127

def to_s
  @changes.join("\n")
end