class Mv::Core::Validation::Length

Attributes

in[R]
is[R]
maximum[R]
minimum[R]
too_long[R]
too_short[R]
within[R]

Public Class Methods

new(table_name, column_name, opts) click to toggle source
Calls superclass method Mv::Core::Validation::Base::new
# File lib/mv/core/validation/length.rb, line 18
def initialize(table_name, column_name, opts)
  unless opts.is_a?(Hash)
    opts = { in: opts } if opts.respond_to?(:to_a)
    opts = { is: opts } if opts.is_a?(Integer)
  end

  super(table_name, column_name, opts)

  opts.with_indifferent_access.tap do |opts|
    @in = opts[:in]
    @within = opts[:within]
    @is = opts[:is]
    @maximum = opts[:maximum]
    @minimum = opts[:minimum]
    @too_long = opts[:too_long] || default_too_long_message
    @too_short = opts[:too_short] || default_too_short_message
  end
end

Public Instance Methods

full_too_long() click to toggle source
# File lib/mv/core/validation/length.rb, line 41
def full_too_long
  too_long ? compose_full_message(too_long) : nil
end
full_too_short() click to toggle source
# File lib/mv/core/validation/length.rb, line 37
def full_too_short
  too_short ? compose_full_message(too_short) : nil
end

Protected Instance Methods

default_message() click to toggle source
# File lib/mv/core/validation/length.rb, line 51
def default_message
  'is the wrong length'
end
default_too_long_message() click to toggle source
# File lib/mv/core/validation/length.rb, line 59
def default_too_long_message
  maximum ? 'is too long' : nil
end
default_too_short_message() click to toggle source
# File lib/mv/core/validation/length.rb, line 55
def default_too_short_message
  minimum ? 'is too short' : nil
end
to_a() click to toggle source
Calls superclass method Mv::Core::Validation::Base#to_a
# File lib/mv/core/validation/length.rb, line 47
def to_a
  super + [self.in.try(:sort), within.try(:sort), is.to_s, maximum.to_s, minimum.to_s, too_short.to_s, too_long.to_s]
end

Private Instance Methods

in_within_is_maximum_minimum_allowance() click to toggle source
# File lib/mv/core/validation/length.rb, line 65
def in_within_is_maximum_minimum_allowance
  not_null_attrs = [[is, :is],
                    [within, :within],
                    [self.in, :in],
                    [maximum || minimum, :minimum_or_maximum]]
                  .select(&:first).collect(&:second)

  if not_null_attrs.length != 1
    not_null_attrs << :is if not_null_attrs.blank?

    not_null_attrs.each do |attr|
      errors.add(
        attr,
        'One and only one attribute from the list [:is, :within, :in, [:minimum, :maximum] can be defined'
      )
    end
  end
end