time_compact 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +66 -0
- data/.rubocop.yml +66 -0
- data/README.md +30 -0
- data/lib/time_compact.rb +25 -2
- data/lib/time_compact/version.rb +1 -1
- data/locale/en.yml +6 -0
- data/locale/ja.yml +6 -0
- data/test/test_time_compact.rb +91 -0
- data/time_compact.gemspec +11 -11
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d07aa8f198853c7de9c5b06bc61a0d7dbcf5ba5
|
4
|
+
data.tar.gz: a573d4b81b7e331df8c2f18e6baca67526f04ecd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dbb595242a53742bb510e9908f84d38de9b7b653f8d661419fccbc959872f73dd957f7a4e814fb3bf7a0bd2c0d58b75a88aa8c34d4d872cba0db3b41c39c62c
|
7
|
+
data.tar.gz: a6a7861bb73c604cc23616d586f7b3d608564a7b707e730e6c3a6702dbee9f86d20d3646a5d455f141ec4bdf69afe03ff3facec49641a8747d9acf89c9aa5105
|
data/.hound.yml
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# tolerant of https://github.com/airbnb/ruby styles
|
2
|
+
LineLength:
|
3
|
+
Max: 128
|
4
|
+
|
5
|
+
MethodLength:
|
6
|
+
Max: 30
|
7
|
+
|
8
|
+
NumericLiterals:
|
9
|
+
MinDigits: 12
|
10
|
+
|
11
|
+
ClassAndModuleChildren:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
DotPosition:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
EmptyLinesAroundAccessModifier:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
HandleExceptions:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
IfUnlessModifier:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
IndentHash:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
NegatedIf:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
PercentLiteralDelimiters:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
RedundantSelf:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Semicolon:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
TrailingComma:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
UselessAssignment:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/StringLiterals:
|
51
|
+
EnforcedStyle: single_quotes
|
52
|
+
|
53
|
+
Style/AlignParameters:
|
54
|
+
EnforcedStyle: with_fixed_indentation
|
55
|
+
|
56
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
57
|
+
EnforcedStyle: space
|
58
|
+
|
59
|
+
Style/SpaceBeforeBlockBraces:
|
60
|
+
EnforcedStyle: space
|
61
|
+
|
62
|
+
Style/Lambda:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
Style/GuardClause:
|
66
|
+
Enabled: false
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# tolerant of https://github.com/airbnb/ruby styles
|
2
|
+
LineLength:
|
3
|
+
Max: 128
|
4
|
+
|
5
|
+
MethodLength:
|
6
|
+
Max: 30
|
7
|
+
|
8
|
+
NumericLiterals:
|
9
|
+
MinDigits: 12
|
10
|
+
|
11
|
+
ClassAndModuleChildren:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
DotPosition:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
EmptyLinesAroundAccessModifier:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
HandleExceptions:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
IfUnlessModifier:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
IndentHash:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
NegatedIf:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
PercentLiteralDelimiters:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
RedundantSelf:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Semicolon:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
TrailingComma:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
UselessAssignment:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/StringLiterals:
|
51
|
+
EnforcedStyle: single_quotes
|
52
|
+
|
53
|
+
Style/AlignParameters:
|
54
|
+
EnforcedStyle: with_fixed_indentation
|
55
|
+
|
56
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
57
|
+
EnforcedStyle: space
|
58
|
+
|
59
|
+
Style/SpaceBeforeBlockBraces:
|
60
|
+
EnforcedStyle: space
|
61
|
+
|
62
|
+
Style/Lambda:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
Style/GuardClause:
|
66
|
+
Enabled: false
|
data/README.md
CHANGED
@@ -61,6 +61,36 @@ You can customize the format.
|
|
61
61
|
same_hour: '%1M分'
|
62
62
|
other: '%Y年%1m月%1d日'
|
63
63
|
|
64
|
+
### Options
|
65
|
+
|
66
|
+
`time_compact` method takes `options` on the last args.
|
67
|
+
|
68
|
+
#### `i18n_key_prefix`
|
69
|
+
|
70
|
+
Prefix to i18n keys. Usage:
|
71
|
+
|
72
|
+
```
|
73
|
+
# config/locales/en.yml:
|
74
|
+
en:
|
75
|
+
time_compact:
|
76
|
+
same_year: '%1m/%1d'
|
77
|
+
same_month: '%1m/%1d'
|
78
|
+
same_day: '%1H:%M'
|
79
|
+
same_hour: '%1M min'
|
80
|
+
other: '%Y/%1m/%1d'
|
81
|
+
verbose: # <- Set sub locales here!
|
82
|
+
same_year: '%1m/%1d %1H:%M'
|
83
|
+
same_month: '%1m/%1d %1H:%M'
|
84
|
+
same_day: '%1H:%M'
|
85
|
+
same_hour: '%1M min'
|
86
|
+
other: '%Y/%1m/%1d %1H:%M'
|
87
|
+
```
|
88
|
+
|
89
|
+
```
|
90
|
+
# in the view
|
91
|
+
<%= time_compact(@post.created_at, i18n_key_prefix: :verbose) %>
|
92
|
+
```
|
93
|
+
|
64
94
|
## Contributing
|
65
95
|
|
66
96
|
1. Fork it ( https://github.com/komagata/time_compact/fork )
|
data/lib/time_compact.rb
CHANGED
@@ -4,11 +4,17 @@ require 'i18n'
|
|
4
4
|
require 'yaml'
|
5
5
|
|
6
6
|
module TimeCompact
|
7
|
-
def time_compact(time,
|
7
|
+
def time_compact(time, *options)
|
8
|
+
now, opts = time_compact_process_optional_args(options)
|
9
|
+
|
10
|
+
opts = {
|
11
|
+
i18n_key_prefix: ''
|
12
|
+
}.merge(opts)
|
13
|
+
|
8
14
|
locale_dir = File.expand_path('../../locale', __FILE__)
|
9
15
|
I18n.enforce_available_locales = true
|
10
16
|
I18n.load_path += Dir["#{locale_dir}/*.yml"]
|
11
|
-
messages = I18n.t(
|
17
|
+
messages = I18n.t(time_compact_i18n_key(opts[:i18n_key_prefix]))
|
12
18
|
|
13
19
|
if time.year == now.year
|
14
20
|
if time.month == now.month
|
@@ -28,4 +34,21 @@ module TimeCompact
|
|
28
34
|
time.strftime(messages[:other])
|
29
35
|
end
|
30
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def time_compact_process_optional_args(args)
|
41
|
+
case args[0]
|
42
|
+
when Time, DateTime
|
43
|
+
[args[0], args[1] || {}]
|
44
|
+
when Hash
|
45
|
+
[Time.now, args[0]]
|
46
|
+
else
|
47
|
+
[Time.now, {}]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def time_compact_i18n_key(prefix = '')
|
52
|
+
['time_compact', prefix.to_s].reject(&:empty?).join('.')
|
53
|
+
end
|
31
54
|
end
|
data/lib/time_compact/version.rb
CHANGED
data/locale/en.yml
CHANGED
data/locale/ja.yml
CHANGED
data/test/test_time_compact.rb
CHANGED
@@ -35,6 +35,35 @@ class TestTimeCompact < MiniTest::Test
|
|
35
35
|
), 'same hour'
|
36
36
|
end
|
37
37
|
|
38
|
+
def test_time_compact_en_verbose
|
39
|
+
I18n.locale = 'en'
|
40
|
+
assert_equal '2014/1/1 0:00', @object.time_compact(
|
41
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
42
|
+
Time.new(2015, 1, 1, 0, 0, 0),
|
43
|
+
i18n_key_prefix: :verbose
|
44
|
+
), 'defferent year'
|
45
|
+
assert_equal '1/1 0:00', @object.time_compact(
|
46
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
47
|
+
Time.new(2014, 2, 1, 0, 0, 0),
|
48
|
+
i18n_key_prefix: :verbose
|
49
|
+
), 'same year'
|
50
|
+
assert_equal '1/1 0:00', @object.time_compact(
|
51
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
52
|
+
Time.new(2014, 1, 2, 0, 0, 0),
|
53
|
+
i18n_key_prefix: :verbose
|
54
|
+
), 'same month'
|
55
|
+
assert_equal '0:00', @object.time_compact(
|
56
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
57
|
+
Time.new(2014, 1, 1, 1, 0, 0),
|
58
|
+
i18n_key_prefix: :verbose
|
59
|
+
), 'same day'
|
60
|
+
assert_equal '0 min', @object.time_compact(
|
61
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
62
|
+
Time.new(2014, 1, 1, 0, 1, 0),
|
63
|
+
i18n_key_prefix: :verbose
|
64
|
+
), 'same hour'
|
65
|
+
end
|
66
|
+
|
38
67
|
def test_time_compact_ja
|
39
68
|
I18n.locale = 'ja'
|
40
69
|
assert_equal '2014年1月1日', @object.time_compact(
|
@@ -58,4 +87,66 @@ class TestTimeCompact < MiniTest::Test
|
|
58
87
|
Time.new(2014, 1, 1, 0, 1, 0)
|
59
88
|
), 'same hour'
|
60
89
|
end
|
90
|
+
|
91
|
+
def test_time_compact_ja_verbose
|
92
|
+
I18n.locale = 'ja'
|
93
|
+
assert_equal '2014年1月1日 0時0分', @object.time_compact(
|
94
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
95
|
+
Time.new(2015, 1, 1, 0, 0, 0),
|
96
|
+
i18n_key_prefix: 'verbose'
|
97
|
+
), 'defferent year'
|
98
|
+
assert_equal '1月1日 0時0分', @object.time_compact(
|
99
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
100
|
+
Time.new(2014, 2, 1, 0, 0, 0),
|
101
|
+
i18n_key_prefix: 'verbose'
|
102
|
+
), 'same year'
|
103
|
+
assert_equal '1日 0時0分', @object.time_compact(
|
104
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
105
|
+
Time.new(2014, 1, 2, 0, 0, 0),
|
106
|
+
i18n_key_prefix: 'verbose'
|
107
|
+
), 'same month'
|
108
|
+
assert_equal '0時0分', @object.time_compact(
|
109
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
110
|
+
Time.new(2014, 1, 1, 1, 0, 0),
|
111
|
+
i18n_key_prefix: 'verbose'
|
112
|
+
), 'same day'
|
113
|
+
assert_equal '0分', @object.time_compact(
|
114
|
+
Time.new(2014, 1, 1, 0, 0, 0),
|
115
|
+
Time.new(2014, 1, 1, 0, 1, 0),
|
116
|
+
i18n_key_prefix: 'verbose'
|
117
|
+
), 'same hour'
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_time_compact_process_optional_args
|
121
|
+
time = Time.new(2014, 1, 1, 0, 0, 0)
|
122
|
+
|
123
|
+
Time.stub(:now, time) do
|
124
|
+
assert_equal [time, {}],
|
125
|
+
@object.send(:time_compact_process_optional_args, []),
|
126
|
+
'no optional args'
|
127
|
+
|
128
|
+
assert_equal [time, { awesome_option: 'yaay!' }],
|
129
|
+
@object.send(:time_compact_process_optional_args,
|
130
|
+
[{ awesome_option: 'yaay!' }]),
|
131
|
+
'only options hash is passed'
|
132
|
+
end
|
133
|
+
|
134
|
+
assert_equal [time, {}],
|
135
|
+
@object.send(:time_compact_process_optional_args, [time]),
|
136
|
+
'only time is passed'
|
137
|
+
|
138
|
+
assert_equal [time, { fantastic_option: 'woohoo!!!' }],
|
139
|
+
@object.send(:time_compact_process_optional_args,
|
140
|
+
[time, { fantastic_option: 'woohoo!!!' }]),
|
141
|
+
'time and options hash are passed'
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_time_compact_i18n_key
|
145
|
+
assert_equal 'time_compact',
|
146
|
+
@object.send(:time_compact_i18n_key),
|
147
|
+
'no prefix'
|
148
|
+
assert_equal 'time_compact.prefix',
|
149
|
+
@object.send(:time_compact_i18n_key, 'prefix'),
|
150
|
+
'with prefix'
|
151
|
+
end
|
61
152
|
end
|
data/time_compact.gemspec
CHANGED
@@ -4,23 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'time_compact/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'time_compact'
|
8
8
|
spec.version = TimeCompact::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
9
|
+
spec.authors = ['Masaki Komagata']
|
10
|
+
spec.email = ['komagata@gmail.com']
|
11
|
+
spec.summary = 'Displays time compactly.'
|
12
|
+
spec.description = 'Displays time compactly.'
|
13
|
+
spec.homepage = 'https://github.com/komagata/time_compact'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'i18n'
|
22
22
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'minitest'
|
26
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.
|
4
|
+
version: 0.4.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-
|
11
|
+
date: 2014-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -74,6 +74,8 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".hound.yml"
|
78
|
+
- ".rubocop.yml"
|
77
79
|
- ".travis.yml"
|
78
80
|
- Gemfile
|
79
81
|
- LICENSE.txt
|
@@ -112,4 +114,3 @@ specification_version: 4
|
|
112
114
|
summary: Displays time compactly.
|
113
115
|
test_files:
|
114
116
|
- test/test_time_compact.rb
|
115
|
-
has_rdoc:
|