Class: Memory
- Inherits:
-
Object
- Object
- Memory
- Defined in:
- lib/sheldon/memory.rb
Instance Method Summary collapse
- #add(recall_cue, hash) ⇒ Object
- #forget(recall_cue) ⇒ Object
- #has_cue?(recall_cue) ⇒ Boolean
-
#initialize(brain_location) ⇒ Memory
constructor
A new instance of Memory.
- #list_cues ⇒ Object
- #present? ⇒ Boolean
- #recall(recall_cue) ⇒ Object
- #save! ⇒ Object
- #size ⇒ Object
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
24 25 26 |
# File 'lib/sheldon/memory.rb', line 24 def has_cue?(recall_cue) list_cues.any?{ |cue| cue == recall_cue } end |
#list_cues ⇒ Object
28 29 30 |
# File 'lib/sheldon/memory.rb', line 28 def list_cues @database.transaction { @database.roots } end |
#present? ⇒ 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 |
#size ⇒ Object
45 46 47 |
# File 'lib/sheldon/memory.rb', line 45 def size list_cues.size end |