Class: Predicates::Time

Inherits:
Base
  • Object
show all
Defined in:
lib/predicates/time.rb

Overview

Describes a field as a Time field. Currently this means that it has both a time and a date.

Options

  • :before [time] - specifies that the value must predate the given time (as object or string)

  • :after [time] - specifies that the value must postdate the given time (as object or string)

  • :distance [range] - specifies a range of seconds that provide an minimum and maximum time value to be calculated from the current time.

Instance Attribute Summary collapse

Attributes inherited from Base

#full_message, #or_empty, #validate_if, #validate_on

Instance Method Summary collapse

Methods inherited from Base

#allow_empty?, #error, #error_binds, #initialize, #normalize, #to_human

Constructor Details

This class inherits a constructor from Predicates::Base

Instance Attribute Details

#afterObject

Returns the value of attribute after.



18
19
20
# File 'lib/predicates/time.rb', line 18

def after
  @after
end

#beforeObject

Returns the value of attribute before.



12
13
14
# File 'lib/predicates/time.rb', line 12

def before
  @before
end

#distanceObject

specifies a range of seconds where the lower boundary is the minimum time value and the upper boundary is the maximum time value, both interpreted from the current time.



24
25
26
# File 'lib/predicates/time.rb', line 24

def distance
  @distance
end

Instance Method Details

#error_messageObject



26
27
28
# File 'lib/predicates/time.rb', line 26

def error_message
  @error_message || :time
end

#validate(value, record) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/predicates/time.rb', line 30

def validate(value, record)
  valid = value.is_a? Time
  valid &&= (value < self.before) if self.before
  valid &&= (value > self.after) if self.after
  
  if self.distance
    now = Time.zone.now
    valid &&= (value >= now + self.distance.begin)
    valid &&= (value <= now + self.distance.end)
  end

  valid
end