cliutils 1.2.4 → 1.2.5

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: cca92462495e55bd03893b1ce6a19c1830288b56
4
- data.tar.gz: dd8f5e2ed4434f381f0bca8a66d34fcab3cf6834
3
+ metadata.gz: 00fd89b1aa359ab732d3a436b51aa26d515f0ef3
4
+ data.tar.gz: 1aa1aa258ca1a09fe55e8fdf9c2d336e54b8ba9b
5
5
  SHA512:
6
- metadata.gz: 226d3b909c6a8f887818491f4611db68128e991c818c4aeb8268316f3f3d0415bf06a5a6967e961c6377f0cfbc3149b8dd94e3ad68dd1e4dcb18086cecdbc46a
7
- data.tar.gz: 0404033a25e60197ec39c4e58cde3a72616d1a09c9d051a44d0c4e74a85e349d3c1b949c33516bc847f38ab77c959de3cf5a1c1109bdc5cbd43d8e9b57e33c82
6
+ metadata.gz: 607089b0e6bce175030d6304b411c4e58947c3573653b90f55247d54a6f6353c2896946fa99fe844cb8e87afd8aecc00e540c392d50b5a17df5c72a34ea10544
7
+ data.tar.gz: e763d764171825dc2391413c7f7a822ccfbe8af1ef219401539aa6313bc70c3a321667640163386b281f6d3e04e829425ca227b73267fbb4511fabd2d053ceb7
data/HISTORY.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.2.5 (2014-04-07)
2
+
3
+ * Added Configurator version checking
4
+
1
5
  # 1.2.4 (2014-04-06)
2
6
 
3
7
  * Cleanup and refactoring
data/README.md CHANGED
@@ -220,14 +220,14 @@ include CLIUtils::Configuration
220
220
  ### Loading a Configuration File
221
221
 
222
222
  ```Ruby
223
- CLIUtils::Configuration.configuration = '~/.my-app-config'
223
+ load_configuration('~/.my-app-config')
224
224
  ```
225
225
 
226
- If there's data in there, it will be consumed into `configuration`'s `data` property.
226
+ If there's data in there, it will be consumed into the `configurator`'s `data` property.
227
227
 
228
228
  ### Adding/Removing Sections
229
229
 
230
- Sections are top levels of the configuration file and are managed via the `configuration` object:
230
+ Sections are top levels of the configuration file and are managed via the `configurator` object:
231
231
 
232
232
  ```Ruby
233
233
  configuration.add_section(:user_data)
@@ -237,7 +237,7 @@ configuration.delete_section(:program_data)
237
237
 
238
238
  ### Adding Data to Sections
239
239
 
240
- There are two ways data can be managed in `configuration`: via its `@data` property or via some magic methods; your call:
240
+ There are two ways data can be managed in `configurator`: via its `@data` property or via some magic methods; your call:
241
241
 
242
242
  ```Ruby
243
243
  configuration.data[:user_data].merge!(username: 'bob')
@@ -261,6 +261,33 @@ user_data:
261
261
  username: bob
262
262
  ```
263
263
 
264
+ ### Checking Configuration Versions
265
+
266
+ Often, you'll want to check the user's current version of your app against the last version that required some sort of configuration change. `configurator` allows for this via its `compare_version` method:
267
+
268
+ ```Ruby
269
+ # Tell your configurator the name of the key that
270
+ # stores the app's version in its configuration file.
271
+ configuration.cur_version_key = :APP_VERSION
272
+
273
+ # Tell your configurator the name of the key that
274
+ # stores the last version that needed a configuration
275
+ # change.
276
+ configuration.last_version_key = :NEWEST_CONFIG_VERSION
277
+
278
+ # Run the check and use a block to get
279
+ # the current and "last-needing-changes"
280
+ # versions (and do something about it).
281
+ configuration.compare_version do |c, l|
282
+ if c < l
283
+ puts "You need to update your app; here's how:"
284
+ # ...do stuff...
285
+ else
286
+ puts "No need to update your app's config file!"
287
+ end
288
+ end
289
+ ```
290
+
264
291
  ## Prefs
265
292
 
266
293
  Many times, CLI apps need to ask their users some questions, collect the feedback, validate it, and store it. CLIUtils makes this a breeze via the `Prefs` class.
@@ -419,7 +446,7 @@ prompts:
419
446
  - local_filepath
420
447
  ```
