Class: RedisModel::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_model/base.rb

Overview

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

Direct Known Subclasses

BelongedTo, Intersected

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connectionObject

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

Returns Redis connection for objects using RedisModel.



38
39
40
# 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) ⇒ Object

Public: Sets custom key label for the object.

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

Returns nothing.



47
48
49
# 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 = {}) ⇒ Object

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.



22
23
24
# 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_schemaObject

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.


31
32
33
# File 'lib/redis_model/base.rb', line 31

def self.redis_model_schema
  @schema ||= RedisModel::Schema.find(self)
end

Instance Method Details

#key_labelObject

Public: Retrieves key label for instantiated object.

Returns the String label for the object.



54
55
56
# File 'lib/redis_model/base.rb', line 54

def key_label
  self.class.redis_model_schema.key_label(self)
end

#to_valueObject

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.



63
64
65
# File 'lib/redis_model/base.rb', line 63

def to_value
  self
end