Class: Predicates::Time
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
-
#after ⇒ Object
Returns the value of attribute after.
-
#before ⇒ Object
Returns the value of attribute before.
-
#distance ⇒ Object
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.
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
#after ⇒ Object
Returns the value of attribute after.
18 19 20 |
# File 'lib/predicates/time.rb', line 18 def after @after end |
#before ⇒ Object
Returns the value of attribute before.
12 13 14 |
# File 'lib/predicates/time.rb', line 12 def before @before end |
#distance ⇒ Object
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_message ⇒ Object
26 27 28 |
# File 'lib/predicates/time.rb', line 26 def @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 |