Module: CMDx::Validators::Length
Overview
Validates the ‘#length` of `value` against one of: `:within` / `:not_within` / `:in` / `:not_in` (Range), `:min` + `:max`, `:gt` / `:lt` (strict comparison), or `:is` / `:is_not` (exact match). `:gte`, `:lte`, `:eq`, `:not_eq` are accepted as aliases of `:min`, `:max`, `:is`, `:is_not` respectively (with matching `_message` overrides). Values without `#length` fail with the `:nil_message` override or a default.
Instance Method Summary collapse
Instance Method Details
#call(value, options = EMPTY_HASH) ⇒ Validators::Failure?
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cmdx/validators/length.rb', line 37 def call(value, = EMPTY_HASH) return nil_failure() unless value.respond_to?(:length) length = value.length case = .transform_keys(ALIASES) in within: within_failure(within.begin, within.end, ) unless within.cover?(length) in not_within: not_within_failure(not_within.begin, not_within.end, ) if not_within.cover?(length) in in: xin within_failure(xin.begin, xin.end, ) unless xin.cover?(length) in not_in: not_within_failure(not_in.begin, not_in.end, ) if not_in.cover?(length) in min:, max: within_failure(min, max, ) unless length.between?(min, max) in min: min_failure(min, ) unless min <= length in max: max_failure(max, ) unless length <= max in gt: gt_failure(gt, ) unless gt < length in lt: lt_failure(lt, ) unless length < lt in is: is_failure(is, ) unless length == is in is_not: is_not_failure(is_not, ) if length == is_not else raise ArgumentError, "unknown length validator options given" end end |