Class: Memory

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

Instance Method Summary collapse

Constructor Details

#initialize(brain_location) ⇒ Memory

Returns a new instance of 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
16
# 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
  return true
end

#forget(recall_cue) ⇒ Object



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

def forget(recall_cue)
  raise "no entry for cue" unless has_cue?(recall_cue)
  @database.transaction{ @database.delete(recall_cue) }
  return true
end

#has_cue?(recall_cue) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/sheldon/memory.rb', line 24

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

#list_cuesObject



28
29
30
# File 'lib/sheldon/memory.rb', line 28

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

#present?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/sheldon/memory.rb', line 32

def present?
  File.exist?(@database.path)
end

#recall(recall_cue) ⇒ Object



40
41
42
43
# File 'lib/sheldon/memory.rb', line 40

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

#save!Object



36
37
38
# File 'lib/sheldon/memory.rb', line 36

def save!
  @database.transaction{@database.commit}
end

#sizeObject



45
46
47
# File 'lib/sheldon/memory.rb', line 45

def size
  list_cues.size
end