Module: Aequitas::Rule::Format

Included in:
Proc, Regexp
Defined in:
lib/aequitas/rule/format.rb,
lib/aequitas/rule/format/proc.rb,
lib/aequitas/rule/format/regexp.rb

Defined Under Namespace

Classes: Proc, Regexp

Constant Summary collapse

FORMATS =
{
  :email_address => Formats::EmailAddress,
  :url           => Formats::Url
}
FORMAT_MESSAGES =

TODO: evaluate re-implementing custom error messages per format type previously these strings were wrapped in lambdas, which were, at one point, invoked with #try_call with the humanized attribute name and value

{
  :email_address => '%s is not a valid email address',
  :url           => '%s is not a valid URL',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



52
53
54
# File 'lib/aequitas/rule/format.rb', line 52

def format
  @format
end

Class Method Details

.new(attribute_name, options) ⇒ Object

Raises:

  • (UnknownValidationFormat)

    if the :as (or :with) option is a Symbol that is not a key in FORMATS, or if the provided format is not a Regexp, Symbol or Proc



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aequitas/rule/format.rb', line 33

def self.new(attribute_name, options)
  format = options.delete(:as) || options.delete(:with)

  case format
  when Symbol
    regexp = FORMATS.fetch(format) do
      raise UnknownValidationFormat, "No such predefined format '#{format}'"
    end
    self::Regexp.new(attribute_name, options.merge(:format => regexp, :format_name => format))
  when ::Regexp
    self::Regexp.new(attribute_name, options.merge(:format => format))
  when ::Proc
    self::Proc.new(attribute_name, options.merge(:format => format))
  else
    raise UnknownValidationFormat, "Expected a Regexp, Symbol, or Proc format. Got: #{format.inspect}"
  end
end

.rules_for(attribute_name, options) ⇒ Object



26
27
28
# File 'lib/aequitas/rule/format.rb', line 26

def self.rules_for(attribute_name, options)
  Array(new(attribute_name, options))
end

Instance Method Details

#initialize(attribute_name, options) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/aequitas/rule/format.rb', line 54

def initialize(attribute_name, options)
  super

  @format = options.fetch(:format)

  skip_condition.default_to_allowing_nil!
  skip_condition.default_to_allowing_blank!
end

#violation_type(resource) ⇒ Object



63
64
65
# File 'lib/aequitas/rule/format.rb', line 63

def violation_type(resource)
  :invalid
end