Module: Toolchain::Validations::Helpers
Instance Method Summary collapse
-
#classify(value) ⇒ String
Converts the provided value to it’s class name.
-
#each_validation(klass, method = :validations) {|Hash| ... } ⇒ Object
Yields all defined validations from the current class, as well as all the (relavant) super-classes.
-
#find_validator(key) ⇒ Object?
Finds the validator by key name.
-
#inject(array, init = nil) ⇒ Object
Custom inject method that includes a 3rd yield argument that determines whether the current iteration is the last.
Instance Method Details
#classify(value) ⇒ String
Converts the provided value to it’s class name.
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.
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.
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.
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 |