Class: Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/sheldon/memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(brain_location) ⇒ Memory



5
6
7
8
# File 'lib/sheldon/memory.rb', line 5

def initialize(brain_location)
  database_path = File.join(brain_location, "db.yml")
  @database = YAML::Store.new(database_path)
end

Instance Method Details

#add(recall_cue, hash) ⇒ Object



10
11
12
13
14
15
# File 'lib/sheldon/memory.rb', line 10

def add(recall_cue, hash)
  raise "cue already used" if has_cue?(recall_cue)
  @database.transaction do
    @database[recall_cue] = hash
  end
end

#has_cue?(recall_cue) ⇒ Boolean



30
31
32
# File 'lib/sheldon/memory.rb', line 30

def has_cue?(recall_cue)
  list_cues.any?{ |cue| cue == recall_cue }
end

#list_cuesObject



26
27
28
# File 'lib/sheldon/memory.rb', line 26

def list_cues
  @database.transaction { @database.roots }
end

#recall(recall_cue) ⇒ Object



17
18
19
20
# File 'lib/sheldon/memory.rb', line 17

def recall(recall_cue)
  raise "no entry for cue" unless has_cue?(recall_cue)
  @database.transaction { @database[recall_cue] }
end

#sizeObject



22
23
24
# File 'lib/sheldon/memory.rb', line 22

def size
  list_cues.size
end