sheldon 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0afba78030a4494e811effb363a21e26a7468be4
4
- data.tar.gz: b70fd44995b128c4ff2f47bfb5cca1dda631af89
3
+ metadata.gz: c591c521b0251b14812cbd07752b6ec6fdf7c3a8
4
+ data.tar.gz: d8db7161ee6f3093454ff3b6c708ce58be519016
5
5
  SHA512:
6
- metadata.gz: bb9d000e17243f6ea9d82a233e7a7c4769d046e17b879097e5ffbe1a7a939da9d87e9cddec94913595a6f5194cc1dc1af688fb995fd20f37860c43bbee812dfc
7
- data.tar.gz: 233d738e092355e4f10fb87c45622cf3713cc37bdb7405dd5c2268e63734ce8a09f9a1b1aa69daed75bba106e02110a12cabcc9ea60bd44d303aaf95e4416361
6
+ metadata.gz: f06d4369c5c00f3c130ed6e74b95ac02bec327e7b0f4e8e5027b77a19e929eb81f3ef928c87e6b0f905a6d3396132d9f5a324ce62ed3fd368a144bb4424270e0
7
+ data.tar.gz: 6f87f3b7ee054de3d16613d4fcbc469e16460162cd885272ef25bdd1fade99bf6ec0d6c4da9793de9ca0f7182925926e6362e928b4afdc8c520171ea606128f2
data/bin/sheldon CHANGED
@@ -12,16 +12,17 @@ module CLI
12
12
  desc "build path", "Tell Sheldon to build all config_ files in a directory to single master config"
13
13
  def build(rel_path_to_target)
14
14
  abs_learn_path = File.expand_path(rel_path_to_target)
15
- sheldon.build(abs_learn_path)
15
+ with_exception_handling{ sheldon.build(abs_learn_path) }
16
16
  announce("Built #{File.basename(rel_path_to_target)}")
17
17
  end
18
18
 
19
19
  desc "learn path", "Add a new file/folder to Sheldon's brain. Supply a recall_cue at runtime."
20
20
  def learn(rel_path_to_target)
21
21
  abs_learn_path = File.expand_path(rel_path_to_target)
22
- recall_cue = prompt_user("Recall Cue For File/Folder")
23
- sheldon.learn(recall_cue, abs_learn_path)
24
- sheldon.recall(recall_cue)
22
+ default_cue = File.basename(rel_path_to_target)
23
+ recall_cue = prompt_user("Recall Cue For File/Folder (#{default_cue})") || default_cue
24
+ with_exception_handling { sheldon.learn(recall_cue, abs_learn_path) }
25
+ with_exception_handling { sheldon.recall(recall_cue) }
25
26
  end
26
27
 
27
28
  desc "recall recall_cue", "Symlink a previously learnt file/directory to it's original location on the filesystem.\nUse `sheldon list` for available cues."
@@ -31,16 +32,23 @@ module CLI
31
32
  if options[:i]
32
33
  sheldon.list_cues.each do |recall_cue|
33
34
  answer = prompt_user("Recall #{recall_cue}? (Y/N)")
34
- sheldon.recall(recall_cue) if answer.downcase == "y"
35
+ with_exception_handling { sheldon.recall(recall_cue) if answer.downcase == "y" }
35
36
  end
36
37
  else
37
38
  recall_cue ||= prompt_user("Recall Cue For File/Folder")
38
- sheldon.recall(recall_cue)
39
+ with_exception_handling { sheldon.recall(recall_cue) }
39
40
  end
40
41
 
41
42
  announce("Recall Complete.")
42
43
  end
43
44
 
45
+ desc "forget recall_cue", "Remove file/folder from Sheldon's brain"
46
+ def forget(recall_cue=nil)
47
+ recall_cue ||= prompt_user("Recall Cue For File/Folder")
48
+ with_exception_handling { sheldon.forget(recall_cue) }
49
+ announce("I've forgotten about #{recall_cue}. Let us never mention it again.")
50
+ end
51
+
44
52
  desc "list", "List all recall cues known by Sheldon"
45
53
  def list
46
54
  sheldon.list_cues.each do |recall_cue|
@@ -55,31 +63,27 @@ module CLI
55
63
 
56
64
  private
57
65
 
58
- def announce(message)
59
- logo = "💥".encode("utf-8")
60
- puts logo + " Sheldon" + logo + " #{message}"
66
+ def prompt_user(prompt)
67
+ print(prompt + ": ")
68
+ input = STDIN.gets.chomp.strip
69
+ input.empty? ? nil : input
61
70
  end
62
71
 
63
72
  def green(message)
64
73
  puts "\e[32m#{message}\e[0m"
65
74
  end
66
75
 
67
- def prompt_user(prompt)
68
- print(prompt + ": ")
69
- STDIN.gets.chomp
70
- end
71
-
72
76
  def red(message)
73
77
  puts "\e[31m#{message}\e[0m"
74
78
  end
75
79
 
80
+ def announce(message)
81
+ logo = "💥".encode("utf-8")
82
+ puts logo + " Sheldon" + logo + " #{message}"
83
+ end
84
+
76
85
  def sheldon
77
- begin
78
- @sheldon ||= ::Sheldon.new(sheldon_data_dir)
79
- rescue MissingDataDirectoryException
80
- announce "The data directory (#{sheldon_data_dir}) doesn't exist. Please create the directory or set an alternative using the $SHELDON_DATA_DIR environment variable."
81
- exit
82
- end
86
+ with_exception_handling { @sheldon ||= ::Sheldon.new(sheldon_data_dir) }
83
87
  end
