Class: GoogleContacts::Proxies::Array

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

Instance Method Summary collapse

Constructor Details

#initialize(parent, options) ⇒ Array

Returns a new instance of Array.



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

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

  reinitialize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Extract href from arguments if the operation changes the contents of the array.



39
40
41
42
43
44
45
46
47
# File 'lib/google_contacts/proxies/array.rb', line 39

def method_missing(sym, *args, &blk)
  if [:<<, :push, :+, :-, :concat].include?(sym)
    args = href_from_items(*args)
  end

  result = @new.send(sym, *args, &blk)
  @new = sanitize(@new)
  result
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  @current != @new
end

#reinitializeObject



12
13
14
15
16
17
18
19
# File 'lib/google_contacts/proxies/array.rb', line 12

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

  # create a deep copy
  @new = @current.map { |item| item.dup }
end

#replace(content) ⇒ Object



32
33
34
# File 'lib/google_contacts/proxies/array.rb', line 32

def replace(content)
  @new = sanitize([content].flatten)
end

#synchronizeObject



25
26
27
28
29
30
# File 'lib/google_contacts/proxies/array.rb', line 25

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