class RedisModel::Schema

Public: Schema information for RedisModel::Base-derived classes. It contains information required to manipulate Redis data including data type and key label.

Constants

DATA_TYPES

Public: Data type label and their corresponding module responsible for specified type.

Attributes

data_type[R]
data_type_module[R]
klass[R]

Public Class Methods

collection() click to toggle source

Public: Global index of RedisModel::Schema objects.

Returns the Hash of global schema index.

# File lib/redis_model/schema.rb, line 33
def self.collection
  @collection ||= {}
end
find(klass) click to toggle source

Public: Find schema information for specified class. It could be schema defined for one of ancestors of the class.

klass - Class to find schema.

Returns RedisModel::Schema object for given class, nil if it does not

exist.
# File lib/redis_model/schema.rb, line 44
def self.find(klass)
  collection[(klass.ancestors - klass.included_modules).detect do |parent_klass|
    collection[parent_klass]
  end]
end
new(options = {}) click to toggle source

Public: Initializes a Schema.

options - Options for schema.

:klass     - Class being specified for the schema.
:data_type - Data type of Redis value.

Returns newly initialized Schema object.

# File lib/redis_model/schema.rb, line 70
def initialize(options = {})
  raise UnknownType.new unless DATA_TYPES[options[:data_type]]

  @klass = options[:klass]
  @data_type = options[:data_type]
  @data_type_module = DATA_TYPES[@data_type]
end
register(klass, options = {}) click to toggle source

Public: Register schema for specified class.

klass - Class to register schema. options - Additional options for schema.

:data_type - Data type of Redis value.

Returns the Module corresponding to data type of the class.

# File lib/redis_model/schema.rb, line 57
def self.register(klass, options = {})
  raise DuplicateDefinition.new if find(klass)

  (collection[klass] = new(options.merge(klass: klass))).data_type_module
end

Public Instance Methods

custom_key_label(&block) click to toggle source

Public: Defines custom part of key label by passing a block having arity of 1 to this method.

block - Block or proc that converts object into custom label string

Examples:

schema.custom_key_label do |object|
  object.id
end

Returns nothing.

# File lib/redis_model/schema.rb, line 100
def custom_key_label(&block)
  @custom_key_label_proc = block
end
key_label(object) click to toggle source

Public: Key label of Redis value associated with instance of classes inherit RedisModel::Base.

object - Instance of RedisModel::Base-derived class.

Returns String containing label for specified object.

# File lib/redis_model/schema.rb, line 84
def key_label(object)
  [base_key_label, @custom_key_label_proc && @custom_key_label_proc.call(object)].compact.join(':')
end

Protected Instance Methods

base_key_label() click to toggle source

Internal: Retrieves string used for label for Redis value associated with the schema. Base label can be

Returns String containing base label.

# File lib/redis_model/schema.rb, line 110
def base_key_label
  @key_label_base ||= [RedisModel::Configurations.instance.app_name, RedisModel::Configurations.instance.environment, @klass.name.underscore].compact.join(':')
end