Module: ReadMyTime::Reader

Included in:
Numeric
Defined in:
lib/read_my_time/reader.rb

Instance Method Summary collapse

Instance Method Details

#locale_prefixObject

Used to localize



28
29
30
# File 'lib/read_my_time/reader.rb', line 28

def locale_prefix
  'read_my_time'
end

#seconds_in_words(opts = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/read_my_time/reader.rb', line 4

def seconds_in_words(opts = {})
  seconds_int = self.to_i

  return '' if seconds_int <= 0

  # options by default
  opts[:skip_seconds] = false if opts[:skip_seconds].nil?

  unit_time_dividers.each_with_object([]) do |(unit_time, divider), s|

    if seconds_int > 0
      seconds_int, rest = seconds_int.divmod(divider)

      if rest.nonzero? && !(unit_time == :seconds && opts[:skip_seconds])
        word = I18n.t(unit_time.to_s, count: rest, scope: locale_prefix)
        s.unshift("#{rest} #{word}")
      end
    end
    s
  end.join(' ')

end

#unit_time_dividersObject



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

def unit_time_dividers
  {
    seconds: 60,
    minutes: 60,
    hours:   24,
    days:    365
  }
end