class RedisModel::Base

Public: Parent class for classes associated with Redis attributes. It provides methods to manipulate values on Redis storage.

Public Class Methods

connection() click to toggle source

Public: Global Redis connection object. It is initialized only once.

Returns Redis connection for objects using RedisModel.

# File lib/redis_model/base.rb, line 38
def self.connection
  @@connection ||= Redis.new(url: RedisModel::Configurations.instance.redis_url)
end
custom_key_label(&block) click to toggle source

Public: Sets custom key label for the object.

block - Required Block which returns custom key for given object.

Returns nothing.

# File lib/redis_model/base.rb, line 47
def self.custom_key_label(&block)
  redis_model_schema.custom_key_label(&block)
end
data_type(type, options = {}) click to toggle source

Public: DSL which defines data type for classes extending RedisModel::Base. It is mandatory for child classes to indicate data type before manipulating Redis values.

type - The Symbol indicating data type for the class.

Examples

class Foo < RedisModel::Base
  data_type :counter # Defines data type for instances of Foo class.
end

foo = Foo.new
foo.incr
foo.to_i # 1

Returns nothing.

# File lib/redis_model/base.rb, line 22
def self.data_type(type, options = {})
  include RedisModel::Schema.register(self, options.merge(data_type: type))
end
redis_model_schema() click to toggle source

Public: Retrieves proper RedisModel::Schema object describes schema information for class being referenced.

Returns RedisModel::Schema object for the class or its direct ancestor,

nil if schema was not found.
# File lib/redis_model/base.rb, line 31
def self.redis_model_schema
  @schema ||= RedisModel::Schema.find(self)
end

Public Instance Methods

key_label() click to toggle source

Public: Retrieves key label for instantiated object.

Returns the String label for the object.

# File lib/redis_model/base.rb, line 54
def key_label
  self.class.redis_model_schema.key_label(self)
end
to_value() click to toggle source

Internal: Converts value stored in Redis to primitive Ruby object. It acts as a helper method which converts getters for certain types of RedisModel attributes to return primitive types.

Returns the primitive value of the object if it is available.

# File lib/redis_model/base.rb, line 63
def to_value
  self
end