Class: Ricordami::UniqueIndex
- Inherits:
-
Object
- Object
- Ricordami::UniqueIndex
- Defined in:
- lib/ricordami/unique_index.rb
Constant Summary collapse
- SEPARATOR =
"_-::-_"
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#need_get_by ⇒ Object
readonly
Returns the value of attribute need_get_by.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #add(id, value) ⇒ Object
- #all ⇒ Object
- #count ⇒ Object
- #id_for_values(*values) ⇒ Object
- #include?(value) ⇒ Boolean
-
#initialize(model, fields, options = {}) ⇒ UniqueIndex
constructor
A new instance of UniqueIndex.
- #normalize_value(value) ⇒ Object
- #ref_key_name ⇒ Object
- #rem(id, value, return_command = false) ⇒ Object
- #uidx_key_name ⇒ Object
Constructor Details
#initialize(model, fields, options = {}) ⇒ UniqueIndex
Returns a new instance of UniqueIndex.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ricordami/unique_index.rb', line 9 def initialize(model, fields, = {}) @model = model @fields = normalize_array(fields) @name = ("u_" + @fields.join("_")).to_sym @need_get_by = [:get_by] && @fields != [:id] if .has_key?(:scope) @scope = normalize_array([:scope]) @fields.push(*@scope) end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/ricordami/unique_index.rb', line 7 def fields @fields end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/ricordami/unique_index.rb', line 7 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/ricordami/unique_index.rb', line 7 def name @name end |
#need_get_by ⇒ Object (readonly)
Returns the value of attribute need_get_by.
7 8 9 |
# File 'lib/ricordami/unique_index.rb', line 7 def need_get_by @need_get_by end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
7 8 9 |
# File 'lib/ricordami/unique_index.rb', line 7 def scope @scope end |
Instance Method Details
#add(id, value) ⇒ Object
28 29 30 31 32 |
# File 'lib/ricordami/unique_index.rb', line 28 def add(id, value) value = normalize_value(value) @model.redis.sadd(uidx_key_name, value) @model.redis.hset(ref_key_name, value, id) if @need_get_by end |
#all ⇒ Object
51 52 53 |
# File 'lib/ricordami/unique_index.rb', line 51 def all @model.redis.smembers(uidx_key_name) end |
#count ⇒ Object
55 56 57 |
# File 'lib/ricordami/unique_index.rb', line 55 def count @model.redis.scard(uidx_key_name) end |
#id_for_values(*values) ⇒ Object
46 47 48 49 |
# File 'lib/ricordami/unique_index.rb', line 46 def id_for_values(*values) values = values.flatten.join(SEPARATOR) @model.redis.hget(ref_key_name, values) end |
#include?(value) ⇒ Boolean
59 60 61 |
# File 'lib/ricordami/unique_index.rb', line 59 def include?(value) @model.redis.sismember(uidx_key_name, value) end |
#normalize_value(value) ⇒ Object
63 64 65 |
# File 'lib/ricordami/unique_index.rb', line 63 def normalize_value(value) value.is_a?(Array) ? value.join(SEPARATOR) : value end |
#ref_key_name ⇒ Object
24 25 26 |
# File 'lib/ricordami/unique_index.rb', line 24 def ref_key_name @ref_key_name ||= KeyNamer.hash_ref(@model, :fields => @fields) end |
#rem(id, value, return_command = false) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ricordami/unique_index.rb', line 34 def rem(id, value, return_command = false) if return_command commands = [] commands << [:hdel, [ref_key_name, id]] if @need_get_by commands << [:srem, [uidx_key_name, value]] return commands end @model.redis.hdel(ref_key_name, id) if @need_get_by value = normalize_value(value) @model.redis.srem(uidx_key_name, value) end |
#uidx_key_name ⇒ Object
20 21 22 |
# File 'lib/ricordami/unique_index.rb', line 20 def uidx_key_name @uidx_key_name ||= KeyNamer.unique_index(@model, :name => @name) end |