RedisKeyManager

Gem Version Coverage Status Build Status

RedisKeyManager provides a simple Ruby DSL for declaring the Redis key patterns used by your application. Instead of repeating the same string literals and manual interpolations throughout your application—with the corresponding risk of error and silent failure—RedisKeyManager generates methods that return those keys, accepting named parameters corresponding to the substitutions specified in each pattern.

Installation

# Gemfile
gem "redis_key_manager"
$ bundle

Example Usage

require "redis_key_manager"

# Declare your key patterns like this:

class MyRedisKeys
  include RedisKeyManager::Manager

  key :hits, "hits"
  key :user, "user:[user_id]"
  key :foo, "foo:[name]:bar:[boom]:[bam]"
end

# Use your keys like this:

key = MyRedisKeys.hits  # => "hits"
Redis.current.incr(key)

key = MyRedisKeys.foo(name: "BOB", boom: 5, bam: 9)  # => "foo:BOB:bar:5:9"
Redis.current.zadd(key, 1.3, 500)

The methods generated by key declarations helpfully raise exceptions if you don't pass the named arguments they expect, or if the values you pass are nil.

MyRedisKeys.foo(name: "BOB", boom: 5, blop: 6)
# ArgumentError: Invalid arguments for MyRedisKeys.foo

MyRedisKeys.foo(name: "BOB", boom: 5)
# ArgumentError: Invalid arguments for MyRedisKeys.foo

MyRedisKeys.foo(name: "BOB", boom: 5, bam: nil)
# RedisKeyManager::InvalidKeyComponentError: Invalid Redis key component passed to MyRedisKeys.foo

Development

After checking out the repo, run bin/setup to install dependencies.

To install this gem onto your local machine, run bundle exec rake install.

You can also run bin/console for an interactive prompt that will allow you to experiment.

Please ensure that you write tests for any commits, and that tests pass locally before you submit a PR. To run tests, run bundle exec rake spec.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Oneflare/redis_key_manager.

License

The gem is available as open source under the terms of the MIT License.