progression 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michael Guterl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # progression
2
+
3
+ progression provides a set of simple utility classes and a DSL for
4
+ measuring an objects progress through a progression of steps. This is
5
+ especially useful for defining and calculating profile completion
6
+ status in a social application.
7
+
8
+ There are a few existing solutions similar to progression, but they
9
+ try to do too much. The other options that I am familiar with are
10
+ either coupled with ActiveRecord or ActionController in some way.
11
+
12
+ ## DSL syntax
13
+
14
+ class User < Struct.new(:first_name, :last_name, :email, :phone)
15
+
16
+ include Progression
17
+
18
+ progression :registration do
19
+ step :step_1 do
20
+ first_name.present? && last_name.present?
21
+ end
22
+
23
+ step :step_2 do
24
+ email.present? && phone.present?
25
+ end
26
+ end
27
+ end
28
+
29
+ user = User.new
30
+ user.registration_progress.completed_steps # => []
31
+ user.registration_progress.percentage_complete # => 0.0
32
+ user.next_step.name # => :step_1
33
+
34
+ user.first_name = "Michael"
35
+ user.last_name = "Jordan"
36
+ user.registration_progress.completed_steps # => [:step_1]
37
+ user.registration_progress.percentage_complete # => 50.0
38
+ user.next_step.name # => :step_2
39
+
40
+ ...
41
+
42
+
43
+ ## Utility classes
44
+
45
+ registration = Progression::Progression.new
46
+ registration.steps << Progression::Step.new(:step_1) { !first_name.blank? && !last_name.blank? }
47
+ registration.steps << Progression::Step.new(:step_2) { !email.blank? && !phone.blank? }
48
+
49
+ User = Struct.new(:first_name, :last_name, :email, :phone)
50
+ user = User.new
51
+
52
+ progress = registration.progress_for(user)
53
+ progress.completed_steps # => []
54
+ progress.percentage_completed # => 0.0
55
+ progress.next_step.name # => :step_1
56
+
57
+ user.first_name = "Michael"
58
+ user.last_name = "Jordan"
59
+
60
+ progress.completed_steps # => [:step_1]
61
+ progress.percentage_completed # => 50.0
62
+ progress.next_step.name # => :step_2
63
+
64
+ user.email = "michael@jordan.com"
65
+ user.phone = "513-347-1111"
66
+
67
+ progress.completed_steps # => [:step_1, :step_2]
68
+ progress.percentage_completed # => 100.0
69
+ progress.next_step # => nil
70
+
71
+ ## Note on Patches/Pull Requests
72
+
73
+ * Fork the project.
74
+ * Make your feature addition or bug fix.
75
+ * Add tests for it. This is important so I don't break it in a
76
+ future version unintentionally.
77
+ * Commit, do not mess with rakefile, version, or history.
78
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
79
+ * Send me a pull request. Bonus points for topic branches.
80
+
81
+ ## Copyright
82
+
83
+ Copyright (c) 2010 Michael Guterl. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "progression"
8
+ gem.summary = %Q{progression provides a set of simple utility classes and a DSL for measuring an objects progress through a progression of steps}
9
+ gem.description = %Q{progression provides a set of simple utility classes and a DSL for
10
+ measuring an objects progress through a progression of steps. This is
11
+ especially useful for defining and calculating profile completion
12
+ status in a social application.
13
+
14
+ There are a few existing solutions similar to progression, but they
15
+ try to do too much. The other options that I am familiar with are
16
+ either coupled with ActiveRecord or ActionController in some way.
17
+ }
18
+ gem.email = "mguterl@gmail.com"
19
+ gem.homepage = "http://github.com/mguterl/progression"
20
+ gem.authors = ["Michael Guterl"]
21
+ gem.add_development_dependency "rspec", ">= 1.2.9"
22
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
+ end
24
+ Jeweler::GemcutterTasks.new
25
+ rescue LoadError
26
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
27
+ end
28
+
29
+ require 'spec/rake/spectask'
30
+ Spec::Rake::SpecTask.new(:spec) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.spec_files = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
36
+ spec.libs << 'lib' << 'spec'
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :spec => :check_dependencies
42
+
43
+ task :default => :spec
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "progression #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,82 @@
1
+ module Progression
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def progression(name, &block)
10
+ (class << self; self; end).send(:define_method, "#{name}_progression") do
11
+ Progression.new(&block)
12
+ end
13
+
14
+ define_method("#{name}_progress") do
15
+ self.class.send("#{name}_progression").progress_for(self)
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ class Progression
22
+
23
+ attr_reader :steps
24
+
25
+ def initialize(&block)
26
+ @steps = []
27
+ instance_exec &block if block_given?
28
+ end
29
+
30
+ def progress_for(object)
31
+ Progress.new(object, steps)
32
+ end
33
+
34
+ private
35
+
36
+ def step(name, &block)
37
+ @steps << Step.new(name, &block)
38
+ end
39
+
40
+ end
41
+
42
+ class Progress
43
+
44
+ attr_reader :steps
45
+
46
+ def initialize(object, steps)
47
+ @object = object
48
+ @steps = steps
49
+ end
50
+
51
+ def completed_steps
52
+ steps.select do |step|
53
+ step.evaluate(@object)
54
+ end
55
+ end
56
+
57
+ def percentage_completed
58
+ (completed_steps.size / steps.size.to_f) * 100
59
+ end
60
+
61
+ def next_step
62
+ steps.find do |step|
63
+ !step.evaluate(@object)
64
+ end
65
+ end
66
+ end
67
+
68
+ class Step
69
+
70
+ attr_reader :name
71
+
72
+ def initialize(name, &block)
73
+ @name, @block = name, block
74
+ end
75
+
76
+ def evaluate(object)
77
+ object.instance_exec &@block
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class Profile < Struct.new(:first_name, :last_name)
4
+
5
+ include Progression
6
+
7
+ progression :profile do
8
+
9
+ step :step_1 do
10
+ !first_name.blank?
11
+ end
12
+
13
+ step :step_2 do
14
+ !last_name.blank?
15
+ end
16
+
17
+ end
18
+
19
+ # def self.profile_progression
20
+ # @progression ||= Progression::Progression.new do
21
+ # steps << Progression::Step.new(:step_1) { !first_name.blank? }
22
+ # steps << Progression::Step.new(:step_2) { !last_name.blank? }
23
+ # end
24
+ # end
25
+
26
+ # def profile_progress
27
+ # self.class.profile_progression.progress_for(self)
28
+ # end
29
+
30
+ end
31
+
32
+ describe "Progression" do
33
+
34
+ before do
35
+ @profile = Profile.new
36
+ @progression = @profile.profile_progress
37
+ end
38
+
39
+ it 'should return a list of steps' do
40
+ @progression.should have(2).steps
41
+ end
42
+
43
+ it 'should return a list of completed steps' do
44
+ @progression.should have(0).completed_steps
45
+ end
46
+
47
+ it 'should return the percentage completed' do
48
+ @progression.percentage_completed.should == 0.0
49
+ end
50
+
51
+ it 'should return the next step to be completed' do
52
+ @progression.next_step.name.should == :step_1
53
+ end
54
+
55
+ describe "after first step" do
56
+
57
+ before do
58
+ @profile.first_name = "Michael"
59
+ @progression = @profile.profile_progress
60
+ end
61
+
62
+ it 'should have 1 completed step' do
63
+ @progression.should have(1).completed_steps
64
+ end
65
+
66
+ it 'should return the percentage completed' do
67
+ @progression.percentage_completed.should == 50.0
68
+ end
69
+
70
+ it 'should return the next step to be completed' do
71
+ @progression.next_step.name.should == :step_2
72
+ end
73
+
74
+ describe "and second step" do
75
+
76
+ before do
77
+ @profile.last_name = "Jordan"
78
+ @progression = @profile.profile_progress
79
+ end
80
+
81
+ it 'should have 1 completed step' do
82
+ @progression.should have(2).completed_steps
83
+ end
84
+
85
+ it 'should return the percentage completed' do
86
+ @progression.percentage_completed.should == 100.0
87
+ end
88
+
89
+ it 'should return nil when there are no more steps to be completed' do
90
+ @progression.next_step.should == nil
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'progression'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: progression
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Guterl
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-22 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: |
35
+ progression provides a set of simple utility classes and a DSL for
36
+ measuring an objects progress through a progression of steps. This is
37
+ especially useful for defining and calculating profile completion
38
+ status in a social application.
39
+
40
+ There are a few existing solutions similar to progression, but they
41
+ try to do too much. The other options that I am familiar with are
42
+ either coupled with ActiveRecord or ActionController in some way.
43
+
44
+ email: mguterl@gmail.com
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - LICENSE
51
+ - README.md
52
+ files:
53
+ - .document
54
+ - .gitignore
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - VERSION
59
+ - lib/progression.rb
60
+ - spec/progression_spec.rb
61
+ - spec/spec.opts
62
+ - spec/spec_helper.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/mguterl/progression
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --charset=UTF-8
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.6
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: progression provides a set of simple utility classes and a DSL for measuring an objects progress through a progression of steps
93
+ test_files:
94
+ - spec/progression_spec.rb
95
+ - spec/spec_helper.rb