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) ⇒ Brain

Returns a new instance of Brain.



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

def initialize(sheldon_data_dir)
  @location = sheldon_data_dir
  @memory = Memory.new(@location)
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#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



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

def forget(recall_cue)
  entry = memory.recall(recall_cue)
  brain_path = brain_directory_for_cue(recall_cue)
  destination_path = add_home(entry[:filepath])
  FileUtils.rm_r(destination_path) if File.symlink?(destination_path)
  FileUtils.rm_r(brain_path) if Dir.exist?(brain_path)

  memory.forget(recall_cue)
end

#has_cue?(recall_cue) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/sheldon/brain.rb', line 20

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

#learn(recall_cue, abs_learn_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sheldon/brain.rb', line 24

def learn(recall_cue, abs_learn_path)
  raise "recall cue cannot be empty." if recall_cue.strip.empty?
  raise "This cue has already been used." if has_cue?(recall_cue)
  raise "Unable to find a file or folder at #{abs_learn_path}" unless File.exist?(abs_learn_path)

  brain_path = brain_directory_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



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

def list_cues
  memory.list_cues
end

#path_for_cue(recall_cue) ⇒ Object



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

def path_for_cue(recall_cue)
  raise "no entry for cue '#{recall_cue}'" unless memory.has_cue?(recall_cue)
  brain_directory = brain_directory_for_cue(recall_cue)
  get_content(brain_directory)
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  memory.present?
end

#recall(recall_cue, opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sheldon/brain.rb', line 50

def recall(recall_cue, opts={})
  entry = memory.recall(recall_cue)
  destination_path = add_home(entry[:filepath])
  destination_dir = File.dirname(destination_path)

  raise DestinationNotEmptyException, "#{destination_path} already exists." if File.exist?(destination_path) && !opts[:overwrite]

  FileUtils.mkdir_p(destination_dir) unless File.directory?(destination_dir)
  brain_path = brain_directory_for_cue(recall_cue)
  FileUtils.ln_s(get_content(brain_path), destination_path, force: opts[:overwrite])
  return true
end

#recalled?(recall_cue) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/sheldon/brain.rb', line 63

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



71
72
73
# File 'lib/sheldon/brain.rb', line 71

def size
  memory.size
end