Module: Toolchain::Validations::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/toolchain/validations/helpers.rb

Instance Method Summary collapse

Instance Method Details

#classify(value) ⇒ String

Converts the provided value to it’s class name.

Parameters:

  • value (Symbol, String)

Returns:

  • (String)


36
37
38
# File 'lib/toolchain/validations/helpers.rb', line 36

def classify(value)
  value.to_s.split("_").map(&:capitalize).join
end

#each_validation(klass, method = :validations) {|Hash| ... } ⇒ Object

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

Parameters:

  • klass (Class)
  • method (Symbol, String) (defaults to: :validations)

Yields:

  • (Hash)


23
24
25
26
27
28
# 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) ⇒ Object?

Finds the validator by key name.

Parameters:

  • key (Symbol, String)

Returns:

  • (Object, nil)

    nil if the validator doesn’t exist.



10
11
12
13
# 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) ⇒ Object

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

Parameters:

  • array (Array)
  • init (Object) (defaults to: nil)

    uses first value of array if nil.

Returns:

  • (Object)

    The final memo result.



50
51
52
53
54
55
56
57
# 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