Class: Rohbau::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rohbau/index.rb

Defined Under Namespace

Classes: DefaultMapper, Validator

Instance Method Summary collapse

Constructor Details

#initializeIndex

Returns a new instance of Index.



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

def initialize
  @last_uid = 0
  @entities = ThreadSafe::Hash.new

  @validator = Validator.new(self)

  @options = {
    :uid_generation => true,
    :mapper         => DefaultMapper,
    :unmapper       => DefaultMapper
  }
end

Instance Method Details

#add(entity) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rohbau/index.rb', line 20

def add(entity)

  if option?(:uid_generation)
    validate :add, entity

    added_entity = copy(entity).tap do |new_entity|
      new_entity.uid = next_uid
      entities[new_entity.uid] = map(new_entity)
    end
  else
    validate :add_with_uid, entity

    added_entity = copy(entity).tap do |new_entity|
      entities[new_entity.uid] = map(new_entity)
    end
  end

  get added_entity.uid
end

#allObject



72
73
74
# File 'lib/rohbau/index.rb', line 72

def all
  entities.values.map(&method(:unmap))
end

#bulk_add(entities) ⇒ Object



40
41
42
43
44
# File 'lib/rohbau/index.rb', line 40

def bulk_add(entities)
  entities.map do |entity|
    add entity
  end
end

#bulk_delete(uids) ⇒ Object



66
67
68
69
70
# File 'lib/rohbau/index.rb', line 66

def bulk_delete(uids)
  uids.map do |uid|
    delete uid
  end
end

#delete(uid) ⇒ Object



60
61
62
63
64
# File 'lib/rohbau/index.rb', line 60

def delete(uid)
  validate :delete, uid

  unmap entities.delete(uid)
end

#each(&block) ⇒ Object



76
77
78
# File 'lib/rohbau/index.rb', line 76

def each(&block)
  all.each(&block)
end

#get(uid) ⇒ Object



46
47
48
49
50
# File 'lib/rohbau/index.rb', line 46

def get(uid)
  validate :get, uid

  unmap(entities[uid])
end

#has_uid?(uid) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/rohbau/index.rb', line 84

def has_uid?(uid)
  entities.key?(uid)
end

#option(key, value = nil) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/rohbau/index.rb', line 88

def option(key, value = nil)
  if value != nil
    @options[key] = value
  else
    @options[key]
  end
end

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/rohbau/index.rb', line 96

def option?(key)
  !!@options[key]
end

#sizeObject



80
81
82
# File 'lib/rohbau/index.rb', line 80

def size
  entities.size
end

#update(entity) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rohbau/index.rb', line 52

def update(entity)
  validate :update, entity

  entities[entity.uid] = map(entity)

  get entity.uid
end