Class: MiniDefender::Rules::DateEq

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

Direct Known Subclasses

DateGt, DateGte, DateLt, DateLte

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(target_date) ⇒ DateEq

Returns a new instance of DateEq.



6
7
8
9
# File 'lib/mini_defender/rules/date_eq.rb', line 6

def initialize(target_date)
  @target_date = parse_date(target_date)
  @valid_value = false
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/mini_defender/rules/date_eq.rb', line 15

def self.make(args)
  raise ArgumentError, 'Target date is required for date rules.' unless args.length == 1

  self.new(args[0])
end

.signatureObject



11
12
13
# File 'lib/mini_defender/rules/date_eq.rb', line 11

def self.signature
  'date_eq'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



30
31
32
33
34
# File 'lib/mini_defender/rules/date_eq.rb', line 30

def message(attribute, value, validator)
  return "The given value is not a valid date." unless @valid_value

  "The value must be equal to #{@target_date}."
end

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

Returns:



21
22
23
24
25
26
27
28
# File 'lib/mini_defender/rules/date_eq.rb', line 21

def passes?(attribute, value, validator)
  value = parse_date(value)
  @valid_value = true

  value == @target_date
rescue ArgumentError
  false
end