Class: QueryFilter::Utils::DatePeriod
- Inherits:
-
Object
- Object
- QueryFilter::Utils::DatePeriod
- Defined in:
- lib/query_filter/utils/date_period.rb
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.
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.
8 9 10 11 12 |
# File 'lib/query_filter/utils/date_period.rb', line 8 def initialize(date_from = nil, date_to = nil, format = nil) @date_from_raw = date_from @date_to_raw = date_to @format = format 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 |
Class Method Details
.parse_from_string(value, format = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/query_filter/utils/date_period.rb', line 69 def self.parse_from_string(value, format = nil) return value if value.is_a?(DatePeriod) if value.blank? new else dates = value.to_s.split(QueryFilter.date_period_splitter).map(&:strip) new(dates[0], dates[1], format || QueryFilter.date_period_format) end end |
Instance Method Details
#as_json(options = nil) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/query_filter/utils/date_period.rb', line 62 def as_json( = nil) { date_from: datefrom, date_to: dateto }.merge( || {}) end |
#date_from ⇒ Object
22 23 24 |
# File 'lib/query_filter/utils/date_period.rb', line 22 def date_from @date_from ||= date_from_original.beginning_of_day end |
#date_from_original ⇒ Object
30 31 32 |
# File 'lib/query_filter/utils/date_period.rb', line 30 def date_from_original @date_from_original ||= normalize_date(@date_from_raw) end |
#date_to ⇒ Object
26 27 28 |
# File 'lib/query_filter/utils/date_period.rb', line 26 def date_to @date_to ||= date_to_original.end_of_day end |
#date_to_original ⇒ Object
34 35 36 |
# File 'lib/query_filter/utils/date_period.rb', line 34 def date_to_original @date_to_original ||= normalize_date(@date_to_raw) end |
#datefrom ⇒ Object
42 43 44 |
# File 'lib/query_filter/utils/date_period.rb', line 42 def datefrom @datefrom ||= I18n.l(date_from, format: date_display_format) end |
#dateto ⇒ Object
46 47 48 |
# File 'lib/query_filter/utils/date_period.rb', line 46 def dateto @dateto ||= I18n.l(date_to, format: date_display_format) end |
#default? ⇒ Boolean
58 59 60 |
# File 'lib/query_filter/utils/date_period.rb', line 58 def default? @date_from_raw.blank? && @date_to_raw.blank? end |
#range ⇒ Object
14 15 16 |
# File 'lib/query_filter/utils/date_period.rb', line 14 def range @range ||= date_from..date_to end |
#range_original ⇒ Object
18 19 20 |
# File 'lib/query_filter/utils/date_period.rb', line 18 def range_original @range_original ||= date_from_original..date_to_original end |
#title ⇒ Object
38 39 40 |
# File 'lib/query_filter/utils/date_period.rb', line 38 def title @title ||= to_human end |
#to_human ⇒ Object
54 55 56 |
# File 'lib/query_filter/utils/date_period.rb', line 54 def to_human [datefrom, dateto].join(" #{QueryFilter.date_period_splitter} ") end |
#to_param ⇒ Object
50 51 52 |
# File 'lib/query_filter/utils/date_period.rb', line 50 def to_param [datefrom, dateto].join(QueryFilter.date_period_splitter) end |