Class: Memories::MilestonesProxy

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

Overview

As of version 0.2.0, Memories also supports milestones. Milestones are special versions that you want to flag in some way. For example, suppose you were creating a content management system, and every time someone publishes an article to the website, you want to flag the version they published as a milestone.

class Article < CouchRest::Model::Base
  include Memories
  use_database SOME_DATABASE

  property :title
  property :author
  property :body

  def publish!
    # .... publishing logic
  end
end

a = Article.create(
  :title => "Memories gem makes versioning simple", 
  :author => "moonmaster9000", 
  :body => <<-ARTICLE
    Check it out at http://github.com/moonmaster9000/memories
  ARTICLE
)
a.save
a.publish!
a.current_version #==> 1 
a.milestone! do
  name "First publish."
  notes "Passed all relevant editing. Signed off by moonmaster10000"
end

Notice that we annotated our milestone; we gave it a name, and some notes. You can annotate with whatever properties you desire. The annotation do block is entirely optional. Now that we’ve created a milestone, let’s inspect it via the ‘milestones` array:

a.milestones.count #==> 1
a.milestones.last.version # ==> 'rev-1-reiwurieowu9340289032804932krew'
a.milestones.last.version_number # ==> 1
a.milestones.last.annotations.name ==> "First publish."
a.milestones.last.annotations.notes ==> "Passed all relevant editing. Signed off by moonmaster10000"

Now, let’s imagine that we’ve made some more edits / saves to the document, but they don’t get approved. Now we want to revert to the version the document was at at the first milestone. How do we do that? Simple!

a.revert_to_milestone! 1

And now our document properties are back to the where they were when we first published the document.

If you want to access the data from a milestone, simply use the “data” method:

a.milestones.first.data.title #==> returns the "title" attribute on the first milestone
a.milestones.each do |m|
  puts "Version: " + m.version
  puts "Title: " + m.data.title
end

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ MilestonesProxy

Returns a new instance of MilestonesProxy.



58
59
60
61
# File 'lib/memories/milestones_proxy.rb', line 58

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



63
64
65
66
# File 'lib/memories/milestones_proxy.rb', line 63

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