Class: RelativeTime::InWords

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale: :en) ⇒ InWords

Returns a new instance of InWords.



16
17
18
19
# File 'lib/relative_time/in_words.rb', line 16

def initialize(locale: :en)
  self.class.setup
  I18n.locale = locale
end

Class Method Details

.setupObject



6
7
8
9
10
11
12
13
14
# File 'lib/relative_time/in_words.rb', line 6

def self.setup
  return if @setup

  I18n.load_path << Dir[File.expand_path("#{__dir__}/../../config/locales") + '/*.yml']
  I18n.load_path << Dir[File.expand_path("#{__dir__}/../../config/locales") + '/*.rb']
  I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

  @setup = true
end

Instance Method Details

#call(date_to, date_from) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/relative_time/in_words.rb', line 21

def call(date_to, date_from)
  diff = date_from.to_time - date_to.to_time
  return I18n.t('relative.less_than_a_minute') if diff.abs.round <= 59

  date_string = resolution(diff.abs.round)
  diff >= 0 ? I18n.t('relative.ago', date_string: date_string) : I18n.t('relative.in', date_string: date_string)
end