Class: QueryFilter::Utils::DatePeriod
- Inherits:
-
Object
- Object
- QueryFilter::Utils::DatePeriod
- Defined in:
- lib/query_filter/utils/date_period.rb
Constant Summary collapse
- TIME_FORMAT =
'%Y-%m-%d %H:%M:%S'
Instance Attribute Summary collapse
-
#date_from_raw ⇒ Object
readonly
Returns the value of attribute date_from_raw.
-
#date_to_raw ⇒ Object
readonly
Returns the value of attribute date_to_raw.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #date_from ⇒ Object
- #date_from_original ⇒ Object
- #date_to ⇒ Object
- #date_to_original ⇒ Object
- #datefrom ⇒ Object
- #dateto ⇒ Object
- #default? ⇒ Boolean
-
#initialize(date_from = nil, date_to = nil, format = nil) ⇒ DatePeriod
constructor
A new instance of DatePeriod.
- #range ⇒ Object
- #range_original ⇒ Object
- #title ⇒ Object
- #to_human ⇒ Object
- #to_param ⇒ Object
Constructor Details
#initialize(date_from = nil, date_to = nil, format = nil) ⇒ DatePeriod
Returns a new instance of DatePeriod.
10 11 12 13 14 |
# File 'lib/query_filter/utils/date_period.rb', line 10 def initialize(date_from = nil, date_to = nil, format = nil) @format = (format.blank? ? QueryFilter.date_period_format : format) @date_from_raw = date_from @date_to_raw = date_to end |
Instance Attribute Details
#date_from_raw ⇒ Object (readonly)
Returns the value of attribute date_from_raw.
6 7 8 |
# File 'lib/query_filter/utils/date_period.rb', line 6 def date_from_raw @date_from_raw end |
#date_to_raw ⇒ Object (readonly)
Returns the value of attribute date_to_raw.
6 7 8 |
# File 'lib/query_filter/utils/date_period.rb', line 6 def date_to_raw @date_to_raw end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
6 7 8 |
# File 'lib/query_filter/utils/date_period.rb', line 6 def format @format end |
Class Method Details
.parse_from_string(value, format = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/query_filter/utils/date_period.rb', line 71 def self.parse_from_string(value, format = nil) return value if value.is_a?(DatePeriod) if value.blank? new(nil, nil, format) else dates = value.to_s.split(QueryFilter.date_period_splitter).map(&:strip) new(dates[0], dates[1], format) end end |
Instance Method Details
#as_json(options = nil) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/query_filter/utils/date_period.rb', line 64 def as_json( = nil) { date_from: datefrom, date_to: dateto }.merge( || {}) end |
#date_from ⇒ Object
24 25 26 |
# File 'lib/query_filter/utils/date_period.rb', line 24 def date_from @date_from ||= date_from_original.beginning_of_day end |
#date_from_original ⇒ Object
32 33 34 |
# File 'lib/query_filter/utils/date_period.rb', line 32 def date_from_original @date_from_original ||= normalize_date(@date_from_raw) end |
#date_to ⇒ Object
28 29 30 |
# File 'lib/query_filter/utils/date_period.rb', line 28 def date_to @date_to ||= date_to_original.end_of_day end |
#date_to_original ⇒ Object
36 37 38 |
# File 'lib/query_filter/utils/date_period.rb', line 36 def date_to_original @date_to_original ||= normalize_date(@date_to_raw) end |
#datefrom ⇒ Object
44 45 46 |
# File 'lib/query_filter/utils/date_period.rb', line 44 def datefrom @datefrom ||= I18n.l(date_from, format: @format) end |
#dateto ⇒ Object
48 49 50 |
# File 'lib/query_filter/utils/date_period.rb', line 48 def dateto @dateto ||= I18n.l(date_to, format: @format) end |
#default? ⇒ Boolean
60 61 62 |
# File 'lib/query_filter/utils/date_period.rb', line 60 def default? @date_from_raw.blank? && @date_to_raw.blank? end |
#range ⇒ Object
16 17 18 |
# File 'lib/query_filter/utils/date_period.rb', line 16 def range @range ||= date_from..date_to end |
#range_original ⇒ Object
20 21 22 |
# File 'lib/query_filter/utils/date_period.rb', line 20 def range_original @range_original ||= date_from_original..date_to_original end |
#title ⇒ Object
40 41 42 |
# File 'lib/query_filter/utils/date_period.rb', line 40 def title @title ||= to_human end |
#to_human ⇒ Object
56 57 58 |
# File 'lib/query_filter/utils/date_period.rb', line 56 def to_human [datefrom, dateto].join(" #{QueryFilter.date_period_splitter} ") end |
#to_param ⇒ Object
52 53 54 |
# File 'lib/query_filter/utils/date_period.rb', line 52 def to_param [datefrom, dateto].join(QueryFilter.date_period_splitter) end |