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)


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

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

#same_hours?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/date_range_formatter.rb', line 41

def same_hours?
  same_days? && from_date.hour == until_date.hour
end

#same_months?Boolean

Returns:

  • (Boolean)


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

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

#same_years?Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

def to_s
  return I18n.t 'same_hours', same_hours_data.merge(scope: ['date_range', format]) if same_hours?
  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?
  I18n.t 'different_components', different_components_data.merge(scope: ['date_range', format])
end