Class: Virginity::Vcard::Patching::Update

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

Overview

to update field matching the query for automatically generated diffs this will usually only entail updates to params there is an alternative action that will be executed if there is no field to update

Constant Summary collapse

COLON =
":"
SEMICOLON =
";"

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(query, updates, alternative = nil) ⇒ Update

Returns a new instance of Update.



186
187
188
189
190
# File 'lib/virginity/vcard/patching.rb', line 186

def initialize(query, updates, alternative = nil)
  @query = query
  @updates = updates
  @alternative = alternative
end

Instance Attribute Details

#alternativeObject

Returns the value of attribute alternative.



185
186
187
# File 'lib/virginity/vcard/patching.rb', line 185

def alternative
  @alternative
end

#queryObject

Returns the value of attribute query.



185
186
187
# File 'lib/virginity/vcard/patching.rb', line 185

def query
  @query
end

#updatesObject

Returns the value of attribute updates.



185
186
187
# File 'lib/virginity/vcard/patching.rb', line 185

def updates
  @updates
end

Class Method Details

.diff_lines(before, after, options = {}) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/virginity/vcard/patching.rb', line 192

def self.diff_lines(before, after, options = {})
  Update.new(
    options[:query] || Patch::query_from_string(before),
    line_diff(before, after),
    Add.new(after)
  )
end

Instance Method Details

#apply(vcard) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/virginity/vcard/patching.rb', line 220

def apply(vcard)
  to_update = vcard.where(@query)
  if to_update.empty?
    # if the field has been deleted in the meantime on the server, the patch adds it again
    # to_update << vcard.add_field(field_from_query(@query)) if to_update.empty?
    @alternative.apply(vcard) unless @alternative.nil?
  else
    to_update.each { |field| update_field!(field) }
  end
end

#pretty_print(q) ⇒ Object



238
239
240
241
242
243
# File 'lib/virginity/vcard/patching.rb', line 238

def pretty_print(q)
  q.text "Replace #{query} with #{@updates.map {|k,v| "#{k}(#{v.inspect})" }.join(", ")})"
  if @alternative
    q.text " else { #{alternative} }"
  end
end

#to_sObject



231
232
233
234
235
236
# File 'lib/virginity/vcard/patching.rb', line 231

def to_s
  s = "Update(#{@query.inspect}, #{@updates.map {|k,v| "#{k}(#{v.inspect})" }.join(", ")})"
  if @alternative
    s << " else { #{alternative} }"
  end
end

#update_field!(field) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/virginity/vcard/patching.rb', line 202

def update_field!(field)
  @updates.each_pair do |key, value|
    case key
    when :value
      field.raw_value = value
    when :add_param
      # params_from_string returns an array of params
      field.params += Param::params_from_string(value + COLON)
    when :remove_param
      Param::params_from_string(value + COLON).each do |param_to_delete|
        field.params.delete_if { |p| p == param_to_delete }
      end
    else
      raise IllegalPatch, "#{key}, #{value}"
    end
  end
end