Module: BarkestCore::DateParser
- Included in:
- LogViewOptions, MsSqlFunction
- Defined in:
- lib/barkest_core/concerns/date_parser.rb
Overview
This module will add consistent date parsing functions to a class.
Constant Summary collapse
- DATE_FORMAT =
A simple hash that can be used to validate a date entry column.
validates :my_date, :format => DATE_FORMAT
{ with: DATE_REGEX, multiline: true, message: 'must be in MM/DD/YYYY or YYYY-MM-DD format' }
- NULLABLE_DATE_FORMAT =
A simple hash that can be used to validate a nullable date entry column.
validates :my_date, :format => NULLABLE_DATE_FORMAT
{ with: NULLABLE_DATE_REGEX, multiline: true, message: 'must be in MM/DD/YYYY or YYYY-MM-DD format' }
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
-
.parse_for_date_column(value) ⇒ Object
Parses a value for storage in a date/datetime column.
-
.parse_for_date_filter(value) ⇒ Object
Parses a value for use in a SQL query.
-
.parse_for_time_column(value) ⇒ Object
Parses a value for storage in a datetime column.
-
.parse_for_time_filter(value) ⇒ Object
Parses a value for use in a SQL query.
Class Method Details
.included(base) ⇒ Object
:nodoc:
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/barkest_core/concerns/date_parser.rb', line 87 def self.included(base) base.class_eval do ## # Parses a value for storage in a date/datetime column. # # Value should be a string in 'M/D/YYYY' or 'YYYY-MM-DD' format but can also be a Date or Time object. # # Returns a Time object or nil if value is invalid. # def self.parse_for_date_column(value) BarkestCore::DateParser.parse_for_date_column value end ## # Parses a value for storage in a datetime column. # # Value should be a string in 'M/D/YYYY HH:MM:SS' or 'YYYY-MM-DD HH:MM:SS' format, but can also be a Date or Time object. # # Returns a Time object or nil if value is invalid. # def self.parse_for_time_column(value) BarkestCore::DateParser.parse_for_time_column value end ## # Parses a value for use in a SQL query. # # Returns NULL if the parsed date is nil. # Otherwise returns the date in 'YYYY-MM-DD' format. # def self.parse_for_date_filter(value) BarkestCore::DateParser.parse_for_date_filter value end ## # Parses a value for use in a SQL query. # # Returns NULL if the parsed time is nil. # Otherwise returns the time in 'YYYY-MM-DD HH:MM:SS' format. # def self.parse_for_time_filter(value) BarkestCore::DateParser.parse_for_time_filter value end end end |
.parse_for_date_column(value) ⇒ Object
Parses a value for storage in a date/datetime column.
Value should be a string in ‘M/D/YYYY’ or ‘YYYY-MM-DD’ format but can also be a Date or Time object.
Returns a Time object or nil if value is invalid.
47 48 49 |
# File 'lib/barkest_core/concerns/date_parser.rb', line 47 def self.parse_for_date_column(value) Time.utc_parse(value).date rescue nil end |
.parse_for_date_filter(value) ⇒ Object
Parses a value for use in a SQL query.
Returns NULL if the parsed date is nil. Otherwise returns the date in ‘YYYY-MM-DD’ format.
68 69 70 71 72 |
# File 'lib/barkest_core/concerns/date_parser.rb', line 68 def self.parse_for_date_filter(value) value = parse_for_date_column(value) return 'NULL' unless value value.strftime('\'%Y-%m-%d\'') end |
.parse_for_time_column(value) ⇒ Object
Parses a value for storage in a datetime column.
Value should be a string in ‘M/D/YYYY HH:MM:SS’ or ‘YYYY-MM-DD HH:MM:SS’ format, but can also be a Date or Time object.
Returns a Time object or nil if value is invalid.
58 59 60 |
# File 'lib/barkest_core/concerns/date_parser.rb', line 58 def self.parse_for_time_column(value) Time.utc_parse(value) rescue nil end |
.parse_for_time_filter(value) ⇒ Object
Parses a value for use in a SQL query.
Returns NULL if the parsed time is nil. Otherwise returns the time in ‘YYYY-MM-DD HH:MM:SS’ format.
80 81 82 83 84 |
# File 'lib/barkest_core/concerns/date_parser.rb', line 80 def self.parse_for_time_filter(value) value = parse_for_time_column(value) return 'NULL' unless value value.strftime('\'%Y-%m-%d %H:%M:%S\'') end |