Class: Brain
- Inherits:
-
Object
- Object
- Brain
- Defined in:
- lib/sheldon/brain.rb
Instance Attribute Summary collapse
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
Instance Method Summary collapse
- #forget(recall_cue) ⇒ Object
- #has_cue?(recall_cue) ⇒ Boolean
-
#initialize(sheldon_data_dir) ⇒ Brain
constructor
A new instance of Brain.
- #learn(recall_cue, abs_learn_path) ⇒ Object
- #list_cues ⇒ Object
- #present? ⇒ Boolean
- #recall(recall_cue) ⇒ Object
- #recalled?(recall_cue) ⇒ Boolean
- #size ⇒ Object
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
#location ⇒ Object (readonly)
Returns the value of attribute location.
3 4 5 |
# File 'lib/sheldon/brain.rb', line 3 def location @location end |
#memory ⇒ Object (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_path_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
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_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_cues ⇒ Object
36 37 38 |
# File 'lib/sheldon/brain.rb', line 36 def list_cues memory.list_cues end |
#present? ⇒ Boolean
40 41 42 |
# File 'lib/sheldon/brain.rb', line 40 def present? memory.present? end |
#recall(recall_cue) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sheldon/brain.rb', line 44 def recall(recall_cue) 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) 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) return true end |
#recalled?(recall_cue) ⇒ Boolean
56 57 58 59 60 61 62 |
# File 'lib/sheldon/brain.rb', line 56 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 |
#size ⇒ Object
64 65 66 |
# File 'lib/sheldon/brain.rb', line 64 def size memory.size end |