Class: Relix::UniqueIndex

Inherits:
Index
  • Object
show all
Includes:
Ordering
Defined in:
lib/relix/indexes/unique.rb

Instance Attribute Summary

Attributes inherited from Index

#model_name

Instance Method Summary collapse

Methods inherited from Index

#attribute_immutable?, compact_kind, #create_query_clause, #initialize, kind, #name, #normalize, #query, #read, #read_normalized, #watch

Constructor Details

This class inherits a constructor from Relix::Index

Instance Method Details

#all(r, options = {}) ⇒ Object



50
51
52
# File 'lib/relix/indexes/unique.rb', line 50

def all(r, options={})
  r.zrange(sorted_set_name, *range_from_options(r, options))
end

#deindex(r, pk, old_value) ⇒ Object



40
41
42
43
# File 'lib/relix/indexes/unique.rb', line 40

def deindex(r, pk, old_value)
  r.hdel(hash_name, old_value)
  r.zrem(sorted_set_name, pk)
end

#destroy_all(r) ⇒ Object



45
46
47
48
# File 'lib/relix/indexes/unique.rb', line 45

def destroy_all(r)
  r.del(hash_name)
  r.del(sorted_set_name)
end

#eq(r, value, options = {}) ⇒ Object



54
55
56
# File 'lib/relix/indexes/unique.rb', line 54

def eq(r, value, options={})
  [r.hget(hash_name, value)].compact
end

#filter(r, object, value) ⇒ Object



17
18
19
20
21
22
# File 'lib/relix/indexes/unique.rb', line 17

def filter(r, object, value)
  if r.hexists(hash_name, value)
    raise NotUniqueError.new("'#{value}' is not unique in index #{name}")
  end
  super
end

#hash_nameObject



9
10
11
# File 'lib/relix/indexes/unique.rb', line 9

def hash_name
  @set.keyer.component(name, 'lookup')
end

#index(r, pk, object, value, old_value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/relix/indexes/unique.rb', line 29

def index(r, pk, object, value, old_value)
  if read(object).values.all?{|e| !e.nil?}
    r.hset(hash_name, value, pk)
    r.zadd(sorted_set_name, score(object, value), pk)
  else
    r.hdel(hash_name, value)
    r.zrem(sorted_set_name, pk)
  end
  r.hdel(hash_name, old_value)
end

#index?(r, object, value) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/relix/indexes/unique.rb', line 24

def index?(r, object, value)
  return false if read(object).values.any?{|e| e.nil?}
  super
end

#sorted_set_nameObject



5
6
7
# File 'lib/relix/indexes/unique.rb', line 5

def sorted_set_name
  @set.keyer.component(name, 'ordering')
end

#watch_keys(*values) ⇒ Object



13
14
15
# File 'lib/relix/indexes/unique.rb', line 13

def watch_keys(*values)
  hash_name
end