Class: DateRangeFormatter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/date_range_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_date, until_date, format) ⇒ Formatter

Returns a new instance of Formatter.



27
28
29
30
31
# File 'lib/date_range_formatter.rb', line 27

def initialize(from_date, until_date, format)
  @from_date = from_date
  @until_date = until_date
  @format = format.to_sym
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



25
26
27
# File 'lib/date_range_formatter.rb', line 25

def format
  @format
end

#from_dateObject (readonly)

Returns the value of attribute from_date.



25
26
27
# File 'lib/date_range_formatter.rb', line 25

def from_date
  @from_date
end

#until_dateObject (readonly)

Returns the value of attribute until_date.



25
26
27
# File 'lib/date_range_formatter.rb', line 25

def until_date
  @until_date
end

Instance Method Details

#same_days?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/date_range_formatter.rb', line 44

def same_days?
  same_months? && from_date.day == until_date.day
end

#same_months?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/date_range_formatter.rb', line 48

def same_months?
  same_years? && from_date.month == until_date.month
end

#same_years?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/date_range_formatter.rb', line 52

def same_years?
  from_date.year == until_date.year
end

#to_sObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/date_range_formatter.rb', line 33

def to_s
  return I18n.t 'same_days', same_days_data.merge(scope: ['date_range', format]) if same_days?
  return I18n.t 'same_months', same_months_data.merge(scope: ['date_range', format]) if same_months?
  return I18n.t 'same_years', same_years_data.merge(scope: ['date_range', format]) if same_years?

  from_str = I18n.l from_date, format:  format
  until_str = I18n.l until_date, format: format
  separator = I18n.t "separator", scope: ["date_range", format]
  [from_str, separator, until_str].join
end