string_with_unit 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 +4 -4
- data/.travis.yml +7 -3
- data/README.md +15 -1
- data/lib/string_with_unit/core_ext/integer.rb +6 -0
- data/lib/string_with_unit/value.rb +34 -0
- data/lib/string_with_unit/version.rb +1 -1
- data/lib/string_with_unit.rb +2 -1
- data/string_with_unit.gemspec +2 -1
- metadata +19 -4
- data/lib/string_with_unit/core_ext/numeric.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c99dc8b650dac023740fef0aa7fc165fb773b5d
|
4
|
+
data.tar.gz: f5af8de870c9ff948ad80622131da86608456202
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 969c9e634b2262a6e7ce42207e3c55d29d1d9aabbb29a8eacfcebbf17589a98a8969acd74832d50d17709f025766d2e4dd3454792cfa5bf0cdd87b5aae8f2d62
|
7
|
+
data.tar.gz: f88ea61c1a8e19a8e31f5eff6a8c1c3f356c8d95c5bd617a4c7dca954612590e581e46bcc0bd159881133fbe0186f98994dba1529f13bd6d901d8ac89d6b5465
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -30,10 +30,24 @@ end
|
|
30
30
|
# => "1 gem"
|
31
31
|
# => "2 gems"
|
32
32
|
|
33
|
-
# alias:
|
33
|
+
# alias: Integer#to_s_with_unit
|
34
34
|
puts 101.to_s_with_unit('dalmatian', 'dalmatians') # => "101 dalmatians"
|
35
35
|
```
|
36
36
|
|
37
|
+
### With ActiveSupport::Inflector
|
38
|
+
|
39
|
+
```
|
40
|
+
require 'active_support/inflector'
|
41
|
+
(-2..2).each do |i|
|
42
|
+
puts i.to_string_with_unit('gem') # Specify singular unit only
|
43
|
+
end
|
44
|
+
# => "-2 gems"
|
45
|
+
# => "-1 gem"
|
46
|
+
# => "0 gems"
|
47
|
+
# => "1 gem"
|
48
|
+
# => "2 gems"
|
49
|
+
```
|
50
|
+
|
37
51
|
## Contributing
|
38
52
|
|
39
53
|
Bug reports and pull requests are welcome on GitHub at https://github.com/meganemura/string_with_unit.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module StringWithUnit
|
2
|
+
class Value
|
3
|
+
def initialize(value, singular_unit, plural_unit = nil)
|
4
|
+
@value = value
|
5
|
+
@singular_unit = singular_unit
|
6
|
+
@plural_unit = plural_unit
|
7
|
+
end
|
8
|
+
attr_reader :value, :singular_unit
|
9
|
+
|
10
|
+
FORMAT = "%s %s".freeze
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
format(FORMAT, value, unit)
|
14
|
+
end
|
15
|
+
|
16
|
+
def unit
|
17
|
+
singular_number? ? singular_unit : plural_unit
|
18
|
+
end
|
19
|
+
|
20
|
+
def singular_number?
|
21
|
+
value.abs == 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def plural_unit
|
25
|
+
if @plural_unit
|
26
|
+
@plural_unit
|
27
|
+
elsif FORMAT.respond_to?(:pluralize)
|
28
|
+
@plural_unit = singular_unit.pluralize
|
29
|
+
else
|
30
|
+
singular_unit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/string_with_unit.rb
CHANGED
data/string_with_unit.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["meganemura"]
|
10
10
|
spec.email = ["meganemura@users.noreply.github.com"]
|
11
11
|
|
12
|
-
spec.summary = "Add
|
12
|
+
spec.summary = "Add Integer#to_string_with_unit to express a number with singular/plural unit"
|
13
13
|
spec.description = spec.summary
|
14
14
|
spec.homepage = "https://github.com/meganemura/string_with_unit"
|
15
15
|
spec.license = "MIT"
|
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.12"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "activesupport"
|
25
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_with_unit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- meganemura
|
@@ -52,7 +52,21 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
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'
|
69
|
+
description: Add Integer#to_string_with_unit to express a number with singular/plural
|
56
70
|
unit
|
57
71
|
email:
|
58
72
|
- meganemura@users.noreply.github.com
|
@@ -70,7 +84,8 @@ files:
|
|
70
84
|
- bin/console
|
71
85
|
- bin/setup
|
72
86
|
- lib/string_with_unit.rb
|
73
|
-
- lib/string_with_unit/core_ext/
|
87
|
+
- lib/string_with_unit/core_ext/integer.rb
|
88
|
+
- lib/string_with_unit/value.rb
|
74
89
|
- lib/string_with_unit/version.rb
|
75
90
|
- string_with_unit.gemspec
|
76
91
|
homepage: https://github.com/meganemura/string_with_unit
|
@@ -96,6 +111,6 @@ rubyforge_project:
|
|
96
111
|
rubygems_version: 2.5.1
|
97
112
|
signing_key:
|
98
113
|
specification_version: 4
|
99
|
-
summary: Add
|
114
|
+
summary: Add Integer#to_string_with_unit to express a number with singular/plural
|
100
115
|
unit
|
101
116
|
test_files: []
|