Class: Zermelo::Associations::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/zermelo/associations/index.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent_klass, name) ⇒ Index

Returns a new instance of Index.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/zermelo/associations/index.rb', line 9

def initialize(parent_klass, name)
  @parent_klass   = parent_klass
  @attribute_name = name

  @backend   = parent_klass.send(:backend)

  @indexers = {}

  parent_klass.send(:with_index_data, name.to_sym) do |data|
    @attribute_type = data.type
  end
end

Instance Method Details

#add_id(id, value) ⇒ Object



27
28
29
30
# File 'lib/zermelo/associations/index.rb', line 27

def add_id(id, value)
  return unless indexer = key(value)
  @backend.add(indexer, id)
end

#delete_id(id, value) ⇒ Object



22
23
24
25
# File 'lib/zermelo/associations/index.rb', line 22

def delete_id(id, value)
  return unless indexer = key(value)
  @backend.delete(indexer, id)
end

#key(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zermelo/associations/index.rb', line 37

def key(value)
  index_keys = @backend.index_keys(@attribute_type, value)
  raise "Can't index '#{@value}' (#{@attribute_type}" if index_keys.nil?

  @indexers[index_keys.join(":")] ||= Zermelo::Records::Key.new(
    :klass  => @parent_klass,
    :name   => "by_#{@attribute_name}:#{index_keys.join(':')}",
    :type   => :set,
    :object => :index
  )
end

#key_dumpObject



49
50
51
52
53
54
55
56
57
# File 'lib/zermelo/associations/index.rb', line 49

def key_dump
  k = Zermelo::Records::Key.new(
    :klass  => @parent_klass,
    :name   => "by_#{@attribute_name}:*",
    :type   => :set,
    :object => :index
  )
  [@backend.key_to_backend_key(k), k]
end

#move_id(id, value_from, indexer_to, value_to) ⇒ Object



32
33
34
35
# File 'lib/zermelo/associations/index.rb', line 32

def move_id(id, value_from, indexer_to, value_to)
  return unless indexer = key(value_from)
  @backend.move(indexer, id, indexer_to.key(value_to), id)
end