Module: Conflow::Redis::Identifier::ClassMethods

Defined in:
lib/conflow/redis/identifier.rb

Overview

class methods for classes with identifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#counter_keyString

Redis key holding counter with IDs of model.

Examples:

default key

class My::Super::Class < Conflow::Redis::Field
  include Conflow::Redis::Identifier
end

My::Super::Class.counter_key #=> "my:super:class:idcnt"

Returns:

  • (String)

    Redis key



33
34
35
# File 'lib/conflow/redis/identifier.rb', line 33

def counter_key
  @counter_key ||= [*name.downcase.split("::"), :idcnt].join(":")
end

#key_templateString

Template for building keys for fields using only the ID.

Examples:

default key

class My::Super::Class < Conflow::Redis::Field
  include Conflow::Redis::Identifier
end

My::Super::Class.key_template #=> "my:super:class:%<id>d"

Returns:

  • (String)

    Template for building redis keys



45
46
47
# File 'lib/conflow/redis/identifier.rb', line 45

def key_template
  @key_template ||= [*name.downcase.split("::"), "%<id>d"].join(":")
end

Instance Method Details

#generate_idInteger

Returns next available ID.

Returns:

  • (Integer)

    next available ID



50
51
52
# File 'lib/conflow/redis/identifier.rb', line 50

def generate_id
  Conflow.redis.with { |conn| conn.incr(counter_key) }
end

#inherited(base) ⇒ Object

Copies counter_key and key_template to child classes



19
20
21
22
23
# File 'lib/conflow/redis/identifier.rb', line 19

def inherited(base)
  base.instance_variable_set("@counter_key", counter_key)
  base.instance_variable_set("@key_template", key_template)
  super
end