Class: ProgressTracker::Base

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/progress_tracker/base.rb', line 59

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_connectionObject

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_keyObject (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_objectObject

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

#redisObject (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_keysObject (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



80
81
82
# File 'lib/progress_tracker/base.rb', line 80

def message(msg)
  update message: msg
end

#progress(value) ⇒ Object



76
77
78
# File 'lib/progress_tracker/base.rb', line 76

def progress(value)
  update progress: value.to_i
end

#to_hashObject

grab all the keys related to this set from redis



44
45
46
47
48
49
50
51
52
53
# File 'lib/progress_tracker/base.rb', line 44

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



36
37
38
39
40
41
# File 'lib/progress_tracker/base.rb', line 36

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



72
73
74
# File 'lib/progress_tracker/base.rb', line 72

def update(hsh)
  redis.hmset current_tracked_object, *hsh.to_a.flatten
end