Class: QueryFilter::Utils::DatePeriod

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_fromObject (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_toObject (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

#formatObject (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

Returns:

  • (Boolean)


19
20
21
# File 'lib/query_filter/utils/date_period.rb', line 19

def default?
  @default
end

#rangeObject



15
16
17
# File 'lib/query_filter/utils/date_period.rb', line 15

def range
  @range ||= date_from..date_to
end