Class: SafeType::Date

Inherits:
Rule
  • Object
show all
Defined in:
lib/safe_type/primitive/date.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rule

#after, #before, coerce, #coerce

Constructor Details

#initialize(type: ::Date, from: nil, to: nil, **args) ⇒ Date

Returns a new instance of Date.



3
4
5
6
7
# File 'lib/safe_type/primitive/date.rb', line 3

def initialize(type: ::Date, from: nil, to: nil, **args)
  @from = from
  @to = to
  super
end

Class Method Details

.default(value = nil, from: nil, to: nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/safe_type/primitive/date.rb', line 15

def self.default(value=nil, from: nil, to: nil)
  new(
    default: value,
    from: from,
    to: to
  )
end

.strict(from: nil, to: nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/safe_type/primitive/date.rb', line 23

def self.strict(from: nil, to: nil)
  new(
    required: true,
    from: from,
    to: to
  )
end

Instance Method Details

#is_valid?(input) ⇒ Boolean

Returns:



9
10
11
12
13
# File 'lib/safe_type/primitive/date.rb', line 9

def is_valid?(input)
  return false unless @from.nil? || input >= @from
  return false unless @to.nil? || input <= @to
  super
end