Module: TrickBag::Validations
- Defined in:
- lib/trick_bag/validations/hash_validations.rb,
lib/trick_bag/validations/other_validations.rb,
lib/trick_bag/validations/regex_validations.rb,
lib/trick_bag/validations/object_validations.rb
Defined Under Namespace
Classes: InvalidValueError, ObjectValidationError, RegexStringListAnalyzer
Class Method Summary collapse
-
.matches_all_regexes?(regexes, string) ⇒ Boolean
Whether or not the passed string matches all of the regexes in the passed array.
-
.matches_any_regex?(regexes, string) ⇒ Boolean
Whether or not the passed string matches any of the regexes in the passed array.
-
.matches_no_regexes?(regexes, string) ⇒ Boolean
Whether or not the passed string matches none of the regexes in the passed array.
-
.missing_hash_entries(the_hash, *keys) ⇒ Object
Looks to see which keys, if any, are missing from the hash.
-
.missing_hash_entries_as_string(the_hash, *keys) ⇒ Object
Looks to see which keys, if any, are missing from the hash.
-
.nil_instance_vars(object, vars) ⇒ Object
Returns an array containing each symbol in vars for which the corresponding instance variable in the specified object is nil.
-
.raise_on_invalid_value(value, valid_values, label = 'value', output_with_inspect = true) ⇒ Object
Used to succinctly (for the caller) check to see that a value provided by the caller is included in the passed enumerable.
-
.raise_on_missing_keys(the_hash, *keys) ⇒ Object
Checks to see that all passed keys are present in the hash.
-
.raise_on_nil_instance_vars(object, vars) ⇒ Object
For each symbol in vars, checks to see that the corresponding instance variable in the specified object is not nil.
-
.test_gem_dependency_specs(gem_name) ⇒ Object
Note: This method is not supported in JRuby.
Class Method Details
.matches_all_regexes?(regexes, string) ⇒ Boolean
Returns whether or not the passed string matches all of the regexes in the passed array.
18 19 20 |
# File 'lib/trick_bag/validations/regex_validations.rb', line 18 def matches_all_regexes?(regexes, string) regexes.all? { |regex| regex === string } end |
.matches_any_regex?(regexes, string) ⇒ Boolean
Returns whether or not the passed string matches any of the regexes in the passed array.
10 11 12 |
# File 'lib/trick_bag/validations/regex_validations.rb', line 10 def matches_any_regex?(regexes, string) regexes.any? { |regex| regex === string } end |
.matches_no_regexes?(regexes, string) ⇒ Boolean
Returns whether or not the passed string matches none of the regexes in the passed array.
26 27 28 |
# File 'lib/trick_bag/validations/regex_validations.rb', line 26 def matches_no_regexes?(regexes, string) regexes.none? { |regex| regex === string } end |
.missing_hash_entries(the_hash, *keys) ⇒ Object
Looks to see which keys, if any, are missing from the hash.
8 9 10 11 12 13 14 15 |
# File 'lib/trick_bag/validations/hash_validations.rb', line 8 def missing_hash_entries(the_hash, *keys) # Support passing either an Array or multiple args: if keys.size == 1 && keys.first.is_a?(Array) keys = keys.first end keys - the_hash.keys end |
.missing_hash_entries_as_string(the_hash, *keys) ⇒ Object
Looks to see which keys, if any, are missing from the hash.
20 21 22 23 24 25 26 |
# File 'lib/trick_bag/validations/hash_validations.rb', line 20 def missing_hash_entries_as_string(the_hash, *keys) if keys.size == 1 && keys.first.is_a?(Array) keys = keys.first end missing_keys = missing_hash_entries(the_hash, *keys) missing_keys.empty? ? nil : missing_keys.inspect end |
.nil_instance_vars(object, vars) ⇒ Object
Returns an array containing each symbol in vars for which the corresponding instance variable in the specified object is nil.
11 12 13 14 |
# File 'lib/trick_bag/validations/object_validations.rb', line 11 def nil_instance_vars(object, vars) vars = Array(vars) vars.select { |var| object.instance_variable_get(var).nil? } end |
.raise_on_invalid_value(value, valid_values, label = 'value', output_with_inspect = true) ⇒ Object
Used to succinctly (for the caller) check to see that a value provided by the caller is included in the passed enumerable. Raises an error if not, e.g.:
raise_on_invalid_value(‘foo’, [:bar, :baz], ‘manufacturer’)
will raise an error with the following message:
Invalid manufacturer ‘foo’; must be one of: [:bar, :baz].
If the passed value is included in the valid values, this method returns silently.
24 25 26 27 28 29 30 31 32 |
# File 'lib/trick_bag/validations/other_validations.rb', line 24 def raise_on_invalid_value(value, valid_values, label = 'value', output_with_inspect = true) missing = ! valid_values.include?(value) if missing values_display_array = output_with_inspect ? valid_values.map(&:inspect) : valid_values.map(&:to_s) = "Invalid #{label} '#{value}'; must be one of: [#{values_display_array.join(', ')}]." raise InvalidValueError.new() end end |
.raise_on_missing_keys(the_hash, *keys) ⇒ Object
Checks to see that all passed keys are present in the hash. If not, an exception is raised, with a message string listing the missing keys.
30 31 32 33 34 35 |
# File 'lib/trick_bag/validations/hash_validations.rb', line 30 def raise_on_missing_keys(the_hash, *keys) missing_entries_string = missing_hash_entries_as_string(the_hash, keys) if missing_entries_string raise "The following required options were not provided: #{missing_entries_string}" end end |
.raise_on_nil_instance_vars(object, vars) ⇒ Object
For each symbol in vars, checks to see that the corresponding instance variable in the specified object is not nil. If any are nil, raises an error listing the nils.
19 20 21 22 23 24 |
# File 'lib/trick_bag/validations/object_validations.rb', line 19 def raise_on_nil_instance_vars(object, vars) nil_vars = nil_instance_vars(object, vars) unless nil_vars.empty? raise ObjectValidationError.new("The following instance variables were nil: #{nil_vars.join(', ')}.") end end |
.test_gem_dependency_specs(gem_name) ⇒ Object
Note: This method is not supported in JRuby.
When a gem project has a .gemspec, this uses bundle exec to verify that requiring that gem name does not result in an error. (An error would occur, for example, if a gem required by the project gem is not specified as a dependency in the .gemspec file.
and the :process_status (Process::Status object).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/trick_bag/validations/other_validations.rb', line 44 def test_gem_dependency_specs(gem_name) raise "This method not supported in JRuby" if /java/.match(RUBY_PLATFORM) command = %Q{bundle exec ruby -e "require '#{gem_name}'"} output, process_status = Open3.capture2e(command) output.prepend(command + "\n\n") { exit_status: process_status.exitstatus, output: output, process_status: process_status } end |