Class: Relix::IndexSet
- Inherits:
-
Object
- Object
- Relix::IndexSet
- Defined in:
- lib/relix/index_set.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #add_index(index_type, name, options = {}) ⇒ Object
- #current_values_name(pk) ⇒ Object
- #deindex!(object) ⇒ Object
- #index!(object) ⇒ Object
- #index_ops(object, pk) ⇒ Object
- #indexes ⇒ Object
-
#initialize(klass, redis) ⇒ IndexSet
constructor
A new instance of IndexSet.
- #key_prefix(name) ⇒ Object
- #keyer(value = nil, options = {}) ⇒ Object
- #lookup(&block) ⇒ Object
- #method_missing(m, *args) ⇒ Object
- #parent ⇒ Object
- #primary_key(accessor) ⇒ Object (also: #pk)
- #primary_key_index ⇒ Object
Constructor Details
#initialize(klass, redis) ⇒ IndexSet
Returns a new instance of IndexSet.
5 6 7 8 9 10 |
# File 'lib/relix/index_set.rb', line 5 def initialize(klass, redis) @klass = klass @redis = redis @indexes = Hash.new @keyer = Keyer.default_for(@klass) unless parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/relix/index_set.rb', line 36 def method_missing(m, *args) if Relix.index_types.keys.include?(m.to_sym) add_index(m, *args) else super end end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
4 5 6 |
# File 'lib/relix/index_set.rb', line 4 def klass @klass end |
#redis ⇒ Object
Returns the value of attribute redis.
3 4 5 |
# File 'lib/relix/index_set.rb', line 3 def redis @redis end |
Instance Method Details
#add_index(index_type, name, options = {}) ⇒ Object
44 45 46 47 |
# File 'lib/relix/index_set.rb', line 44 def add_index(index_type, name, ={}) accessor = (.delete(:on) || name) @indexes[name.to_s] = Relix.index_types[index_type].new(self, name, accessor, ) end |
#current_values_name(pk) ⇒ Object
133 134 135 |
# File 'lib/relix/index_set.rb', line 133 def current_values_name(pk) keyer.values(pk, @klass) end |
#deindex!(object) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/relix/index_set.rb', line 100 def deindex!(object) pk = primary_key_for(object) handle_concurrent_modifications(pk) do current_values_name = current_values_name(pk) @redis.watch current_values_name current_values = @redis.hgetall(current_values_name) indexes.map do |name, index| old_value = if index.attribute_immutable? index.read_normalized(object) else current_values[name] end ((watch = index.watch(old_value)) && @redis.watch(*watch)) proc { index.deindex(@redis, pk, object, old_value) } end.tap { |ops| ops << proc { @redis.del current_values_name } } end end |
#index!(object) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/relix/index_set.rb', line 92 def index!(object) pk = primary_key_for(object) handle_concurrent_modifications(pk) do index_ops(object, pk) end end |
#index_ops(object, pk) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/relix/index_set.rb', line 63 def index_ops(object, pk) current_values_name = current_values_name(pk) @redis.watch current_values_name current_values = @redis.hgetall(current_values_name) ops = indexes.collect do |name,index| value = index.read_normalized(object) old_value = current_values[name] ((watch = index.watch(value, old_value)) && @redis.watch(*watch)) next if value == old_value current_values[name] = value unless index.attribute_immutable? next unless index.filter(@redis, object, value) query_value = index.query(@redis, value) proc do index.index(@redis, pk, object, value, old_value, *query_value) end end.compact ops << proc do @redis.hmset(current_values_name, *current_values.flatten) end if current_values.any? ops end |
#indexes ⇒ Object
49 50 51 |
# File 'lib/relix/index_set.rb', line 49 def indexes (parent ? parent.indexes.merge(@indexes) : @indexes) end |
#key_prefix(name) ⇒ Object
121 122 123 |
# File 'lib/relix/index_set.rb', line 121 def key_prefix(name) "#{@klass.name}:#{name}" end |
#keyer(value = nil, options = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/relix/index_set.rb', line 28 def keyer(value=nil, ={}) if value @keyer = value.new(@klass, ) else (@keyer || parent.keyer) end end |
#lookup(&block) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/relix/index_set.rb', line 53 def lookup(&block) if block query = Query.new(self) yield(query) query.run else primary_key_index.all(@redis) end end |
#parent ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/relix/index_set.rb', line 125 def parent unless @parent || @parent == false parent = @klass.superclass @parent = (parent.respond_to?(:relix) ? parent.relix : false) end @parent end |
#primary_key(accessor) ⇒ Object Also known as: pk
12 13 14 |
# File 'lib/relix/index_set.rb', line 12 def primary_key(accessor) @primary_key_index = add_index(:primary_key, accessor) end |
#primary_key_index ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/relix/index_set.rb', line 17 def primary_key_index unless @primary_key_index if parent @primary_key_index = parent.primary_key_index else raise MissingPrimaryKeyError.new("You must declare a primary key for #{@klass.name}") end end @primary_key_index end |