Class: DateRangeFormatter::Formatter
- Inherits:
-
Object
- Object
- DateRangeFormatter::Formatter
- Defined in:
- lib/date_range_formatter.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#from_date ⇒ Object
readonly
Returns the value of attribute from_date.
-
#until_date ⇒ Object
readonly
Returns the value of attribute until_date.
Instance Method Summary collapse
-
#initialize(from_date, until_date, format) ⇒ Formatter
constructor
A new instance of Formatter.
- #same_days? ⇒ Boolean
- #same_months? ⇒ Boolean
- #same_years? ⇒ Boolean
- #to_s ⇒ Object
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
25 26 27 |
# File 'lib/date_range_formatter.rb', line 25 def format @format end |
#from_date ⇒ Object (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_date ⇒ Object (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
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
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
52 53 54 |
# File 'lib/date_range_formatter.rb', line 52 def same_years? from_date.year == until_date.year end |
#to_s ⇒ Object
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 |