Module: TimeCompact

Defined in:
lib/time_compact.rb,
lib/time_compact/railtie.rb,
lib/time_compact/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Instance Method Details

#time_compact(time, *options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/time_compact.rb', line 7

def time_compact(time, *options)
  now, opts = time_compact_process_optional_args(options)

  opts = {
    i18n_key_prefix: ''
  }.merge(opts)

  locale_dir = File.expand_path('../../locale', __FILE__)
  I18n.enforce_available_locales = true
  I18n.load_path += Dir["#{locale_dir}/*.yml"]
  messages = I18n.t(time_compact_i18n_key(opts[:i18n_key_prefix]))

  if time.year == now.year
    if time.month == now.month
      if time.day == now.day
        if time.hour == now.hour
          time.strftime(messages[:same_hour])
        else
          time.strftime(messages[:same_day])
        end
      else
        time.strftime(messages[:same_month])
      end
    else
      time.strftime(messages[:same_year])
    end
  else
    time.strftime(messages[:other])
  end
end