Module: Redis::Helper
- Defined in:
- lib/redis/helper.rb,
lib/redis/helper/version.rb
Defined Under Namespace
Modules: ClassMethods Classes: UnknownUniqueValue
Constant Summary collapse
- DEFAULT_UNIQUE_ATTR_NAME =
デフォルトの固有キー名
:id- REDIS_KEY_DELIMITER =
redisキーの区切り文字
":".freeze
- VERSION =
バージョン
"1.0.0".freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#attr_key(name, unique_attr = nil) ⇒ String
instance固有のkeyとattr_nameからkeyを生成する (instanceに複数のkeyを設定したい場合やkeyにattr_nameを含めたい場合に使用する).
-
#instance_key(unique_attr = nil) ⇒ String
instance固有のkeyを生成する (“<Class Name>:<unique key>”).
-
#redis ⇒ Redis
Redis.currentへのショートカット.
-
#ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ⇒ Integer
引数で指定した時間にexpireするためのttl値を生成.
Class Method Details
.included(klass) ⇒ Object
38 39 40 |
# File 'lib/redis/helper.rb', line 38 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#attr_key(name, unique_attr = nil) ⇒ String
instance固有のkeyとattr_nameからkeyを生成する(instanceに複数のkeyを設定したい場合やkeyにattr_nameを含めたい場合に使用する)
55 56 57 |
# File 'lib/redis/helper.rb', line 55 def attr_key(name, unique_attr = nil) [instance_key(unique_attr), name].join(REDIS_KEY_DELIMITER) end |
#instance_key(unique_attr = nil) ⇒ String
instance固有のkeyを生成する (“<Class Name>:<unique key>”)
62 63 64 65 66 67 68 |
# File 'lib/redis/helper.rb', line 62 def instance_key(unique_attr = nil) attr_name = unique_attr || DEFAULT_UNIQUE_ATTR_NAME if (unique_key = self.public_send(attr_name)).blank? raise UnknownUniqueValue, "unique keyとして指定された値(#{attr_name})が取得できません" end [self.class.name, unique_key].join(REDIS_KEY_DELIMITER) end |
#redis ⇒ Redis
Redis.currentへのショートカット
88 89 90 |
# File 'lib/redis/helper.rb', line 88 def redis self.class.redis end |
#ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ⇒ Integer
引数で指定した時間にexpireするためのttl値を生成
80 81 82 83 84 |
# File 'lib/redis/helper.rb', line 80 def ttl_to(to_time, from_time = Time.current, unsigned_non_zero: true) ttl = (to_time - from_time).to_i return ttl if ttl > 0 unsigned_non_zero ? 1 : ttl end |