motivation 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
1
  # Motivation
2
2
 
3
3
  Simple DSL for use in classes to motivate a user towards a goal. An example
4
- goal might be "Complete Profile", or "Setup Project"
4
+ goal might be "Complete Profile", or "Setup Project".
5
+
6
+ This was heavily inspired by [progression](https://github.com/mguterl/progression).
7
+
8
+ I switched primarily because I wanted to use specific classes rather than inject into
9
+ an existing model namespace, and extend the DSL some.
5
10
 
6
11
  ## Installation
7
12
 
@@ -19,6 +24,8 @@ Or install it yourself as:
19
24
 
20
25
  ## Usage
21
26
 
27
+ Create a motivational class and `include Motivation`.
28
+
22
29
  ```ruby
23
30
  require "motivation"
24
31
 
@@ -43,7 +50,7 @@ class ProfileMotivation
43
50
  # check is heavy and you want to use a cached result. This
44
51
  # will define a method `#complete_tweets_added
45
52
  step :tweets_added
46
- check { cached_tweets_count.to_i > 0 || profile.tweets.length > 0 }
53
+ check { profile.cached_tweets_count.to_i > 0 || profile.tweets.length > 0 }
47
54
  complete { profile.cached_tweets_count = profile.tweets.length.to_i }
48
55
  end
49
56
 
@@ -60,12 +67,31 @@ Note that the `check` and `complete` blocks will not tolerate early returns, you
60
67
  will get a `LocalJumpError`. If you want to simplify your DSL definitions, you can
61
68
  just call methods, including privates, in your ProfileMotivation class itself.
62
69
 
63
- ## Acknowledgements
70
+ You can also iterate over checks for a motivation instance:
64
71
 
65
- This was heavily inspired by [progression](https://github.com/mguterl/progression).
72
+ ```ruby
73
+ motivation.each_check do |check|
74
+ puts check.name
75
+ puts check.completed?
76
+ end
77
+ ```
66
78
 
67
- I switched primarily because I wanted to use specific classes rather than inject into
68
- an existing model namespace, and extend the DSL some.
79
+ You can get the next incomplete check, this is useful if you want to render
80
+ a message in a header, for instance.
81
+
82
+ ```ruby
83
+ motivation.next_check
84
+ ```
85
+
86
+ Motivation checks are wrapped to provide a tranlation key suitable for use with i18n.
87
+ Note it currently does not include i18n, you can pass the generated keys straight to
88
+ your helper (such as `t` in Rails views).
89
+
90
+ ```
91
+ motivation.next_check.translation_key #=> "motivations.profile.setup_twitter.incomplete"
92
+ ```
93
+
94
+ The key format is `motivations.motivation_subject.step_name.[incomplete|complete]`
69
95
 
70
96
  ## Contributing
71
97
 
@@ -1,3 +1,3 @@
1
1
  module Motivation
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/motivation.rb CHANGED
@@ -16,16 +16,20 @@ module Motivation
16
16
  true
17
17
  end
18
18
 
19
- def checks
19
+ def unwrapped_checks
20
20
  self.class.checks
21
21
  end
22
22
 
23
+ def checks
24
+ unwrapped_checks.collect { |c| WrappedCheck.new(c, self) }
25
+ end
26
+
23
27
  def each_check(&block)
24
- wrapped_checks.each &block
28
+ checks.each &block
25
29
  end
26
30
 
27
- def wrapped_checks
28
- checks.collect { |c| WrappedCheck.new(c, self) }
31
+ def next_check
32
+ checks.detect { |c| ! c.completed? }
29
33
  end
30
34
 
31
35
  def completions
@@ -155,9 +159,9 @@ module Motivation
155
159
  def translation_key
156
160
  [
157
161
  "motivations",
158
- @motivation.key,
159
- status_key,
160
- @check.name
162
+ @motivation.translation_key,
163
+ @check.name,
164
+ status_key
161
165
  ].join('.')
162
166
  end
163
167
  private
@@ -136,11 +136,26 @@ describe Motivation do
136
136
  end
137
137
  end
138
138
 
139
+ describe "getting the next check" do
140
+ it "returns the first wrapped check that is incomplete" do
141
+ project.name = "name"
142
+ project.subdomain = nil
143
+ expect(motivation.next_check.name.to_s).to eq "subdomain_setup"
144
+ project.subdomain = "subdomain"
145
+ expect(motivation.next_check.name.to_s).to eq "local_method_step"
146
+ end
147
+
148
+ it "returns nil if all checks are complete" do
149
+ motivation.method_check = true
150
+ expect(motivation.next_check).to be_nil
151
+ end
152
+ end
153
+
139
154
  end
140
155
 
141
156
  describe Motivation::WrappedCheck, "#translation_key" do
142
157
  let(:check) { OpenStruct.new }
143
- let(:motivation) { stub(:motivation, :key => "project") }
158
+ let(:motivation) { stub(:motivation, :translation_key => "project") }
144
159
  let(:wrapped_check) { Motivation::WrappedCheck.new(check, motivation) }
145
160
 
146
161
  before do
@@ -156,16 +171,16 @@ describe Motivation::WrappedCheck, "#translation_key" do
156
171
  end
157
172
 
158
173
  context "for a incomplete step" do
159
- it "generates a key like motivations.project.incomplete.step_name" do
174
+ it "generates a key like motivations.project.step_name.incomplete" do
160
175
  check.flag = false
161
- expect(wrapped_check.translation_key).to eq "motivations.project.incomplete.foo_bar"
176
+ expect(wrapped_check.translation_key).to eq "motivations.project.foo_bar.incomplete"
162
177
  end
163
178
  end
164
179
 
165
180
  context "for a completed step" do
166
- it "generates a key like motivations.project.complete.step_name" do
181
+ it "generates a key like motivations.project.step_name.complete" do
167
182
  check.flag = true
168
- expect(wrapped_check.translation_key).to eq "motivations.project.complete.foo_bar"
183
+ expect(wrapped_check.translation_key).to eq "motivations.project.foo_bar.complete"
169
184
  end
170
185
  end
171
186
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motivation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: