Skip to content

Instantly share code, notes, and snippets.

@jconstable
Created October 22, 2009 00:14
Show Gist options
  • Select an option

  • Save jconstable/215597 to your computer and use it in GitHub Desktop.

Select an option

Save jconstable/215597 to your computer and use it in GitHub Desktop.
module UserMods
module Tutorials
TUTORIALS = {
# define the flows, and an array of symbols for the steps of each flow here
:intro_flow => [
:welcome,
:buy_pet,
:pet_chore
]
}
TUTORIAL_COMPLETE = :completed
MAX_TUTORIAL_STEPS = 31
def progress_for(tutorial)
flow = get_flow_for(tutorial)
(1..([MAX_TUTORIAL_STEPS, TUTORIALS[tutorial].length].min)).each do |step_number|
# return the index of the first step that has not been completed
return TUTORIALS[tutorial][step_number-1] if (flow.bitfield & (2**step_number-1)) != (2**step_number-1)
end
return TUTORIAL_COMPLETE
end
def update_progress_for(tutorial, step)
flow = get_flow_for(tutorial)
index = TUTORIALS[tutorial].index(step)
flow.bitfield = flow.bitfield | (2**index) and flow.save! unless index < 0 or index > TUTORIALS[tutorial].size
end
private
def get_flow_for(tutorial)
self.tutorial_flow.first || begin
TutorialFlow.create!(:user_id => self.id, :name => tutorial.to_s, :bitfield => 0)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment