Class: QueryFilter::Utils::DatePeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/query_filter/utils/date_period.rb

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
# 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_rawObject (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_rawObject (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(options = nil)
  {
    date_from: datefrom,
    date_to: dateto
  }.merge(options || {})
end

#date_fromObject



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_originalObject



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_toObject



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_originalObject



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

#datefromObject



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

#datetoObject



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

Returns:

  • (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

#rangeObject



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

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

#range_originalObject



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

#titleObject



38
39
40
# File 'lib/query_filter/utils/date_period.rb', line 38

def title
  @title ||= to_human
end

#to_humanObject



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_paramObject



50
51
52
# File 'lib/query_filter/utils/date_period.rb', line 50

def to_param
  [datefrom, dateto].join(QueryFilter.date_period_splitter)
end