Class: GoogleContacts::Proxies::Emails

Inherits:
ActiveSupport::BasicObject
Defined in:
lib/google_contacts/proxies/emails.rb

Defined Under Namespace

Classes: Email

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Emails

Returns a new instance of Emails.



4
5
6
7
# File 'lib/google_contacts/proxies/emails.rb', line 4

def initialize(parent)
  @parent = parent
  reinitialize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object (private)



67
68
69
70
71
72
73
# File 'lib/google_contacts/proxies/emails.rb', line 67

def method_missing(sym, *args, &blk)
  if [:size, :delete].include?(sym)
    @new.send(sym, *args, &blk)
  else
    super
  end
end

Instance Method Details

#<<(address) ⇒ Object



39
40
41
42
# File 'lib/google_contacts/proxies/emails.rb', line 39

def <<(address)
  raise "Duplicate address" if @new[address]
  add(address)
end

#[](address) ⇒ Object



44
45
46
# File 'lib/google_contacts/proxies/emails.rb', line 44

def [](address)
  @new[address] || add(address)
end

#changed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/google_contacts/proxies/emails.rb', line 21

def changed?
  @current != @new
end

#inspectObject



55
56
57
# File 'lib/google_contacts/proxies/emails.rb', line 55

def inspect
  @new.values.inspect
end

#primaryObject



25
26
27
# File 'lib/google_contacts/proxies/emails.rb', line 25

def primary
  @new.values.find { |email| email.primary? } || @new.values.first
end

#primary!(address) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/google_contacts/proxies/emails.rb', line 29

def primary!(address)
  @new.each do |key, email|
    if key == address
      email.primary = true
    else
      email.delete(:primary)
    end
  end
end

#reinitializeObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/google_contacts/proxies/emails.rb', line 9

def reinitialize
  @current = ::Hash[*@parent.xml.xpath("./gd:email").map do |entry|
    email = Email.new(self, entry.attributes)
    [email.address, email]
  end.flatten]

  # create a deep copy
  @new = ::Hash[*@current.map do |k,v|
    [k.dup, v.dup]
  end.flatten]
end

#synchronizeObject



48
49
50
51
52
53
# File 'lib/google_contacts/proxies/emails.rb', line 48

def synchronize
  @parent.remove_xml("./gd:email")
  @new.each_pair do |address, email|
    @parent.insert_xml('gd:email', email)
  end
end