module Whowas::Validatable

Public Instance Methods

validate(input) click to toggle source

Checks for required inputs and input formats that the adapter will need to process the search.

It does not matter if there are other, non-required parameters in the input hash; they will be ignored later.

# File lib/whowas/validatable.rb, line 8
def validate(input)
  (check_exists(required_inputs, input) &&
    check_format(input_formats, input)) || 
  (raise Errors::InvalidInput, "Invalid input for #{self.class.name}")
end

Private Instance Methods

check_exists(required, input) click to toggle source

Required keys must exist in the input hash and must have a non-nil, non-empty value.

# File lib/whowas/validatable.rb, line 29
def check_exists(required, input)
  required.inject(true) do |result, key|
    input[key] && result
  end
end
check_format(formats, input) click to toggle source

Format is a lambda defined in the search method class which returns true or false. This allows for flexible format checking:

e.g. regex for ip addresses and DateTime.parse calls for timestamps
# File lib/whowas/validatable.rb, line 39
def check_format(formats, input)
  formats.inject(true) do |result, (key, format)|
    format.call(input[key]) && result
  end
end
input_formats() click to toggle source
# File lib/whowas/validatable.rb, line 23
def input_formats
  {}
end
required_inputs() click to toggle source

hooks for required_inputs and input_formats must be set by including class

# File lib/whowas/validatable.rb, line 19
def required_inputs
  []
end