Class: Memories::VersionsProxy
- Inherits:
-
Object
- Object
- Memories::VersionsProxy
- Defined in:
- lib/memories/versions_proxy.rb
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #count ⇒ Object
-
#initialize(doc) ⇒ VersionsProxy
constructor
A Memories::VersionsProxy is automatically initialized, memoized, and returned when you call the ‘versions` method on your document.
- #method_missing(method_name, *args, &block) ⇒ Object
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[0] #==> nil
doc.versions[1].revision #==> 'rev-1-io329uidlrew098320'
doc.versions[1].instance.name #==> '2001'
doc.versions[1].milestone? #==> false
doc.versions[2].instance.name #==> '2001: A Space Odyssey'
doc.versions[2].milestone? #==> true
doc.versions[2].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 |
#count ⇒ Object
24 25 26 27 |
# File 'lib/memories/versions_proxy.rb', line 24 def count populate_proxies @versions.count == 0 ? 0 : @versions.count - 1 end |