time_compact 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7154fa79a650c2616e1e12e290397e0fdb208ee
4
- data.tar.gz: 6b0edc90a8bac84512dfc4f4917f497392b21e19
3
+ metadata.gz: a07ad5487258701576985039433534a971cd7886
4
+ data.tar.gz: d9dfa3fbf7af561adf42b7827c2e808a40f4c0fc
5
5
  SHA512:
6
- metadata.gz: bc5126f0138f864bcd32f11d8fd18c74823b3c8c4a5508ba8c7ecb4767fa2161b17844eeeb0a86d592b3e4e09cefd1cd433c9e1d7437a00e42bfc668f3f4fecb
7
- data.tar.gz: 3aa51864ac2f9f632c6953dfade2f545755ddd045fb9b9a31e1f05ad9b27f385a6a99d9797a6119f7765308a5535444b32b389265e2c5aef20c0258930afa21f
6
+ metadata.gz: bec71f0877f0fd18352da5daa7c164ea9bef8e75c66f154cda30e6225ca51b4e400b66bdab699527aa34cc4617ced136fc619ca1837a18372df5ae2646140dcf
7
+ data.tar.gz: b42c4de394bc46f6855622d4901b1d0b7e1f454355bf749946a9ceb99bf15f3475537e6299f7813a55d31a74d86759d5e0b10f3d769b07dcd8aa243de6d72845
data/README.md CHANGED
@@ -5,9 +5,9 @@ Displays time compactly.
5
5
  for example:
6
6
 
7
7
  # default locale
8
- 2014/1/2 # when other year.
9
- 1/2 # when same year.
10
- 8:16 # when same day.
8
+ 2014/1/2 # on other year.
9
+ 1/2 # on same year.
10
+ 8:16 # on same day.
11
11
 
12
12
  # 日本語ロケール
13
13
  2014年1月2日 # 違う年の時
@@ -44,11 +44,20 @@ You can customize the format.
44
44
  # config/locales/en.yml:
45
45
  en:
46
46
  time_compact:
47
- same_year: '%{month}/%{day}'
48
- same_month: '%{month}/%{day}'
49
- same_day: '%{hour}:%{min}'
50
- same_hour: '%{min} min'
51
- other: '%{year}/%{month}/%{day}'
47
+ same_year: '%1m/%1d'
48
+ same_month: '%1m/%1d'
49
+ same_day: '%1H:%M'
50
+ same_hour: '%1M min'
51
+ other: '%Y/%1m/%1d'
52
+
53
+ # config/locales/ja.yml:
54
+ ja:
55
+ time_compact:
56
+ same_year: '%1m月%1d日'
57
+ same_month: '%1d日'
58
+ same_day: '%1H時%1M分'
59
+ same_hour: '%1M分'
60
+ other: '%Y年%1m月%1d日'
52
61
 
53
62
  ## Contributing
54
63
 
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
2
3
 
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/test_*.rb'
6
+ end
7
+
8
+ task default: :test
@@ -1,3 +1,3 @@
1
1
  module TimeCompact
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/time_compact.rb CHANGED
@@ -1,63 +1,38 @@
1
1
  require_relative 'time_compact/version'
2
2
  require_relative 'time_compact/railtie' if defined? Rails
3
3
  require 'i18n'
4
+ require 'yaml'
4
5
 
5
6
  module TimeCompact
7
+ LOCALE_DIR = File.expand_path('../../locale', __FILE__)
8
+
6
9
  def time_compact(time, now = Time.now)
7
- locale_dir = File.expand_path('../../locale', __FILE__)
8
- ::I18n.load_path += Dir[locale_dir + "/*.yml"]
10
+ I18n.enforce_available_locales = true
11
+ I18n.load_path += Dir[LOCALE_DIR + '/*.yml']
9
12
 
10
13
  if time.year == now.year
11
14
  if time.month == now.month
12
15
  if time.day == now.day
13
16
  if time.hour == now.hour
14
- ::I18n.t(
15
- 'time_compact.same_hour',
16
- year: time.year,
17
- month: time.month,
18
- day: time.day,
19
- hour: time.hour,
20
- min: time.min
21
- )
17
+ time.strftime(fetch_locale('same_hour'))
22
18
  else
23
- ::I18n.t(
24
- 'time_compact.same_day',
25
- year: time.year,
26
- month: time.month,
27
- day: time.day,
28
- hour: time.hour,
29
- min: '%02d' % time.min
30
- )
19
+ time.strftime(fetch_locale('same_day'))
31
20
  end
32
21
  else
33
- ::I18n.t(
34
- 'time_compact.same_month',
35
- year: time.year,
36
- month: time.month,
37
- day: time.day,
38
- hour: time.hour,
39
- min: '%02d' % time.min
40
- )
22
+ time.strftime(fetch_locale('same_month'))
41
23
  end
42
24
  else
43
- ::I18n.t(
44
- 'time_compact.same_year',
45
- year: time.year,
46
- month: time.month,
47
- day: time.day,
48
- hour: time.hour,
49
- min: '%02d' % time.min
50
- )
25
+ time.strftime(fetch_locale('same_year'))
51
26
  end
52
27
  else
