trick_bag 0.39.0 → 0.40.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.
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.40.0
2
+
3
+ * Added Validations.raise_on_invalid_value.
4
+ * Moved all validations into same module.
5
+
6
+
1
7
  ## v0.39.0
2
8
 
3
9
  * Added CoreTypes.clone_hash_except.
@@ -1,6 +1,6 @@
1
1
  module TrickBag
2
2
  module Validations
3
- module HashValidations
3
+
4
4
  module_function
5
5
 
6
6
  # Looks to see which keys, if any, are missing from the hash.
@@ -32,5 +32,4 @@ module HashValidations
32
32
  end
33
33
  end
34
34
  end
35
- end
36
35
 
@@ -1,6 +1,5 @@
1
1
  module TrickBag
2
2
  module Validations
3
- module ObjectValidations
4
3
 
5
4
  module_function
6
5
 
@@ -23,4 +22,3 @@ module ObjectValidations
23
22
  end
24
23
  end
25
24
  end
26
- end
@@ -0,0 +1,34 @@
1
+ module TrickBag
2
+ module Validations
3
+
4
+ module_function
5
+
6
+ # Used to succinctly (for the caller) check to see that a value provided by the caller
7
+ # is included in the passed enumerable. Raises an error if not, e.g.:
8
+ #
9
+ # raise_on_invalid_value('foo', [:bar, :baz], 'manufacturer')
10
+ #
11
+ # will raise an error with the following message:
12
+ #
13
+ # Invalid manufacturer 'foo'; must be one of: [:bar, :baz].
14
+ #
15
+ # If the passed value is included in the valid values, this method returns silently.
16
+ #
17
+ # @param value the value to check against the valid values
18
+ # @param the valid values, of which the value should be one
19
+ # @param label a string to include in the error message after "Invalid ", default to 'value'
20
+ # @param output_with_inspect if true, the values' inspect method will be called for the
21
+ # error string; this will preserve the colon in symbols; if false, to_s will be used.
22
+ def raise_on_invalid_value(value, valid_values, label = 'value', output_with_inspect = true)
23
+ missing = ! valid_values.include?(value)
24
+
25
+ if missing
26
+ values_display_array = output_with_inspect ? valid_values.map(&:inspect) : valid_values.map(&:to_s)
27
+ message = "Invalid #{label} '#{value}'; must be one of: [#{values_display_array.join(', ')}]."
28
+ raise message
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+
@@ -1,3 +1,3 @@
1
1
  module TrickBag
2
- VERSION = "0.39.0"
2
+ VERSION = "0.40.0"
3
3
  end
@@ -3,10 +3,10 @@ require_relative '../../spec_helper.rb'
3
3
  require 'trick_bag/validations/hash_validations'
4
4
 
5
5
  module TrickBag
6
- module Validations
7
- describe HashValidations do
8
6
 
9
- include HashValidations
7
+ describe Validations do
8
+
9
+ include Validations
10
10
 
11
11
  context '.missing_hash_entries' do
12
12
 
@@ -65,5 +65,4 @@ module Validations
65
65
  end
66
66
  end
67
67
  end
68
- end
69
68
 
@@ -3,21 +3,20 @@ require_relative '../../spec_helper.rb'
3
3
  require 'trick_bag/validations/object_validations'
4
4
 
5
5
  module TrickBag
6
- module Validations
7
- describe ObjectValidations do
8
6
 
9
- include ObjectValidations
7
+ describe Validations do
10
8
 
11
- specify 'a missing instance variable should raise an error' do
12
- vars = [:@this_name_could_not_possibly_be_defined_as_a_real_variable]
13
- expect(-> { raise_on_nil_instance_vars(self, vars) }).to raise_error
14
- end
9
+ include Validations
15
10
 
16
- specify 'an existing instance variable should NOT raise an error' do
17
- vars = [:@foo]
18
- -> { class AbCdEfG; def initialize; @foo = 'hi'; end; end }.()
19
- expect(-> { raise_on_nil_instance_vars(AbCdEfG.new, vars) }).not_to raise_error
20
- end
11
+ specify 'a missing instance variable should raise an error' do
12
+ vars = [:@this_name_could_not_possibly_be_defined_as_a_real_variable]
13
+ expect(-> { raise_on_nil_instance_vars(self, vars) }).to raise_error
21
14
  end
22
- end
15
+
16
+ specify 'an existing instance variable should NOT raise an error' do
17
+ vars = [:@foo]
18
+ -> { class AbCdEfG; def initialize; @foo = 'hi'; end; end }.()
19
+ expect(-> { raise_on_nil_instance_vars(AbCdEfG.new, vars) }).not_to raise_error
20
+ end
21
+ end
23
22
  end
@@ -0,0 +1,30 @@
1
+ require_relative '../../spec_helper.rb'
2
+
3
+ require 'trick_bag/validations/other_validations'
4
+
5
+ module TrickBag
6
+
7
+ describe Validations do
8
+
9
+ include Validations
10
+
11
+ specify "*no* error is raised if the value is included" do
12
+ expect(->{raise_on_invalid_value('foo', ['foo']) }).not_to raise_error
13
+ end
14
+
15
+ specify "an error *is* raised if the value is not included" do
16
+ expect(->{raise_on_invalid_value('foo', []) }).to raise_error
17
+ end
18
+
19
+ specify "the error message is correct" do
20
+ begin
21
+ raise_on_invalid_value('foo', [:bar, :baz], 'manufacturer')
22
+ fail "Should have raised an error"
23
+ rescue => error
24
+ expect(/manufacturer/ === error.message).to be_true
25
+ expect(/:bar/ === error.message).to be_true
26
+ expect(error.message).to eq("Invalid manufacturer 'foo'; must be one of: [:bar, :baz].")
27
+ end
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trick_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.39.0
4
+ version: 0.40.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-28 00:00:00.000000000 Z
12
+ date: 2014-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: os
@@ -141,6 +141,7 @@ files:
141
141
  - lib/trick_bag/timing/timing.rb
142
142
  - lib/trick_bag/validations/hash_validations.rb
143
143
  - lib/trick_bag/validations/object_validations.rb
144
+ - lib/trick_bag/validations/other_validations.rb
144
145
  - lib/trick_bag/version.rb
145
146
  - spec/spec_helper.rb
146
147
  - spec/trick_bag/collections/linked_list_spec.rb
@@ -162,6 +163,7 @@ files:
162
163
  - spec/trick_bag/timing/timing_spec.rb
163
164
  - spec/trick_bag/validations/hashes_validations_spec.rb
164
165
  - spec/trick_bag/validations/object_validations_spec.rb
166
+ - spec/trick_bag/validations/other_validations_spec.rb
165
167
  - trick_bag.gemspec
166
168
  homepage: https://github.com/keithrbennett/trick_bag
167
169
  licenses:
@@ -209,4 +211,5 @@ test_files:
209
211
  - spec/trick_bag/timing/timing_spec.rb
210
212
  - spec/trick_bag/validations/hashes_validations_spec.rb
211
213
  - spec/trick_bag/validations/object_validations_spec.rb
214
+ - spec/trick_bag/validations/other_validations_spec.rb
212
215
  has_rdoc: