Class: Brain

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheldon_data_dir, opts = {}) ⇒ Brain

Returns a new instance of Brain.



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

def initialize(sheldon_data_dir, opts = {})
  @brain_location = sheldon_data_dir
  @memory = opts[:memory] || Memory.new(@brain_location)
end

Instance Attribute Details

#memoryObject (readonly)

Returns the value of attribute memory.



3
4
5
# File 'lib/sheldon/brain.rb', line 3

def memory
  @memory
end

Instance Method Details

#forget(recall_cue) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/sheldon/brain.rb', line 28

def forget(recall_cue)
  entry = memory.recall(recall_cue)
  brain_path = brain_path_for_cue(recall_cue)
  destination_path = add_home(entry[:filepath])
  FileUtils.rm_r(brain_path)
  FileUtils.rm_r(destination_path)

  memory.forget(recall_cue)
end

#has_cue?(recall_cue) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sheldon/brain.rb', line 46

def has_cue?(recall_cue)
  memory.has_cue?(recall_cue)
end

#learn(recall_cue, abs_learn_path) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/sheldon/brain.rb', line 11

def learn(recall_cue, abs_learn_path)
  brain_path = brain_path_for_cue(recall_cue)
  FileUtils.mkdir_p(brain_path)
  FileUtils.mv(abs_learn_path, brain_path)
  entry = { filepath: remove_home(abs_learn_path) }
  memory.add(recall_cue, entry)
end

#list_cuesObject



54
55
56
# File 'lib/sheldon/brain.rb', line 54

def list_cues
  memory.list_cues
end

#recall(recall_cue) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/sheldon/brain.rb', line 19

def recall(recall_cue)
  entry = memory.recall(recall_cue)
  destination_path = add_home(entry[:filepath])
  destination_dir = File.dirname(destination_path)
  FileUtils.mkdir_p(destination_dir) unless File.directory?(destination_dir)
  brain_path = brain_path_for_cue(recall_cue)
  FileUtils.ln_s(get_content(brain_path), destination_path, force: true)
end

#recalled?(recall_cue) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/sheldon/brain.rb', line 38

def recalled?(recall_cue)
  entry = memory.recall(recall_cue)
  destination_path = add_home(entry[:filepath])
  destination_dir = File.dirname(destination_path)
  # Check for presence of symlink and that the symlink isn't broken
  File.symlink?(destination_path) && File.exists?(File.readlink(destination_path))
end

#sizeObject



50
51
52
# File 'lib/sheldon/brain.rb', line 50

def size
  memory.size
end