421
448
 
422
- This will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved as `/Users/bachya/.ssh/`).
449
+ The `local_filepath` behavior will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved as `/Users/bachya/.ssh/`).
423
450
 
424
451
  `Prefs` currently supports these behaviors:
425
452
 
@@ -430,7 +457,11 @@ This will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved
430
457
  Once the user has answered all the preference prompts, you can fold those answers back into a Configurator using the `ingest` method:
431
458
 
432
459
  ```Ruby
460
+ # Ingest the Prefs answers into our
461
+ # Configurator.
433
462
  configuration.ingest_prefs(prefs)
463
+
464
+ # Save it to the filesystem.
434
465
  configuration.save
435
466
  ```
436
467
 
@@ -439,6 +470,9 @@ configuration.save
439
470
  Note that you can also initialize a `Prefs` object with a Configurator:
440
471
 
441
472
  ```Ruby
473
+ # Scans my_configurator for values to the
474
+ # questions posed by prefs; if found, use
475
+ # them as defaults.
442
476
  prefs = CLIUtils::Prefs.new('path/to/yaml/file', my_configurator)
443
477
  ```
444
478
 
@@ -33,7 +33,7 @@ module CLIUtils
33
33
  # a Configurator.
34
34
  # @param [String] path The filepath to use
35
35
  # @return [void]
36
- def self.configuration=(path)
36
+ def load_configuration(path)
37
37
  @@configuration = Configurator.new(path)
38
38
  end
39
39
  end
@@ -5,6 +5,17 @@ module CLIUtils
5
5
  # Manages any configuration values and the flat YAML file
6
6
  # into which they get stored.
7
7
  class Configurator
8
+ # Stores the Configurator key that refers
9
+ # to the current configuration version.
10
+ # @return [Symbol]
11
+ attr_accessor :cur_version_key
12
+
13
+ # Stores the Configurator key that refers
14
+ # to the value at which the app last changed
15
+ # config versions.
16
+ # @return [Symbol]
17
+ attr_accessor :last_version_key
18
+
8
19
  # Stores the path to the configuration file.
9
20
  # @return [String]
10
21
  attr_reader :config_path
@@ -13,6 +24,11 @@ module CLIUtils
13
24
  # @return [Hash]
14
25
  attr_reader :data
15
26
 
27
+ # Stores the section that contains the version
28
+ # keys.
29
+ # @return [Symbol]
30
+ attr_accessor :version_section
31
+
16
32
  # Initializes configuration from a flat file.
17
33
  # @param [String] path The filepath to the config YAML
18
34
  # @return [void]
@@ -39,6 +55,41 @@ module CLIUtils
39
55
  end
40
56
  end
41
57
 
58
+ # Checks to see whether the passed key (and a
59
+ # non-nil value) exist in the @data Hash.
60
+ # @param [Symbol] key The key to search for
61
+ # @return [Boolean]
62
+ def check_key_and_value(key)
63
+ !@data.recursive_find_by_key(key).nil? &&
64
+ !@data.recursive_find_by_key(key).empty?
65
+ end
66
+
67
+ # Compares the current version (if it exists) to
68
+ # the last version that needed a configuration
69
+ # change (if it exists). Assuming they exist and
70
+ # that the current version is behind the "last-config"
71
+ # version, execute a passed block.
72
+ # @return [void]
73
+ def compare_version
74
+ unless check_key_and_value(@cur_version_key)
75
+ fail 'Cannot check version; no current version found'
76
+ end
77
+
78
+ unless check_key_and_value(@last_version_key)
79
+ fail 'Cannot check version; no previous version found'
80
+ end
81
+
82
+ c_version = @data.recursive_find_by_key(@cur_version_key)
83
+ c_version_gem = Gem::Version.new(c_version)
84
+
85
+ l_version = @data.recursive_find_by_key(@last_version_key)
86
+ l_version_gem = Gem::Version.new(l_version)
87
+
88
+ if c_version_gem < l_version_gem
89
+ yield c_version, l_version
90
+ end
91
+ end
92
+
42
93
  # Removes a section to the config file (if it exists).
