Class: Virginity::Vcard::Patching::Update
- 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
-
#alternative ⇒ Object
Returns the value of attribute alternative.
-
#query ⇒ Object
Returns the value of attribute query.
-
#updates ⇒ Object
Returns the value of attribute updates.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(vcard) ⇒ Object
-
#initialize(query, updates, alternative = nil) ⇒ Update
constructor
A new instance of Update.
- #pretty_print(q) ⇒ Object
- #to_s ⇒ Object
- #update_field!(field) ⇒ Object
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
#alternative ⇒ Object
Returns the value of attribute alternative.
185 186 187 |
# File 'lib/virginity/vcard/patching.rb', line 185 def alternative @alternative end |
#query ⇒ Object
Returns the value of attribute query.
185 186 187 |
# File 'lib/virginity/vcard/patching.rb', line 185 def query @query end |
#updates ⇒ Object
Returns the value of attribute updates.
185 186 187 |
# File 'lib/virginity/vcard/patching.rb', line 185 def updates @updates end |
Class Method Details
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_s ⇒ Object
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 |