Class: RedisModel::Schema

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

Overview

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

Defined Under Namespace

Classes: DuplicateDefinition, UnknownType

Constant Summary collapse

DATA_TYPES =

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

{
  value: RedisModel::Types::Counter,
  counter: RedisModel::Types::Counter,
  list: RedisModel::Types::List,
  sorted_set: RedisModel::Types::SortedSet,
  float: RedisModel::Types::Float,
  set: RedisModel::Types::Set,
  hash: RedisModel::Types::Hash,
  string: RedisModel::Types::String,
  timestamp: RedisModel::Types::Timestamp,
  integer: RedisModel::Types::Integer
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Schema

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.

Raises:



70
71
72
73
74
75
76
# 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

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



6
7
8
# File 'lib/redis_model/schema.rb', line 6

def data_type
  @data_type
end

#data_type_moduleObject (readonly)

Returns the value of attribute data_type_module.



6
7
8
# File 'lib/redis_model/schema.rb', line 6

def data_type_module
  @data_type_module
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/redis_model/schema.rb', line 6

def klass
  @klass
end

Class Method Details

.collectionObject

Public: Global index of RedisModel::Schema objects.

Returns the Hash of global schema index.



33
34
35
# File 'lib/redis_model/schema.rb', line 33

def self.collection
  @collection ||= {}
end

.find(klass) ⇒ Object

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.


44
45
46
47
48
# 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

.register(klass, options = {}) ⇒ Object

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.



57
58
59
60
61
# 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

Instance Method Details

#custom_key_label(&block) ⇒ Object

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.



100
101
102
# File 'lib/redis_model/schema.rb', line 100

def custom_key_label(&block)
  @custom_key_label_proc = block
end

#key_label(object) ⇒ Object

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.



84
85
86
# 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