progression 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,17 +28,48 @@ either coupled with ActiveRecord or ActionController in some way.
28
28
 
29
29
  user = User.new
30
30
  user.registration_progress.completed_steps # => []
31
- user.registration_progress.percentage_complete # => 0.0
32
- user.next_step.name # => :step_1
31
+ user.registration_progress.percentage_completed # => 0.0
32
+ user.registration_progress.next_step.name # => :step_1
33
33
 
34
34
  user.first_name = "Michael"
35
35
  user.last_name = "Jordan"
36
36
  user.registration_progress.completed_steps # => [:step_1]
37
- user.registration_progress.percentage_complete # => 50.0
38
- user.next_step.name # => :step_2
37
+ user.registration_progress.percentage_completed # => 50.0
38
+ user.registration_progress.next_step.name # => :step_2
39
39
 
40
40
  ...
41
41
 
42
+ ## Custom Percentages
43
+
44
+ progression provides support for defining your own percentages for
45
+ each step rather than having them predefined as (1 / number_of_steps).
46
+
47
+ class User < Struct.new(:first_name, :last_name, :email, :phone)
48
+
49
+ include Progression
50
+
51
+ progression :registration do
52
+ step :important_step, :percentage => 50.0 do
53
+ first_name.present? && last_name.present?
54
+ end
55
+
56
+ step :step_2, :percentage => 25.0 do
57
+ email.present?
58
+ end
59
+
60
+ step :step_3, :percentage => 25.0 do
61
+ phone.present?
62
+ end
63
+ end
64
+ end
65
+
66
+ user = User.new
67
+ user.first_name = "Michael"
68
+ user.last_name = "Jordan"
69
+ user.registration_progress.completed_steps # => [:important_step]
70
+ user.registration_progress.percentage_completed # => 50.0
71
+ user.registration_progress.next_step.name # => :step_2
72
+
42
73
 
43
74
  ## Utility classes
44
75
 
@@ -68,6 +99,16 @@ either coupled with ActiveRecord or ActionController in some way.
68
99
  progress.percentage_completed # => 100.0
69
100
  progress.next_step # => nil
70
101
 
102
+ ## Rails / ActiveRecord
103
+
104
+ You can use progression as a gem or a plugin in your Rails application. progression does not make any assumptions about the objects that you are interested in extending.
105
+
106
+ Simply add an initializer:
107
+
108
+ config/initializers/progression.rb
109
+
110
+ ActiveRecord::Base.send(:include, Progression)
111
+
71
112
  ## Note on Patches/Pull Requests
72
113
 
73
114
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/lib/progression.rb CHANGED
@@ -24,17 +24,24 @@ module Progression
24
24
 
25
25
  def initialize(&block)
26
26
  @steps = []
27
+ @percentage_values = {}
27
28
  instance_exec &block if block_given?
28
29
  end
29
30
 
30
31
  def progress_for(object)
31
- Progress.new(object, steps)
32
+ Progress.new(object, self)
33
+ end
34
+
35
+ def percentage_value_for(step)
36
+ @percentage_values[step] || (1 / steps.size.to_f) * 100
32
37
  end
33
38
 
34
39
  private
35
40
 
36
- def step(name, &block)
37
- @steps << Step.new(name, &block)
41
+ def step(name, options = {}, &block)
42
+ step = Step.new(name, &block)
43
+ @steps << step
44
+ @percentage_values[step] = options[:percentage] if options[:percentage]
38
45
  end
39
46
 
40
47
  end
@@ -43,9 +50,10 @@ module Progression
43
50
 
44
51
  attr_reader :steps
45
52
 
46
- def initialize(object, steps)
53
+ def initialize(object, progression)
47
54
  @object = object
48
- @steps = steps
55
+ @progression = progression
56
+ @steps = progression.steps
49
57
  end
50
58
 
51
59
  def completed_steps
@@ -55,7 +63,9 @@ module Progression
55
63
  end
56
64
 
57
65
  def percentage_completed
58
- (completed_steps.size / steps.size.to_f) * 100
66
+ completed_steps.inject(0) do |percentage_completed, step|
67
+ percentage_completed + @progression.percentage_value_for(step)
68
+ end
59
69
  end
