Class: Sanitized::DateTime

Inherits:
ActiveModel::Type::DateTime
  • Object
show all
Includes:
Base
Defined in:
lib/sanitized/date_time.rb

Overview

Description

ActiveModel::Type to cast and sanitize a model’s DateTime attribute according to the options specified on initialisation.

attribute :attr_name, Sanitized::DateTime.new(:end_of_week, :at_noon), default: Time.now

Can optionally include a custom block:

type_cast = Sanitized::DateTime.new(:beginning_of_week, :at_noon) do |value|
    ... custome code ...
end
attribute :attr_name, type_cast, default: Date.today

Class Method Summary collapse

Methods included from Base

#cast, #options

Class Method Details

.exclusive_optionsObject

Return an Array of the options that modify both the date and the time



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sanitized/date_time.rb', line 78

def self.exclusive_options
  [
    :at_beginning_of_day,
    :at_beginning_of_hour,
    :at_beginning_of_minute,
    :at_beginning_of_month,
    :at_beginning_of_quarter,
    :at_beginning_of_week,
    :at_beginning_of_year,
    :at_end_of_day,
    :at_end_of_hour,
    :at_end_of_minute,
    :at_end_of_month,
    :at_end_of_quarter,
    :at_end_of_week,
    :at_end_of_year,
    :at_midday,
    :at_middle_of_day,
    :at_midnight,
    :at_noon,
    :beginning_of_day,
    :beginning_of_hour,
    :beginning_of_minute,
    :beginning_of_month,
    :beginning_of_quarter,
    :beginning_of_week,
    :beginning_of_year,
    :end_of_month,
    :end_of_quarter,
    :end_of_week,
    :end_of_year,
    :end_of_day,
    :end_of_hour,
    :end_of_minute,
    :midday,
    :middle_of_day,
    :midnight,
    :noon
  ]
end

.valid_optionsObject

return a Hash of valid options with the other options they are exclusive with



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sanitized/date_time.rb', line 34

def self.valid_options
  {
    at_beginning_of_day:      self.exclusive_options,
    at_beginning_of_hour:     self.exclusive_options,
    at_beginning_of_minute:   self.exclusive_options,
    at_beginning_of_month:    self.exclusive_options,
    at_beginning_of_quarter:  self.exclusive_options,
    at_beginning_of_week:     self.exclusive_options,
    at_beginning_of_year:     self.exclusive_options,
    at_end_of_day:            self.exclusive_options,
    at_end_of_hour:           self.exclusive_options,
    at_end_of_minute:         self.exclusive_options,
    at_end_of_month:          self.exclusive_options,
    at_end_of_quarter:        self.exclusive_options,
    at_end_of_week:           self.exclusive_options,
    at_end_of_year:           self.exclusive_options,
    at_midday:                self.exclusive_options,
    at_middle_of_day:         self.exclusive_options,
    at_midnight:              self.exclusive_options,
    at_noon:                  self.exclusive_options,
    beginning_of_day:         self.exclusive_options,
    beginning_of_hour:        self.exclusive_options,
    beginning_of_minute:      self.exclusive_options,
    beginning_of_month:       self.exclusive_options,
    beginning_of_quarter:     self.exclusive_options,
    beginning_of_week:        self.exclusive_options,
    beginning_of_year:        self.exclusive_options,
    end_of_month:             self.exclusive_options,
    end_of_quarter:           self.exclusive_options,
    end_of_week:              self.exclusive_options,
    end_of_year:              self.exclusive_options,
    end_of_day:               self.exclusive_options,
    end_of_hour:              self.exclusive_options,
    end_of_minute:            self.exclusive_options,
    midday:                   self.exclusive_options,
    middle_of_day:            self.exclusive_options,
    midnight:                 self.exclusive_options,
    noon:                     self.exclusive_options
  }
end