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
# 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



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

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

#bulk_add(entities) ⇒ Object



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

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

#bulk_delete(uids) ⇒ Object



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

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

#delete(uid) ⇒ Object



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

def delete(uid)
  validate :delete, uid

  unmap entities.delete(uid)
end

#each(&block) ⇒ Object



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

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

#get(uid) ⇒ Object



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

def get(uid)
  validate :get, uid

  unmap(entities[uid])
end

#has_uid?(uid) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#option(key, value = nil) ⇒ Object



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

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

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#sizeObject



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

def size
  entities.size
end

#update(entity) ⇒ Object



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

def update(entity)
  validate :update, entity

  entities[entity.uid] = map(entity)

  get entity.uid
end