cliutils 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96c4a2208dc32603d60a3d1615cbcee89e35089f
4
- data.tar.gz: 74a8b7774dc1b13e054de3449e2fdf8666648673
3
+ metadata.gz: 1bba7fa0a39d1e55ba1a84a6ffadb7ebd649d2a0
4
+ data.tar.gz: b4ba62f707e30bc694310c612b738458739cf139
5
5
  SHA512:
6
- metadata.gz: e29a35f2889f09151ff12dcd1db686f8b984a5fa84eb8d93e2e54cbdf3975afa20e71c444dd549a6f877ee07bc9553d708f36affa1963f681a0d1bfb41e7c942
7
- data.tar.gz: d9c9efd319ad362fa145c8abee217898fccb46f175fa3d5a120a922a1c714c5e95e11162e99bd80cb653d881e840d9cfac28f0fbfe902b5077952dbc2d9dc923
6
+ metadata.gz: a64a0501b9af9eec35d8d80d6e2ff25926e653705e3ddfd446345cc1614f55eb3d6da5e8351182ea069eaa88f8099e50586682d5c3bd0a085b324e06125166ca
7
+ data.tar.gz: 1aa47c68d25a0981f8f08ec10d9a99ba5cce648f74d155a51181c33034eb683a289d7937d1e5106b3d594fcd6b9a3661a11d5f901749a479a7b55e53601d5347
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cliutils (1.0.7)
4
+ cliutils (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/HISTORY.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 1.1.0 (2014-04-01)
2
+
3
+ * Added notice of valid options in Prefs
4
+ * Added ability for Prefs to use defaults from Configuration values
5
+ * Increased default wrapping to 80 characters
6
+ * Fixed a bug in which Prefs would not convert answer keys to symbols
7
+
1
8
  # 1.0.7 (2014-03-30)
2
9
 
3
10
  * Modified Messenging targets to be a Hash for easier lookup/deletion
data/README.md CHANGED
@@ -178,7 +178,7 @@ For instance, let's say you wanted to log a few messages to both your user's STD
178
178
  ```Ruby
179
179
  messenger.info('This should only appear in STDOUT.')
180
180
 
181
- #messenger.attach takes a Hash of string/symbol keys
181
+ # messenger.attach takes a Hash of string/symbol keys
182
182
  # and Logger values (so you can refer to them later on).
183
183
  messenger.attach(MY\_FILE\_LOGGER: Logger.new('file.txt'))
184
184
 
@@ -328,6 +328,14 @@ configuration.ingest(prefs)
328
328
  configuration.save
329
329
  ```
330
330
 
331
+ Note that you can also initialize a `Prefs` object with a Configurator:
332
+
333
+ ```Ruby
334
+ prefs = CLIUtils::Prefs.new('path/to/yaml/file', my_configurator)
335
+ ```
336
+
337
+ In this case, `Prefs` will look to see if any values already exist for a specific prompt; if so, that value will be used as the default, rather than the default specified in the YAML.
338
+
331
339
  ### Why a Prefs Class?
332
340
 
333
341
  I've written apps that need to request user input at various times for multiple different things; as such, I thought it'd be easier to have those scenarios chunked up. You can always wrap `Prefs` into a module singleton if you wish.
@@ -57,8 +57,8 @@ module CLIUtils
57
57
  def ingest_prefs(prefs)
58
58
  fail 'Invaid Prefs class' if !prefs.kind_of?(Prefs) || prefs.answers.nil?
59
59
  prefs.answers.each do |p|
60
- add_section(p[:section]) unless @data.key?(p[:section])
61
- @data[p[:section]].merge!(p[:key] => p[:answer])
60
+ add_section(p[:section].to_sym) unless @data.key?(p[:section].to_sym)
61
+ @data[p[:section].to_sym].merge!(p[:key].to_sym => p[:answer])
62
62
  end
63
63
  end
64
64
 
@@ -1,4 +1,5 @@
1
1
  require 'cliutils/pretty-io'
2
+ require 'cliutils/configuration'
2
3
 
3
4
  module CLIUtils
4
5
  # Engine to derive preferences from a YAML file, deliver
@@ -6,12 +7,16 @@ module CLIUtils
6
7
  class Prefs
7
8
  include PrettyIO
8
9
  # Stores answers to prompt questions.
9
- # @return [Hash]
10
+ # @return [Array]
10
11
  attr_reader :answers
11
12
 
12
13
  # Stores the filepath (if it exists) to the prefs file.
13
14
  # @return [String]
14
15
  attr_reader :config_path
16
+
17
+ # Stores a Configurator instance.
18
+ # @return [Configurator]
19
+ attr_reader :configurator
15
20
 
16
21
  # Stores answers to prompt questions.
17
22
  # @return [Hash]
@@ -19,9 +24,11 @@ module CLIUtils
19
24
 
20
25
  # Reads prompt data from and stores it.
21
26
  # @param [<String, Hash, Array>] data Filepath to YAML, Hash, or Array
27
+ # @param [Configurator] configurator The configurator to take default values from
22
28
  # @return [void]
23
- def initialize(data)
29
+ def initialize(data, configurator = nil)
24
30
  @answers = []
31
+ @configurator = configurator
25
32
  @prompts = {}
26
33
 
27
34
  case data
@@ -71,20 +78,28 @@ module CLIUtils
71
78
  # @param [Hash] p The prompt
72
79
  # @return [void]
73
80
  def _deliver_prompt(p)
81
+ default = p[:default]
82
+
83
+ unless @configurator.nil?
84
+ unless @configurator.data[p[:section].to_sym].nil?
85
+ config_val = @configurator.data[p[:section].to_sym][p[:key].to_sym]
86
+ default = config_val unless config_val.nil?
87
+ end
88
+ end
89
+
74
90
  if p[:options].nil?
75
- pref = prompt(p[:prompt], p[:default])
91
+ pref = prompt(p[:prompt], default)
76
92
  else
77
93
  valid_option_chosen = false
78
94
  until valid_option_chosen
79
- pref = prompt(p[:prompt], p[:default])
95
+ pref = prompt(p[:prompt], default)
80
96
  if p[:options].include?(pref)
81
97
  valid_option_chosen = true
82
98
  else
83
- error("Invalid option chosen: #{ pref }")
99
+ error("Invalid option chosen (\"#{ pref }\"); valid options are: #{ p[:options] }")
84
100
  end
85
101
  end
86
102
  end
87
-
88
103
  p[:answer] = pref
89
104
  @answers << p
90
105
  end
@@ -10,7 +10,6 @@ module CLIUtils
10
10
  # CLIMessenger Module
11
11
  # Outputs color-coordinated messages to a CLI
12
12
  module PrettyIO
13
-
14
13
  class << self
15
14
  # Determines whether wrapping should be enabled.
16
15
  # @return [Boolean]
@@ -22,7 +21,7 @@ module CLIUtils
22
21
  end
23
22
 
24
23
  self.wrap = true
25
- self.wrap_char_limit = 40
24
+ self.wrap_char_limit = 80
26
25
 
27
26
  # Hook that triggers when this module is included.
28
27
  # @param [Object] k The includer object
@@ -1,4 +1,4 @@
1
1
  module CLIUtils
2
2
  # The current version of the gem
3
- VERSION = "1.0.7"
3
+ VERSION = "1.1.0"
4
4
  end
@@ -1,7 +1,10 @@
1
1
  prompts:
2
- - prompt: What is the hostname of your DD-WRT router?
2
+ - prompt: What is the local hostname of your DD-WRT router?
3
3
  default: 192.168.1.1
4
- key: hostname
4
+ key: local_hostname
5
+ section: ssh_info
6
+ - prompt: What is the remote hostname of your DD-WRT router?
7
+ key: remote_hostname
5
8
  section: ssh_info
6
9
  - prompt: What is the SSH username of your DD-WRT router?
7
10
  default: root
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Bach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler