class Rohbau::Index

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

add(entity) click to toggle source
# 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
all() click to toggle source
# File lib/rohbau/index.rb, line 71
def all
  entities.values.map(&method(:unmap))
end
bulk_add(entities) click to toggle source
# File lib/rohbau/index.rb, line 39
def bulk_add(entities)
  entities.map do |entity|
    add entity
  end
end
bulk_delete(uids) click to toggle source
# File lib/rohbau/index.rb, line 65
def bulk_delete(uids)
  uids.map do |uid|
    delete uid
  end
end
delete(uid) click to toggle source
# File lib/rohbau/index.rb, line 59
def delete(uid)
  validate :delete, uid

  unmap entities.delete(uid)
end
each(&block) click to toggle source
# File lib/rohbau/index.rb, line 75
def each(&block)
  all.each(&block)
end
get(uid) click to toggle source
# File lib/rohbau/index.rb, line 45
def get(uid)
  validate :get, uid

  unmap(entities[uid])
end
has_uid?(uid) click to toggle source
# File lib/rohbau/index.rb, line 83
def has_uid?(uid)
  entities.key?(uid)
end
option(key, value = nil) click to toggle source
# 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) click to toggle source
# File lib/rohbau/index.rb, line 95
def option?(key)
  !!@options[key]
end
size() click to toggle source
# File lib/rohbau/index.rb, line 79
def size
  entities.size
end
update(entity) click to toggle source
# File lib/rohbau/index.rb, line 51
def update(entity)
  validate :update, entity

  entities[entity.uid] = map(entity)

  get entity.uid
end

Private Instance Methods

copy(entity) click to toggle source
# File lib/rohbau/index.rb, line 109
def copy(entity)
  Marshal.load(Marshal.dump entity)
end
entities() click to toggle source
# File lib/rohbau/index.rb, line 117
def entities
  @entities
end
map(entity) click to toggle source
# File lib/rohbau/index.rb, line 101
def map(entity)
  option(:mapper).call(entity)
end
next_uid() click to toggle source
# File lib/rohbau/index.rb, line 113
def next_uid
  "#{@last_uid += 1}"
end
unmap(mapped_entity) click to toggle source
# File lib/rohbau/index.rb, line 105
def unmap(mapped_entity)
  option(:unmapper).call(mapped_entity)
end
validate(method, *args) click to toggle source
# File lib/rohbau/index.rb, line 121
def validate(method, *args)
  @validator.public_send("validate_#{method}", *args)
end