Class: GoogleContacts::Proxies::Hash

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

Instance Method Summary collapse

Constructor Details

#initialize(parent, options) ⇒ Hash

Returns a new instance of Hash.



4
5
6
7
8
9
10
11
# File 'lib/google_contacts/proxies/hash.rb', line 4

def initialize(parent, options)
  @parent = parent
  @tag    = options[:tag]
  @key    = options[:key]
  @value  = options[:value]

  reinitialize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



36
37
38
# File 'lib/google_contacts/proxies/hash.rb', line 36

def method_missing(sym, *args, &blk)
  @new.send(sym, *args, &blk)
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/google_contacts/proxies/hash.rb', line 24

def changed?
  @current != @new
end

#reinitializeObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/google_contacts/proxies/hash.rb', line 13

def reinitialize
  @current = HashWithIndifferentAccess.new
  @parent.xml.xpath("./#{@tag}").map do |entry|
    @current[entry[@key]] = entry[@value]
  end

  # create a deep copy
  @new = HashWithIndifferentAccess.new
  @current.each { |k,v| @new[k.dup] = v.dup }
end

#synchronizeObject



28
29
30
31
32
33
# File 'lib/google_contacts/proxies/hash.rb', line 28

def synchronize
  @parent.remove_xml("./#{@tag}")
  @new.each_pair do |key, value|
    @parent.insert_xml(@tag, @key => key, @value => value)
  end
end