84
88
 
85
89
  def sheldon_data_dir
@@ -87,6 +91,13 @@ module CLI
87
91
  File.expand_path(path)
88
92
  end
89
93
 
94
+ def with_exception_handling(&block)
95
+ yield
96
+ rescue Exception => e
97
+ announce(e.message)
98
+ exit(1)
99
+ end
100
+
90
101
  end
91
102
  end
92
103
 
data/lib/sheldon/brain.rb CHANGED
@@ -9,9 +9,9 @@ class Brain
9
9
 
10
10
 
11
11
  def learn(recall_cue, abs_learn_path)
12
- cell = get_cell(recall_cue)
13
- FileUtils.mkdir_p(cell)
14
- FileUtils.mv(abs_learn_path, cell)
12
+ brain_path = brain_path_for_cue(recall_cue)
13
+ FileUtils.mkdir_p(brain_path)
14
+ FileUtils.mv(abs_learn_path, brain_path)
15
15
  entry = { filepath: remove_home(abs_learn_path) }
16
16
  memory.add(recall_cue, entry)
17
17
  end
@@ -21,8 +21,18 @@ class Brain
21
21
  destination_path = add_home(entry[:filepath])
22
22
  destination_dir = File.dirname(destination_path)
23
23
  FileUtils.mkdir_p(destination_dir) unless File.directory?(destination_dir)
24
- source_cell = get_cell(recall_cue)
25
- FileUtils.ln_s(read_cell(source_cell), destination_path, force: true)
24
+ brain_path = brain_path_for_cue(recall_cue)
25
+ FileUtils.ln_s(get_content(brain_path), destination_path, force: true)
26
+ end
27
+
28
+ def forget(recall_cue)
29
+ entry = memory.recall(recall_cue)
30
+ brain_path = brain_path_for_cue(recall_cue)
31
+ destination_path = add_home(entry[:filepath])
32
+ FileUtils.rm_r(brain_path)
33
+ FileUtils.rm_r(destination_path)
34
+
35
+ memory.forget(recall_cue)
26
36
  end
27
37
 
28
38
  def recalled?(recall_cue)
@@ -47,13 +57,13 @@ class Brain
47
57
 
48
58
  private
49
59
 
50
- def get_cell(recall_cue)
60
+ def brain_path_for_cue(recall_cue)
51
61
  File.join(@brain_location, recall_cue)
52
62
  end
53
63
 
54
- def read_cell(cell)
55
- basename = (Dir.entries(cell) - [".", "..", ".DS_Store"]).first
56
- File.join(cell, basename)
64
+ def get_content(path)
65
+ basename = (Dir.entries(path) - [".", "..", ".DS_Store"]).first
66
+ File.join(path, basename)
57
67
  end
58
68
 
59
69
  end
@@ -19,6 +19,11 @@ class Memory
19
19
  @database.transaction { @database[recall_cue] }
20
20
  end
21
21
 
22
+ def forget(recall_cue)
23
+ raise "no entry for cue" unless has_cue?(recall_cue)
24
+ @database.transaction{ @database.delete(recall_cue) }
25
+ end
26
+
22
27
  def size
23
28
  list_cues.size
24
29
  end
@@ -1,11 +1,14 @@
1
1
  require "fileutils"
2
2
 
3
3
  class Sheldon
4
- VERSION = "0.3.1".freeze
4
+ VERSION = "0.3.2".freeze
5
5
  attr_reader :brain, :builder
6
6
 
7
7
  def initialize(sheldon_data_dir, opts = {})
8
- raise MissingDataDirectoryException unless Dir.exists?(sheldon_data_dir)
8
+ unless Dir.exists?(sheldon_data_dir)
9
+ raise MissingDataDirectoryException, "The data directory (#{sheldon_data_dir}) doesn't exist. Please create the directory or set an alternative using the $SHELDON_DATA_DIR environment variable."
10
+ end
11
+
9
12
  @brain = opts[:brain] || Brain.new(sheldon_data_dir)
10
13
  @builder = opts[:builder] || Builder.new
11
14
  end
@@ -15,6 +18,7 @@ class Sheldon
15
18
  end
16
19
 
17
20
  def learn(recall_cue, abs_learn_path)
21
+ raise "recall cue cannot be empty." if recall_cue.strip.empty?
18
22
  if brain.has_cue?(recall_cue)
19
23
  raise "This cue has already been used."
20
24
  else
@@ -22,6 +26,16 @@ class Sheldon
22
26
  end
23
27
  end
24
28
 
29
+ def recall(recall_cue)
30
+ raise "Cue '#{recall_cue}' could not be found." unless brain.has_cue?(recall_cue)
31
+ brain.recall(recall_cue)
32
+ end
33
+
34
+ def forget(recall_cue)
35
+ raise "Cue '#{recall_cue}' could not be found." unless brain.has_cue?(recall_cue)
36
+ brain.forget(recall_cue)
37
+ end
38
+
25
39
  def list_cues
26
40
  brain.list_cues
27
41
  end
@@ -31,9 +45,4 @@ class Sheldon
31
45
  brain.recalled?(recall_cue)
32
46
  end
33
47
 
34
- def recall(recall_cue)
35
- raise "Cue '#{recall_cue}' could not be found." unless brain.has_cue?(recall_cue)
36
- brain.recall(recall_cue)
37
- end
38
-
39
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sheldon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-18 00:00:00.000000000 Z
11
+ date: 2017-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor