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 ⇒ 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
- #as_json(options = nil) ⇒ 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
- #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 15 16 17 |
# 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 @date_from = normalize_date(date_from).beginning_of_day @date_to = normalize_date(date_to).end_of_day end |
Instance Attribute Details
#date_from ⇒ Object (readonly)
Returns the value of attribute date_from.
6 7 8 |
# File 'lib/query_filter/utils/date_period.rb', line 6 def date_from @date_from end |
#date_to ⇒ Object (readonly)
Returns the value of attribute date_to.
6 7 8 |
# File 'lib/query_filter/utils/date_period.rb', line 6 def date_to @date_to 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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/query_filter/utils/date_period.rb', line 54 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
47 48 49 50 51 52 |
# File 'lib/query_filter/utils/date_period.rb', line 47 def as_json( = nil) { date_from: datefrom, date_to: dateto }.merge( || {}) end |
#datefrom ⇒ Object
27 28 29 |
# File 'lib/query_filter/utils/date_period.rb', line 27 def datefrom @datefrom ||= I18n.l(@date_from, format: @format) end |
#dateto ⇒ Object
31 32 33 |
# File 'lib/query_filter/utils/date_period.rb', line 31 def dateto @dateto ||= I18n.l(@date_to, format: @format) end |
#default? ⇒ Boolean
43 44 45 |
# File 'lib/query_filter/utils/date_period.rb', line 43 def default? @date_from_raw.blank? && @date_to_raw.blank? end |
#range ⇒ Object
19 20 21 |
# File 'lib/query_filter/utils/date_period.rb', line 19 def range @range ||= date_from..date_to end |
#title ⇒ Object
23 24 25 |
# File 'lib/query_filter/utils/date_period.rb', line 23 def title @title ||= to_human end |
#to_human ⇒ Object
39 40 41 |
# File 'lib/query_filter/utils/date_period.rb', line 39 def to_human [datefrom, dateto].join(" #{QueryFilter.date_period_splitter} ") end |
#to_param ⇒ Object
35 36 37 |
# File 'lib/query_filter/utils/date_period.rb', line 35 def to_param [datefrom, dateto].join(QueryFilter.date_period_splitter) end |