43
94
  # @param [String] section_name The section to remove
44
95
  # @return [void]
@@ -1,5 +1,5 @@
1
1
  # Stores constants to use.
2
2
  module CLIUtils
3
3
  # The current version of the gem
4
- VERSION = '1.2.4'
4
+ VERSION = '1.2.5'
5
5
  end
@@ -62,6 +62,33 @@ class Hash
62
62
  _deep_transform_keys_in_object!(self, &block)
63
63
  end
64
64
 
65
+ # Recursively searches a hash for the passed
66
+ # key and returns the value (if there is one).
67
+ # http://stackoverflow.com/a/2239847/327179
68
+ # @param [<Symbol, String>] key The key to search for
69
+ # @return [Multiple]
70
+ def recursive_find_by_key(key)
71
+ # Create a stack of hashes to search through for the needle which
72
+ # is initially this hash
73
+ stack = [ self ]
74
+
75
+ # So long as there are more haystacks to search...
76
+ while (to_search = stack.pop)
77
+ # ...keep searching for this particular key...
78
+ to_search.each do |k, v|
79
+ # ...and return the corresponding value if it is found.
80
+ return v if (k == key)
81
+
82
+ # If this value can be recursively searched...
83
+ if (v.respond_to?(:recursive_find_by_key))
84
+ # ...push that on to the list of places to search.
85
+ stack << v
86
+ end
87
+ end
88
+ end
89
+ yield if block_given?
90
+ end
91
+
65
92
  private
66
93
 
67
94
  # Modification to deep_transform_keys that allows for
@@ -4,6 +4,8 @@ module CLIUtils
4
4
  # PrefValidation Module
5
5
  # Validation rules that can be applied to a Pref.
6
6
  module PrefValidation
7
+ # Struct to contain a validation result
8
+ # and a result message.
7
9
  Validator = Struct.new(:code, :message)
8
10
 
9
11
  # Validates that a value is only letters.
@@ -96,7 +96,7 @@ module CLIUtils
96
96
 
97
97
  # Generates an Array of Prefs based on passed
98
98
  # in data.
99
- # @param [Hash] pref_data Loaded pref data
99
+ # @param [Hash] pref_data_hash Loaded pref data
100
100
  # @return [Array]
101
101
  def _generate_prefs(pref_data_hash)
102
102
  pref_data_hash[:prompts].map { |p| CLIUtils::Pref.new(p) }
@@ -48,4 +48,16 @@ class TestConfigurator < Test::Unit::TestCase
48
48
  assert_output("---\nsection1:\n a: test\n b: test\n") { puts f.read }
49
49
  end
50
50
  end
51
+
52
+ def test_compare_version
53
+ @config.add_section(:app_data)
54
+ @config.app_data.merge!({ VERSION: '1.0.0', NEWEST_CONFIG_VERSION: '1.8.8' })
55
+
56
+ @config.cur_version_key = :VERSION
57
+ @config.last_version_key = :NEWEST_CONFIG_VERSION
58
+
59
+ @config.compare_version do |c, l|
60
+ assert_output('true') { print c < l }
61
+ end
62
+ end
51
63
  end
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.2.4
4
+ version: 1.2.5
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-04-06 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler