module Toolchain::Validations::Helpers

Private Instance Methods

classify(value) click to toggle source

Converts the provided value to it's class name.

@param value [Symbol, String]

@return [String]

# File lib/toolchain/validations/helpers.rb, line 36
def classify(value)
  value.to_s.split("_").map(&:capitalize).join
end
each_validation(klass, method = :validations) { |v| ... } click to toggle source

Yields all defined validations from the current class, as well as all the (relavant) super-classes.

@param klass [Class] @param method [Symbol, String]

@yield [Hash]

# File lib/toolchain/validations/helpers.rb, line 23
def each_validation(klass, method = :validations)
  while ![Class, Module, Object, BasicObject, nil].include?(klass)
    klass.send(method).each { |v| yield v } if klass.respond_to?(method)
    klass = klass.superclass
  end
end
find_validator(key) click to toggle source

Finds the validator by key name.

@param key [Symbol, String]

@return [Object, nil] nil if the validator doesn't exist.

# File lib/toolchain/validations/helpers.rb, line 10
def find_validator(key)
  Toolchain::Validations::Validators.const_get(classify(key))
rescue NameError
end
inject(array, init = nil) { |memo, value, index == length - 1| ... } click to toggle source

Custom inject method that includes a 3rd yield argument that determines whether the current iteration is the last.

@param array [Array] @param init [Object] uses first value of array if nil.

@yield memo [Object], value [Object], last [Boolean]

@return [Object] The final memo result.

# File lib/toolchain/validations/helpers.rb, line 50
def inject(array, init = nil)
  memo = init || array.shift
  length = array.length
  array.each_with_index do |value, index|
    memo = yield memo, value, index == length - 1
  end
  memo
end