module TrickBag::Validations

Public Instance Methods

matches_all_regexes?(regexes, string) click to toggle source

@return whether or not the passed string matches all of the regexes in the passed array @param regexes array of regexes to test against @param string the string to test against the regexes

# 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) click to toggle source

@return whether or not the passed string matches any of the regexes in the passed array @param regexes array of regexes to test against @param string the string to test against the regexes

# 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) click to toggle source

@return whether or not the passed string matches none of the regexes in the passed array @param regexes array of regexes to test against @param string the string to test against the regexes

# 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() click to toggle source

Looks to see which keys, if any, are missing from the hash. @return an array of missing keys (empty if none)

# 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) click to toggle source

Looks to see which keys, if any, are missing from the hash. @return nil if none missing, else comma separated string of missing keys.

# 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) click to toggle source

Returns an array containing each symbol in vars for which the corresponding instance variable in the specified object is nil.

# 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) click to toggle source

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.

@param value the value to check against the valid values @param the valid values, of which the value should be one @param label a string to include in the error message after “Invalid ”, default to 'value' @param output_with_inspect if true, the values' inspect method will be called for the

error string; this will preserve the colon in symbols; if false, to_s will be used.
# 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)
    message = "Invalid #{label} '#{value}'; must be one of: [#{values_display_array.join(', ')}]."
    raise InvalidValueError.new(message)
  end
end
raise_on_missing_keys(the_hash, *keys) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

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.

@return a hash containing the :exit_status (0 = success), output (stdout + stderr), and the :process_status (Process::Status object).

# 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