60
70
 
61
71
  def next_step
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{progression}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Guterl"]
12
+ s.date = %q{2010-04-22}
13
+ s.description = %q{progression provides a set of simple utility classes and a DSL for
14
+ measuring an objects progress through a progression of steps. This is
15
+ especially useful for defining and calculating profile completion
16
+ status in a social application.
17
+
18
+ There are a few existing solutions similar to progression, but they
19
+ try to do too much. The other options that I am familiar with are
20
+ either coupled with ActiveRecord or ActionController in some way.
21
+ }
22
+ s.email = %q{mguterl@gmail.com}
23
+ s.extra_rdoc_files = [
24
+ "LICENSE",
25
+ "README.md"
26
+ ]
27
+ s.files = [
28
+ ".document",
29
+ ".gitignore",
30
+ "LICENSE",
31
+ "README.md",
32
+ "Rakefile",
33
+ "VERSION",
34
+ "lib/progression.rb",
35
+ "progression.gemspec",
36
+ "rails/init.rb",
37
+ "spec/progression_spec.rb",
38
+ "spec/spec.opts",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+ s.homepage = %q{http://github.com/mguterl/progression}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.6}
45
+ s.summary = %q{progression provides a set of simple utility classes and a DSL for measuring an objects progress through a progression of steps}
46
+ s.test_files = [
47
+ "spec/progression_spec.rb",
48
+ "spec/spec_helper.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ end
63
+ end
64
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'progression'
@@ -1,6 +1,6 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- class Profile < Struct.new(:first_name, :last_name)
3
+ class Profile < Struct.new(:first_name, :last_name, :email)
4
4
 
5
5
  include Progression
6
6
 
@@ -16,6 +16,21 @@ class Profile < Struct.new(:first_name, :last_name)
16
16
 
17
17
  end
18
18
 
19
+ progression :registration do
20
+
21
+ step :step_1, :percentage => 25.0 do
22
+ !first_name.blank?
23
+ end
24
+
25
+ step :step_2, :percentage => 25.0 do
26
+ !last_name.blank?
27
+ end
28
+
29
+ step :step_3, :percentage => 50.0 do
30
+ !email.blank?
31
+ end
32
+ end
33
+
19
34
  # def self.profile_progression
20
35
  # @progression ||= Progression::Progression.new do
21
36
  # steps << Progression::Step.new(:step_1) { !first_name.blank? }
@@ -94,4 +109,49 @@ describe "Progression" do
94
109
 
95
110
  end
96
111
 
112
+ describe "explicit percentages" do
113
+
114
+ before do
115
+ @profile = Profile.new
116
+ @progression = @profile.registration_progress
117
+ end
118
+
119
+ it 'should return a list of steps' do
120
+ @progression.should have(3).steps
121
+ end
122
+
123
+ it 'should return a list of completed steps' do
124
+ @progression.should have(0).completed_steps
125
+ end
126
+
127
+ it 'should return the percentage completed' do
128
+ @progression.percentage_completed.should == 0.0
129
+ end
130
+
131
+ it 'should return the next step to be completed' do
132
+ @progression.next_step.name.should == :step_1
133
+ end
134
+
135
+ describe "after last step" do
136
+
137
+ before do
138
+ @profile.email = "foo@bar.com"
139
+ end
140
+
141
+ it 'should return a list of completed steps' do
142
+ @progression.should have(1).completed_steps
143
+ end
144
+
145
+ it 'should return the percentage completed' do
146
+ @progression.percentage_completed.should == 50.0
147
+ end
148
+
149
+ it 'should return the next step to be completed' do
150
+ @progression.next_step.name.should == :step_1
151
+ end
152
+
153
+ end
154
+
155
+ end
156
+
97
157
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Guterl
@@ -57,6 +57,8 @@ files:
57
57
  - Rakefile
58
58
  - VERSION
59
59
  - lib/progression.rb
60
+ - progression.gemspec
61
+ - rails/init.rb
60
62
  - spec/progression_spec.rb
61
63
  - spec/spec.opts
62
64
  - spec/spec_helper.rb