Class: Eternity::CollectionIndex

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/eternity/collection_index.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CollectionIndex

Returns a new instance of CollectionIndex.



7
8
9
# File 'lib/eternity/collection_index.rb', line 7

def initialize(options)
  @index = Restruct::Hash.new options
end

Instance Method Details

#[](id) ⇒ Object



19
20
21
# File 'lib/eternity/collection_index.rb', line 19

def [](id)
  include?(id) ? Blob.new(:data, index[id]) : nil
end

#collection_nameObject



11
12
13
# File 'lib/eternity/collection_index.rb', line 11

def collection_name
  index.id.sections.last
end

#delete(id) ⇒ Object



33
34
35
36
# File 'lib/eternity/collection_index.rb', line 33

def delete(id)
  raise "#{collection_name.capitalize} #{id} not found" unless index.key? id
  index.delete id
end

#idsObject



38
39
40
# File 'lib/eternity/collection_index.rb', line 38

def ids
  index.keys
end

#include?(id) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/eternity/collection_index.rb', line 15

def include?(id)
  index.key? id
end

#insert(id, data) ⇒ Object



23
24
25
26
# File 'lib/eternity/collection_index.rb', line 23

def insert(id, data)
  raise "#{collection_name.capitalize} #{id} already exists" if index.key? id
  index[id] = Blob.write :data, data
end

#update(id, data) ⇒ Object



28
29
30
31
# File 'lib/eternity/collection_index.rb', line 28

def update(id, data)
  raise "#{collection_name.capitalize} #{id} not found" unless index.key? id
  index[id] = Blob.write :data, data
end