Class: QueryFilter::Utils::DatePeriod
- Inherits:
-
Object
- Object
- QueryFilter::Utils::DatePeriod
- Defined in:
- lib/query_filter/utils/date_period.rb
Constant Summary collapse
- SPLITTER =
'to'.freeze
- DATE_FORMAT =
'%m/%d/%Y'.freeze
Instance Attribute Summary collapse
-
#date_from ⇒ Object
readonly
Returns the value of attribute date_from.
-
#date_to ⇒ Object
readonly
Returns the value of attribute date_to.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
Class Method Summary collapse
Instance Method Summary collapse
- #default? ⇒ Boolean
-
#initialize(date_from = nil, date_to = nil, format = nil) ⇒ DatePeriod
constructor
A new instance of DatePeriod.
- #range ⇒ Object
Constructor Details
#initialize(date_from = nil, date_to = nil, format = nil) ⇒ DatePeriod
Returns a new instance of DatePeriod.
8 9 10 11 12 13 |
# File 'lib/query_filter/utils/date_period.rb', line 8 def initialize(date_from = nil, date_to = nil, format = nil) @format = format || DATE_FORMAT @default = (date_from.blank? && date_to.blank?) @date_from = normalize_date(date_from || Time.zone.now).beginning_of_day @date_to = normalize_date(date_to || Time.zone.now).end_of_day end |
Instance Attribute Details
#date_from ⇒ Object (readonly)
Returns the value of attribute date_from.
3 4 5 |
# File 'lib/query_filter/utils/date_period.rb', line 3 def date_from @date_from end |
#date_to ⇒ Object (readonly)
Returns the value of attribute date_to.
3 4 5 |
# File 'lib/query_filter/utils/date_period.rb', line 3 def date_to @date_to end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
3 4 5 |
# File 'lib/query_filter/utils/date_period.rb', line 3 def format @format end |
Class Method Details
.parse_from_string(value, format = DATE_FORMAT) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/query_filter/utils/date_period.rb', line 23 def self.parse_from_string(value, format = DATE_FORMAT) return value if value.is_a?(DatePeriod) if value.blank? new(nil, nil, format) else dates = value.to_s.split(SPLITTER).map(&:strip) new(dates.first, dates.last, format) end end |
Instance Method Details
#default? ⇒ Boolean
19 20 21 |
# File 'lib/query_filter/utils/date_period.rb', line 19 def default? @default end |
#range ⇒ Object
15 16 17 |
# File 'lib/query_filter/utils/date_period.rb', line 15 def range @range ||= date_from..date_to end |