Class: ProgressTracker::Base
- Inherits:
-
Object
- Object
- ProgressTracker::Base
- Defined in:
- lib/progress_tracker/base.rb
Constant Summary collapse
- DEFAULTS =
Just so we’ve always got values…
{ progress: 0, message: "" }
Class Attribute Summary collapse
-
.redis_connection ⇒ Object
Returns the value of attribute redis_connection.
Instance Attribute Summary collapse
-
#base_key ⇒ Object
readonly
Instance-level access to Redis (actually a Redis::Namespace).
-
#current_tracked_object ⇒ Object
The tracked object that this instance currently refers to.
-
#redis ⇒ Object
readonly
Instance-level access to Redis (actually a Redis::Namespace).
-
#tracked_object_keys ⇒ Object
readonly
All sub-objects tracked with Base#track.
Instance Method Summary collapse
-
#initialize(object_name, object_id = nil) ⇒ Base
constructor
A new instance of Base.
- #message(msg) ⇒ Object
-
#method_missing(meth, *args) ⇒ Object
If a method is called that matches the name of a tracked object, return a new instance of self with the @current_tracked_object set to the correct key.
- #progress(value) ⇒ Object
-
#reset ⇒ Object
reset the currently trackd object.
-
#reset! ⇒ Object
re-set every redis key that this instance is tracking.
-
#to_hash ⇒ Object
grab all the keys related to this set from redis.
-
#track(object_name, object_id = nil) ⇒ Object
Build a compound key and store it for future reference.
- #update(hsh) ⇒ Object
Constructor Details
#initialize(object_name, object_id = nil) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/progress_tracker/base.rb', line 24 def initialize(object_name, object_id = nil) @base_key = _ck(object_name, object_id) @redis = ::Redis::Namespace.new(@base_key, redis: ProgressTracker::Base.redis_connection) # initialize or fetch our tracked object keys from Redis, ensuring we at least have _base present @tracked_object_keys = Set.new(redis.smembers("tracked-object-keys").map(&:to_sym) << :_base) @current_tracked_object = :_base end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
If a method is called that matches the name of a tracked object, return a new instance of self with the @current_tracked_object set to the correct key
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/progress_tracker/base.rb', line 67 def method_missing(meth, *args) meth_key = _ck(meth, args.first) if tracked_object_keys.include?(meth_key) self.dup.tap do |t| t.current_tracked_object = meth_key end else raise NoMethodError end end |
Class Attribute Details
.redis_connection ⇒ Object
Returns the value of attribute redis_connection.
6 7 8 |
# File 'lib/progress_tracker/base.rb', line 6 def redis_connection @redis_connection end |
Instance Attribute Details
#base_key ⇒ Object (readonly)
Instance-level access to Redis (actually a Redis::Namespace)
13 14 15 |
# File 'lib/progress_tracker/base.rb', line 13 def base_key @base_key end |
#current_tracked_object ⇒ Object
The tracked object that this instance currently refers to
19 20 21 |
# File 'lib/progress_tracker/base.rb', line 19 def current_tracked_object @current_tracked_object end |
#redis ⇒ Object (readonly)
Instance-level access to Redis (actually a Redis::Namespace)
13 14 15 |
# File 'lib/progress_tracker/base.rb', line 13 def redis @redis end |
#tracked_object_keys ⇒ Object (readonly)
All sub-objects tracked with Base#track
16 17 18 |
# File 'lib/progress_tracker/base.rb', line 16 def tracked_object_keys @tracked_object_keys end |
Instance Method Details
#message(msg) ⇒ Object
94 95 96 |
# File 'lib/progress_tracker/base.rb', line 94 def (msg) update message: msg end |
#progress(value) ⇒ Object
90 91 92 |
# File 'lib/progress_tracker/base.rb', line 90 def progress(value) update progress: value.to_i end |
#reset ⇒ Object
reset the currently trackd object
85 86 87 |
# File 'lib/progress_tracker/base.rb', line 85 def reset redis.del current_tracked_object end |
#reset! ⇒ Object
re-set every redis key that this instance is tracking
36 37 38 39 40 41 |
# File 'lib/progress_tracker/base.rb', line 36 def reset! ['tracked-object-keys', *tracked_object_keys.to_a].each { |key| redis.del key } @tracked_object_keys = Set.new([:_base]) @current_tracked_object = :_base end |
#to_hash ⇒ Object
grab all the keys related to this set from redis
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/progress_tracker/base.rb', line 52 def to_hash tracked_object_keys.inject({}) do |hash, key| hash.tap do |h| h[key] = DEFAULTS.merge(redis.hgetall(key).symbolize_keys) # Make sure :progress is always returned as an integer h[key][:progress] = h[key][:progress].to_i end end end |
#track(object_name, object_id = nil) ⇒ Object
Build a compound key and store it for future reference
44 45 46 47 48 49 |
# File 'lib/progress_tracker/base.rb', line 44 def track(object_name, object_id = nil) new_key = _ck(object_name, object_id) redis.sadd "tracked-object-keys", new_key tracked_object_keys << new_key end |
#update(hsh) ⇒ Object
80 81 82 |
# File 'lib/progress_tracker/base.rb', line 80 def update(hsh) redis.hmset current_tracked_object, *hsh.to_a.flatten end |