Skip to content

Commit

Permalink
clean up some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed May 27, 2008
1 parent c9246b1 commit 2ef3969
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions doctest/lifecycles.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Hobo lifecycles is a mechanism for declaring the life-cycle of a model in a natu

First the junk to get us started:

>> require 'rubygems'
>> require 'activerecord'
>> require 'hobofields'
>> Dependencies.load_paths << '.'
>> Dependencies.mechanism = :require
>> require 'rubygems'
>> require 'activerecord'
>> require 'hobofields'
>> Dependencies.load_paths << '.'
>> Dependencies.mechanism = :require
>> require 'hobo'
>> require 'hobo/model'
>> ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
:database => "lifecycle_doctest")
>> ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
:database => "lifecycle_doctest")


A user model for our example:
Expand All @@ -35,7 +35,7 @@ A user model for our example:
name :string
end
end

Now the friendship. For now we'll just declare the *invite* part of the lifecycle. We first declare the *states* -- there's only one for now. We then declare the *invite* action. This is the action that first creates the friendship, so we declare it with `create`:

>>
Expand All @@ -49,25 +49,25 @@ Now the friendship. For now we'll just declare the *invite* part of the lifecycl
create :requester, :request, :params => [ :requestee ], :become => :requested
end
end

Now let's get the DB ready:

>> require 'hobo_fields/migration_generator'
>> up, down = HoboFields::MigrationGenerator.run
>> ActiveRecord::Migration.class_eval up
>> User.delete_all
>> Friendship.delete_all

We need some users to be friends:

>> tom = User.create(:name => "Tom")
>> bob = User.create(:name => "Bob")

Tom is allowed to request friendship:

>> Friendship::Lifecycle.can_request?(tom)
=> true

Tom does so:

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
Expand All @@ -77,15 +77,15 @@ Tom does so:
=> "Bob"
>> f.state
=> "requested"

To continue modeling the friendship lifecycle, we add some *transitions*:

>>
class Friendship
lifecycle do
state :active
transition :requestee, :accept, { :requested => :active }
transition :requestee, :reject, { :requested => :destroy }
class Friendship
lifecycle do
state :active
transition :requestee, :accept, { :requested => :active }
transition :requestee, :reject, { :requested => :destroy }
end
end

Expand All @@ -100,7 +100,7 @@ We can test which transitions are available:
>> f.lifecycle.available_transitions_for(tom).*.name
=> []
>> f.lifecycle.available_transitions_for(bob).*.name
["accept", "reject"]
=> ["accept", "reject"]
>> f.lifecycle.can_accept?(tom)
=> false
>> f.lifecycle.can_reject?(tom)
Expand All @@ -126,7 +126,7 @@ And now there's nowhere to go:
Cleanup

>> Friendship.delete_all

Let's try a rejected friendship:

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
Expand All @@ -139,7 +139,7 @@ Let's try a rejected friendship:
>> f.lifecycle.reject(bob)
>> Friendship.count
=> 0

Cleanup

>> User.delete_all
Expand Down Expand Up @@ -181,13 +181,13 @@ Here is the entire lifecycle
transition :requestee, :accept, { :requested => :active } do
$emails << "Dear #{requester.name}, #{requestee.name} is now your friend :-)"
end

transition :requestee, :reject, { :requested => :destroy } do
$emails << "Dear #{requester.name}, #{requestee.name} blew you out :-("
end

transition :requestee, :ignore, { :requested => :ignored }

transition :requester, :retract, { :requested => :destroy } do
$emails << "Dear #{requestee.name}, #{requester.name} reconsidered"
end
Expand All @@ -198,7 +198,7 @@ Here is the entire lifecycle
end

end

Check the simple accept still works, and sends emails

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
Expand All @@ -211,14 +211,14 @@ Check the simple accept still works, and sends emails
=> true

Rejection:

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
>> f.lifecycle.reject(bob)
>> $emails.last
=> "Dear Tom, Bob blew you out :-("
>> f.state
=> "destroy"

Retraction:

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
Expand All @@ -228,18 +228,18 @@ Retraction:
>> $emails.last
=> "Dear Bob, Tom reconsidered"
>> f.lifecycle.active?
Ignoring

Ignoring

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
>> $emails = []
>> f.lifecycle.ignore(bob)
>> $emails # Ignoring shouldn't send any email
=> []
>> f.state
=> "ignored"
Requester cancels

Requester cancels

>> f = Friendship::Lifecycle.request(tom, :requestee => bob)
>> f.lifecycle.can_cancel?(tom)
Expand All @@ -248,7 +248,7 @@ Requester cancels
>> f.lifecycle.cancel(tom)
>> f.state
=> "destroy"




Expand Down

0 comments on commit 2ef3969

Please sign in to comment.