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.



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

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

  @backend   = parent_klass.send(:backend)
  @class_key = parent_klass.send(:class_key)

  @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



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

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

#delete_id(id, value) ⇒ Object



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

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

#key(value) ⇒ Object



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

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  => @class_key,
    :name   => "by_#{@attribute_name}:#{index_keys.join(':')}",
    :type   => :set,
    :object => :index
  )
end

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



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

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))
end