Module: Goalkeeper
- Defined in:
- lib/goalkeeper.rb,
lib/goalkeeper/set.rb,
lib/goalkeeper/goal.rb,
lib/goalkeeper/version.rb
Overview
Goalkeeper provides methods to track if specific events(Goals) have been completed(met).
Its goes likes this..
Lets ensure we wakeup New Years Day 2020. The goal will be called ‘wakeup:2020-01-01’
g = Goalkeeper::Goal.new('wakeup:2020-01-01')
g.met? #=> false
Time flies… it is New Years Day 2020.
g.met! # or Goalkeeper.met!('wakeup:2020-01-01')
g.met? #=> true
g.met_at #=> 2020-01-01 05:01:31 -0500
Now if our application checks our goal, it will be met.
Goalkeeper::Goal.new('wakeup:2020-01-01').met? #=> true
Goalkeeper.met?('wakeup:2020-01-01') #=> true
Note: Once a Goal is ‘met’ the ‘met_at’ timestamp will not change, unless ‘clear!’ is called.
We are probably only interested in this goal being complete for a limited time, so it will expire and be removed from Redis.
g.ttl #=> 86400 (1 day)
If you need to reference the Redis key
g.key #=> Goalkeeper:wakeup:2020-01-01
Finally clear the Goal is simple
g.clear!
Sets
Perhaps you have a series of Goals you want to track, and see if they all have been met, or not.
set = Goalkeeper::Set.new
set.add('goal1').add('goal2')
set.met? #=> false
Lets have 1 goal met:
Goalkeeper.met!('goal1')
But our set is not met yet
set.met? #=> false
See which goals are met, or unmet
set.met #=> [#<Goalkeeper::Goal @label="goal1">]
set.unmet #=> [#<Goalkeeper::Goal @label="goal2">]
Lets complete our set.
Goalkeeper.met!('goal1')
set.met? #=> true
See the time the final goal was met
set.met_at #=> 2015-01-01 08:02:15 -0500
Customization
Customize the redis client by setting it in your application
Goalkeeper.redis = your_redis_client
Each record has a default expiration of 24 hours, but this can be modified.
Goalkeeper.expiration = number_of_seconds
Redis keys are stored under the default namespace of “Goalkeeper:”. The namespace can be configured:
Goalkeeper.namespace = string
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
-
.expiration ⇒ Object
The TTL set for each met Goal record created in Redis Default is 24 hours.
-
.expiration=(number_of_seconds) ⇒ Object
Overwrite the default expiration.
-
.met!(label) ⇒ Object
Creates a persistent Goal market with the given label.
- .met?(label) ⇒ Boolean
- .namespace ⇒ Object
- .namespace=(ns) ⇒ Object
- .redis ⇒ Object
-
.redis=(redis) ⇒ Object
Set the Redis client to a non default setting.
Class Method Details
.expiration ⇒ Object
The TTL set for each met Goal record created in Redis Default is 24 hours
98 99 100 |
# File 'lib/goalkeeper.rb', line 98 def self.expiration @expiration ||= 24 * 60 * 60 end |
.expiration=(number_of_seconds) ⇒ Object
Overwrite the default expiration
103 104 105 |
# File 'lib/goalkeeper.rb', line 103 def self.expiration=(number_of_seconds) @expiration = number_of_seconds end |
.met!(label) ⇒ Object
Creates a persistent Goal market with the given label.
88 89 90 |
# File 'lib/goalkeeper.rb', line 88 def self.met!(label) Goal.new(label).met! end |
.met?(label) ⇒ Boolean
92 93 94 |
# File 'lib/goalkeeper.rb', line 92 def self.met?(label) Goal.new(label).met? end |
.namespace ⇒ Object
107 108 109 |
# File 'lib/goalkeeper.rb', line 107 def self.namespace @namespace ||= "Goalkeeper" end |
.namespace=(ns) ⇒ Object
111 112 113 |
# File 'lib/goalkeeper.rb', line 111 def self.namespace=(ns) @namespace = ns end |
.redis ⇒ Object
83 84 85 |
# File 'lib/goalkeeper.rb', line 83 def self.redis @redis ||= Redis.new end |
.redis=(redis) ⇒ Object
Set the Redis client to a non default setting
79 80 81 |
# File 'lib/goalkeeper.rb', line 79 def self.redis=(redis) @redis = redis end |