Class: Memories::VersionsProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/memories/versions_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ VersionsProxy

A Memories::VersionsProxy is automatically initialized, memoized, and returned when you call the versions method on your document. doc = Book.create :name => '2001' doc.name = '2001: A Space Odyssey' doc.milestone! doc.versions.class # ==> ::Memories::VersionsProxy

You can access versions by version number doc.versions #==> nil doc.versions.revision #==> 'rev-1-io329uidlrew098320' doc.versions.instance.name #==> '2001' doc.versions.milestone? #==> false doc.versions.instance.name #==> '2001: A Space Odyssey' doc.versions.milestone? #==> true doc.versions.version_number # ==> 2



19
20
21
22
# File 'lib/memories/versions_proxy.rb', line 19

def initialize(doc)
  @doc = doc
  @versions = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



39
40
41
42
# File 'lib/memories/versions_proxy.rb', line 39

def method_missing(method_name, *args, &block)
  populate_proxies
  @versions.send(method_name, *args, &block)
end

Instance Method Details

#[](arg) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/memories/versions_proxy.rb', line 29

def [](arg)
  populate_proxies
  
  if arg.kind_of?(String)
    @versions[@doc.version_number arg]
  else
    @versions[arg]
  end
end

#countObject



24
25
26
27
# File 'lib/memories/versions_proxy.rb', line 24

def count
  populate_proxies
  @versions.count == 0 ? 0 : @versions.count - 1
end