53
- ::I18n.t(
54
- 'time_compact.other',
55
- year: time.year,
56
- month: time.month,
57
- day: time.day,
58
- hour: time.hour,
59
- min: '%02d' % time.min
60
- )
28
+ time.strftime(fetch_locale('other'))
61
29
  end
62
30
  end
31
+
32
+ private
33
+
34
+ def fetch_locale(name)
35
+ yml = YAML.load_file("#{LOCALE_DIR}/#{I18n.locale}.yml")
36
+ yml[I18n.locale.to_s]['time_compact'][name]
37
+ end
63
38
  end
data/locale/en.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  en:
2
2
  time_compact:
3
- same_year: '%{month}/%{day}'
4
- same_month: '%{month}/%{day}'
5
- same_day: '%{hour}:%{min}'
6
- same_hour: '%{min} min'
7
- other: '%{year}/%{month}/%{day}'
3
+ same_year: '%1m/%1d'
4
+ same_month: '%1m/%1d'
5
+ same_day: '%1H:%M'
6
+ same_hour: '%1M min'
7
+ other: '%Y/%1m/%1d'
data/locale/ja.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  ja:
2
2
  time_compact:
3
- same_year: '%{month}月%{day}日'
4
- same_month: '%{day}日'
5
- same_day: '%{hour}時%{min}分'
6
- same_hour: '%{min}分'
7
- other: '%{year}年%{month}月%{day}日'
3
+ same_year: '%1m月%1d日'
4
+ same_month: '%1d日'
5
+ same_day: '%1H時%1M分'
6
+ same_hour: '%1M分'
7
+ other: '%Y年%1m月%1d日'
@@ -1,9 +1,11 @@
1
- require 'minitest/unit'
2
1
  require 'minitest/autorun'
2
+ require 'minitest/unit'
3
3
  require_relative '../lib/time_compact'
4
4
 
5
- class TestTimeCompact < MiniTest::Unit::TestCase
5
+ class TestTimeCompact < MiniTest::Test
6
6
  def setup
7
+ I18n.enforce_available_locales = true
8
+ I18n.available_locales = [:en, :ja]
7
9
  @object = Object.new
8
10
  @object.extend TimeCompact
9
11
  end
@@ -13,23 +15,23 @@ class TestTimeCompact < MiniTest::Unit::TestCase
13
15
  assert_equal '2014/1/1', @object.time_compact(
14
16
  Time.new(2014, 1, 1, 0, 0, 0),
15
17
  Time.new(2015, 1, 1, 0, 0, 0)
16
- )
18
+ ), 'defferent year'
17
19
  assert_equal '1/1', @object.time_compact(
18
20
  Time.new(2014, 1, 1, 0, 0, 0),
19
21
  Time.new(2014, 2, 1, 0, 0, 0)
20
- )
22
+ ), 'same year'
21
23
  assert_equal '1/1', @object.time_compact(
22
24
  Time.new(2014, 1, 1, 0, 0, 0),
23
25
  Time.new(2014, 1, 2, 0, 0, 0)
24
- )
26
+ ), 'same month'
25
27
  assert_equal '0:00', @object.time_compact(
26
28
  Time.new(2014, 1, 1, 0, 0, 0),
27
29
  Time.new(2014, 1, 1, 1, 0, 0)
28
- )
30
+ ), 'same day'
29
31
  assert_equal '0 min', @object.time_compact(
30
32
  Time.new(2014, 1, 1, 0, 0, 0),
31
33
  Time.new(2014, 1, 1, 0, 1, 0)
32
- )
34
+ ), 'same hour'
33
35
  end
34
36
 
35
37
  def test_time_compact_ja
@@ -37,22 +39,22 @@ class TestTimeCompact < MiniTest::Unit::TestCase
37
39
  assert_equal '2014年1月1日', @object.time_compact(
38
40
  Time.new(2014, 1, 1, 0, 0, 0),
39
41
  Time.new(2015, 1, 1, 0, 0, 0)
40
- )
42
+ ), 'defferent year'
41
43
  assert_equal '1月1日', @object.time_compact(
42
44
  Time.new(2014, 1, 1, 0, 0, 0),
43
45
  Time.new(2014, 2, 1, 0, 0, 0)
44
- )
46
+ ), 'same year'
45
47
  assert_equal '1日', @object.time_compact(
46
48
  Time.new(2014, 1, 1, 0, 0, 0),
47
49
  Time.new(2014, 1, 2, 0, 0, 0)
48
- )
49
- assert_equal '0時00分', @object.time_compact(
50
+ ), 'same month'
51
+ assert_equal '0時0分', @object.time_compact(
50
52
  Time.new(2014, 1, 1, 0, 0, 0),
51
53
  Time.new(2014, 1, 1, 1, 0, 0)
52
- )
54
+ ), 'same day'
53
55
  assert_equal '0分', @object.time_compact(
54
56
  Time.new(2014, 1, 1, 0, 0, 0),
55
57
  Time.new(2014, 1, 1, 0, 1, 0)
56
- )
58
+ ), 'same hour'
57
59
  end
58
60
  end
data/time_compact.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
25
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_compact
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Komagata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-01 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Displays time compactly.
56
70
  email:
57
71
  - komagata@gmail.com