completion-progress 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a71b32ba28e0d927e325924c1571a6e9ecf491d4
4
+ data.tar.gz: 95b4319417eb652eb7fd390bfccdd31a707f1c72
5
+ SHA512:
6
+ metadata.gz: abefc5727fe45e59c56e9777439e53afa65dd726683c26be3f47e2aaae53800d14a6720846925e3ddb37aa2c8b1246149ab3a76404f7ed9f710013545507aef0
7
+ data.tar.gz: daa9dd9fbff6a552def3611d2cd85c971eac267eaac142ecd05c2671e1f931ada085ff05affc08d15d3cbdf66087f5337216598a5690ff70c737d20262401ac6
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .idea/
2
+ *.project
3
+ completion-progress-*.gem
4
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ script: bundle exec rake test
6
+ notifications:
7
+ email:
8
+ on_success: never
9
+ on_failure: change
10
+ gemfile:
11
+ - Gemfile
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mirko Mignini
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ [![Build Status](https://travis-ci.org/MirkoMignini/completion-progress.png?branch=master)](https://travis-ci.org/MirkoMignini/completion-progress)
2
+
3
+ completion-progress
4
+ ===================
5
+
6
+ DSL to calculate completion level (in percentage or score) of an object based on the value of its properties.
7
+
8
+ ## Requirements
9
+
10
+ * Ruby 1.9+
11
+
12
+ ## Installation
13
+
14
+ Add it to your Gemfile:
15
+
16
+ `gem 'completion-progress'`
17
+
18
+ Then:
19
+
20
+ `bundle`
21
+
22
+ ## Usage
23
+
24
+ Use the completion-progress dsl to define the steps.
25
+ For every step define a name, a value (in percentage or a custom value) and, optionally, a hint to explain the user how to complete the step.
26
+
27
+ ```ruby
28
+ class User
29
+ include CompletionProgress
30
+
31
+ completion_progress :profile do
32
+ step :name, 30
33
+ step :surname, 30
34
+ step :email, 20
35
+ step :age, 10, {hint: {text: 'Please add your age', href: '/profile/edit'}}
36
+ step :phones, 10, {hint: {text: 'Please add at least a phone number', href: '/profile/edit/phones'}}
37
+ end
38
+
39
+ ...
40
+ end
41
+
42
+ user = User.new
43
+
44
+ #set the user properties to custom values
45
+
46
+ user.profile.value #will contains the sum of the values of all the steps which value is not null or empty
47
+ user.profile1.steps[:name].result #check the result (true or false) of a single step
48
+ ```
49
+
50
+ ## License
51
+
52
+ MIT License. Copyright 2014 by Mirko Mignini (https://github.com/MirkoMignini)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'completion-progress'
3
+ s.version = '0.0.2'
4
+ s.platform = Gem::Platform::RUBY
5
+ s.licenses = ['MIT']
6
+ s.summary = 'Easy progress calculator'
7
+ s.description = s.summary
8
+ s.email = 'mirko.mignini@gmail.com'
9
+ s.homepage = 'https://github.com/MirkoMignini/completion-progress'
10
+ s.authors = ['Mirko Mignini']
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- test/*`.split("\n")
14
+ s.require_paths = ['lib']
15
+
16
+ s.add_development_dependency 'rake'
17
+ s.add_development_dependency 'minitest'
18
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'completion-progress/initializer'
2
+
3
+ module CompletionProgress
4
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'step'
2
+
3
+ class Engine
4
+ attr_reader :auto_update, :steps, :hints, :value, :percent, :total_value
5
+ attr_accessor :parent
6
+
7
+ def initialize(options = {})
8
+ @value = 0
9
+ @total_value = 0
10
+ @percent = 0
11
+ @hints = Hash.new
12
+ @steps = Hash.new
13
+ @auto_update = options.has_key?(:auto_update) ? options[:auto_update] : true
14
+ end
15
+
16
+ def step(name, value, options = {}, &block)
17
+ if !@steps.has_key?(name)
18
+ @steps[name] = Step.new(name, value, options, &block)
19
+ @total_value += value
20
+ end
21
+ end
22
+
23
+ def update
24
+ @value = 0
25
+ @hints = Hash.new
26
+ @steps.each do |key, step|
27
+ step.process(parent) ? @value += step.value : @hints[step.name] = step.hint
28
+ end
29
+ @percent = @value * 100 / @total_value
30
+ end
31
+
32
+ def value
33
+ update if @auto_update
34
+ @value
35
+ end
36
+
37
+ def steps
38
+ update if @auto_update
39
+ @steps
40
+ end
41
+
42
+ def percent
43
+ update if @auto_update
44
+ @percent
45
+ end
46
+
47
+ def hints
48
+ update if @auto_update
49
+ @hints
50
+ end
51
+ end
@@ -0,0 +1,10 @@
1
+ class Hint
2
+ attr_accessor :step, :text, :href, :options
3
+
4
+ def initialize(step, text, href = '', options = {})
5
+ @step = step
6
+ @text = text
7
+ @href = href
8
+ @options = options
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'engine'
2
+
3
+ module CompletionProgress
4
+
5
+ def self.included(base)
6
+ base.extend CompletionProgress::ClassMethods
7
+ super
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ @@engines = Hash.new
13
+
14
+ def completion_progress(name, options = {}, &block)
15
+ engine = @@engines[name]
16
+ if engine == nil
17
+ engine = Engine.new(options, &block)
18
+ @@engines[name] = engine
19
+ define_method(name) do
20
+ engine.parent = self
21
+ engine
22
+ end
23
+ end
24
+ engine.instance_eval(&block) if block
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'hint'
2
+
3
+ class Step
4
+ attr_accessor :name, :value, :block, :result, :hint
5
+
6
+ def initialize(name, value, options = {}, &block)
7
+ @name = name
8
+ @value = value
9
+ @block = block
10
+ @result = false
11
+
12
+ if (options.has_key?(:hint))
13
+ @hint = Hint.new(self, options[:hint][:text], options[:hint][:href], options[:hint][:options])
14
+ else
15
+ @hint = Hint.new(self, "Fill #{name}")
16
+ end
17
+ end
18
+
19
+ def process(obj)
20
+ begin
21
+
22
+ if !@block
23
+ val = obj.send(@name)
24
+ else
25
+ val = obj.instance_eval(&@block)
26
+ end
27
+
28
+ if val.kind_of?(Array) or val.kind_of?(Hash)
29
+ @result = val.count == 0 ? false : true
30
+ else
31
+ @result = val
32
+ end
33
+
34
+ rescue Exception => e
35
+ @result = false
36
+ ensure
37
+ @result ? @result = true : @result = false
38
+ end
39
+ end
40
+ end
data/test/test_all.rb ADDED
@@ -0,0 +1,196 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/completion-progress'
3
+
4
+ class CompletionProgressTest < Minitest::Test
5
+
6
+ class User
7
+ include CompletionProgress
8
+
9
+ attr_accessor :name, :surname, :age, :phones, :options, :custom_value
10
+
11
+ def initialize
12
+ @phones = Array.new
13
+ @options = Hash.new
14
+ end
15
+
16
+ completion_progress :profile1 do
17
+ step :name, 40, {hint: {text: 'Test hint', href: '/profile/edit', options: {custom: 10}}}
18
+ step :surname, 40
19
+ step :age, 20
20
+ end
21
+
22
+ completion_progress :profile2 do
23
+ step :name, 20
24
+ step :surname, 20
25
+ step :age, 10
26
+ end
27
+
28
+ completion_progress :partial_profile do
29
+ step :name, 40
30
+ end
31
+
32
+ completion_progress :partial_profile do
33
+ step :surname, 40
34
+ end
35
+
36
+ completion_progress :profile_auto_update_false, auto_update: false do
37
+ step :name, 40
38
+ end
39
+
40
+ completion_progress :profile_steps do
41
+ step :name, 5
42
+ step :phones, 5
43
+ step :options, 5
44
+ step :custom, 5 do
45
+ @custom_value
46
+ end
47
+ end
48
+
49
+ completion_progress :profile_percent do
50
+ step :name, 10
51
+ step :surname, 10
52
+ step :age, 10
53
+ step :phones, 10
54
+ step :custom_value, 10
55
+ end
56
+
57
+ end
58
+
59
+ def test_setup
60
+ user = User.new
61
+ refute_nil(user)
62
+ end
63
+
64
+ def test_add_method
65
+ user = User.new
66
+ refute_nil(user.profile1)
67
+ refute_nil(user.profile2)
68
+ end
69
+
70
+ def test_steps
71
+ user = User.new
72
+ assert_equal(user.profile1.steps.count, 3)
73
+
74
+ refute_nil(user.profile1.steps[:name])
75
+ assert_equal(user.profile1.steps[:name].value, 40)
76
+
77
+ refute_nil(user.profile1.steps[:surname])
78
+ assert_equal(user.profile1.steps[:surname].value, 40)
79
+
80
+ refute_nil(user.profile1.steps[:age])
81
+ assert_equal(user.profile1.steps[:age].value, 20)
82
+ end
83
+
84
+ def test_results
85
+ user = User.new
86
+ assert_equal(user.profile1.steps.count, 3)
87
+
88
+ assert_equal(user.profile1.steps[:name].result, false)
89
+ assert_equal(user.profile1.steps[:surname].result, false)
90
+ assert_equal(user.profile1.steps[:age].result, false)
91
+
92
+ user.name = 'Mirko'
93
+ assert_equal(user.profile1.steps[:name].result, true)
94
+ assert_equal(user.profile1.steps[:surname].result, false)
95
+ assert_equal(user.profile1.steps[:age].result, false)
96
+
97
+ user.surname = 'Mignini'
98
+ assert_equal(user.profile1.steps[:name].result, true)
99
+ assert_equal(user.profile1.steps[:surname].result, true)
100
+ assert_equal(user.profile1.steps[:age].result, false)
101
+
102
+ user.age = 33
103
+ assert_equal(user.profile1.steps[:name].result, true)
104
+ assert_equal(user.profile1.steps[:surname].result, true)
105
+ assert_equal(user.profile1.steps[:age].result, true)
106
+ end
107
+
108
+ def test_values
109
+ user = User.new
110
+ assert_equal(user.profile1.value, 0)
111
+
112
+ user.name = 'Mirko'
113
+ assert_equal(user.profile1.value, 40)
114
+ user.surname = 'Mignini'
115
+ assert_equal(user.profile1.value, 80)
116
+ user.age = 33
117
+ assert_equal(user.profile1.value, 100)
118
+ end
119
+
120
+ def test_multi_object
121
+ user = User.new
122
+ user.name = 'Mirko'
123
+ assert_equal(user.profile1.value, 40)
124
+ assert_equal(user.profile2.value, 20)
125
+ end
126
+
127
+ def test_auto_update
128
+ user = User.new
129
+ assert_equal(user.profile_auto_update_false.value, 0)
130
+ user.name = 'Mirko'
131
+ assert_equal(user.profile_auto_update_false.value, 0)
132
+ user.profile_auto_update_false.update
133
+ assert_equal(user.profile_auto_update_false.value, 40)
134
+ end
135
+
136
+ def test_variable_types
137
+ user = User.new
138
+ assert_equal(user.profile_steps.value, 0)
139
+
140
+ user.name = 'Mirko'
141
+ assert_equal(user.profile_steps.value, 5)
142
+
143
+ user.phones << '0911234567'
144
+ assert_equal(user.profile_steps.value, 10)
145
+
146
+ user.options['test'] = 'hello'
147
+ assert_equal(user.profile_steps.value, 15)
148
+
149
+ user.custom_value = true
150
+ assert_equal(user.profile_steps.value, 20)
151
+ end
152
+
153
+ def test_hints
154
+ user = User.new
155
+ refute_nil(user.profile1.hints)
156
+ assert_equal(user.profile1.hints.count, 3)
157
+
158
+ assert_equal(user.profile1.hints[:name].text, 'Test hint')
159
+ assert_equal(user.profile1.hints[:name].href, '/profile/edit')
160
+ assert_equal(user.profile1.hints[:name].options[:custom], 10)
161
+
162
+ assert_equal(user.profile1.hints[:surname].text, 'Fill surname')
163
+ assert_equal(user.profile1.hints[:surname].href, '')
164
+ assert_equal(user.profile1.hints[:surname].options.count, 0)
165
+
166
+ user.name = 'Mirko'
167
+ assert_equal(user.profile1.hints.count, 2)
168
+
169
+ user.surname = 'Mignini'
170
+ assert_equal(user.profile1.hints.count, 1)
171
+
172
+ user.age = 33
173
+ assert_equal(user.profile1.hints.count, 0)
174
+ end
175
+
176
+ def test_percent
177
+ user = User.new
178
+ assert_equal(user.profile_percent.percent, 0)
179
+ user.name = 'Mirko'
180
+ assert_equal(user.profile_percent.percent, 20)
181
+ user.surname = 'Mignini'
182
+ assert_equal(user.profile_percent.percent, 40)
183
+ user.age = 33
184
+ assert_equal(user.profile_percent.percent, 60)
185
+ user.phones = '0911234567'
186
+ assert_equal(user.profile_percent.percent, 80)
187
+ user.custom_value = 'test'
188
+ assert_equal(user.profile_percent.percent, 100)
189
+ end
190
+
191
+ def test_partials
192
+ user = User.new
193
+ assert_equal(user.partial_profile.steps.count, 2)
194
+ end
195
+
196
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: completion-progress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mirko Mignini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Easy progress calculator
42
+ email: mirko.mignini@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .travis.yml
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - completion-progress.gemspec
54
+ - lib/completion-progress.rb
55
+ - lib/completion-progress/engine.rb
56
+ - lib/completion-progress/hint.rb
57
+ - lib/completion-progress/initializer.rb
58
+ - lib/completion-progress/step.rb
59
+ - test/test_all.rb
60
+ homepage: https://github.com/MirkoMignini/completion-progress
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.4.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Easy progress calculator
84
+ test_files:
85
+ - test/test_all.rb