Class: QueryFilter::Utils::DatePeriod

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

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%d %H:%M:%S'

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.



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

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

#datefromObject



27
28
29
# File 'lib/query_filter/utils/date_period.rb', line 27

def datefrom
  @datefrom ||= I18n.l(@date_from, format: @format)
end

#datetoObject



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

Returns:

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

#rangeObject



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

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

#titleObject



23
24
25
# File 'lib/query_filter/utils/date_period.rb', line 23

def title
  @title ||= to_human
end

#to_humanObject



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_paramObject



35
36
37
# File 'lib/query_filter/utils/date_period.rb', line 35

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