Class: MiniDefender::Rules::DateFormat

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/date_format.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(format) ⇒ DateFormat

Returns a new instance of DateFormat.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/mini_defender/rules/date_format.rb', line 6

def initialize(format)
  raise ArgumentError, 'Format must be a string' unless format.is_a?(String)
  raise ArgumentError, 'Format is required for this rule' if format.empty?

  @format = format
end

Class Method Details

.make(args) ⇒ Object



17
18
19
# File 'lib/mini_defender/rules/date_format.rb', line 17

def self.make(args)
  self.new(args[0])
end

.signatureObject



13
14
15
# File 'lib/mini_defender/rules/date_format.rb', line 13

def self.signature
  'date_format'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



27
28
29
# File 'lib/mini_defender/rules/date_format.rb', line 27

def message(attribute, value, validator)
  'The given value is not a valid date.'
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



21
22
23
24
25
# File 'lib/mini_defender/rules/date_format.rb', line 21

def passes?(attribute, value, validator)
  parse_date(value).is_a?(Time)
rescue ArgumentError
  false
end