Class: RedisModel::Schema
- Inherits:
-
Object
- Object
- RedisModel::Schema
- 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
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#data_type_module ⇒ Object
readonly
Returns the value of attribute data_type_module.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.collection ⇒ Object
Public: Global index of RedisModel::Schema objects.
-
.find(klass) ⇒ Object
Public: Find schema information for specified class.
-
.register(klass, options = {}) ⇒ Object
Public: Register schema for specified class.
Instance Method Summary collapse
-
#custom_key_label(&block) ⇒ Object
Public: Defines custom part of key label by passing a block having arity of 1 to this method.
-
#initialize(options = {}) ⇒ Schema
constructor
Public: Initializes a Schema.
-
#key_label(object) ⇒ Object
Public: Key label of Redis value associated with instance of classes inherit RedisModel::Base.
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.
70 71 72 73 74 75 76 |
# File 'lib/redis_model/schema.rb', line 70 def initialize( = {}) raise UnknownType.new unless DATA_TYPES[[:data_type]] @klass = [:klass] @data_type = [:data_type] @data_type_module = DATA_TYPES[@data_type] end |
Instance Attribute Details
#data_type ⇒ Object (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_module ⇒ Object (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 |
#klass ⇒ Object (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
.collection ⇒ Object
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, = {}) raise DuplicateDefinition.new if find(klass) (collection[klass] = new(.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 |