Class: Virginity::Vcard::Patching::Patch

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

Overview

the base class

Direct Known Subclasses

Add, Diff, Remove, Update

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize_vcard!(vcard) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/virginity/vcard/patching.rb', line 70

def self.normalize_vcard!(vcard)
  vcard.clean_orgs! # orgs are of variable length, but ordered, they need normalizing before we can compare them
  # normalize categories and nicknames (actually every unpackable field)
  vcard.fields.each do |field|
    vcard.unpack_field!(field) if field.respond_to?(:unpacked)
    field.clean!
    field.params.sort!
  end
  # remove double lines
  vcard.clean_same_value_fields!
  vcard.add("N") unless vcard.lines_with_name("N").any?
  vcard.clean_name!
  vcard
end

.query_for_line(line) ⇒ Object



39
40
41
# File 'lib/virginity/vcard/patching.rb', line 39

def self.query_for_line(line)
  line.name + ":" + line.raw_value
end

.query_from_string(query_string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/virginity/vcard/patching.rb', line 43

def self.query_from_string(query_string)
  q = Field.parse(query_string)
  # at this moment all our queries are like "name : raw_value"
  query = { :name => q.name }
  if q.respond_to? :values
    query[:values] = q.values.to_a
  elsif q.respond_to? :text
    query[:text] = q.text
  else
    query[:raw_value] = q.raw_value
  end
  query
end

Instance Method Details

#apply(vcard) ⇒ Object



66
67
68
# File 'lib/virginity/vcard/patching.rb', line 66

def apply(vcard)
  raise "responsibility of subclass"
end

#field_from_query(query) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/virginity/vcard/patching.rb', line 57

def field_from_query(query)
  value = query.reject { |k, v| k == :name }
  Field.named(query[:name]) do |f|
    value.each do |k,v|
      f.send(k+'=', v)
    end
  end
end