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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8f6306267da969808c28907cadc81162d99c42a
4
- data.tar.gz: 394741622312571d525d3e844fd90dc81d1d90a8
3
+ metadata.gz: 2c99dc8b650dac023740fef0aa7fc165fb773b5d
4
+ data.tar.gz: f5af8de870c9ff948ad80622131da86608456202
5
5
  SHA512:
6
- metadata.gz: 800d3c947f7adf3254f23bdf35ca6f3efdf6c3c0f6734ed6306e23f5852e54a4ed4dea0dba9e19c6be094a49d9f93f7ca22db243f8554df287e7a22ab996e83f
7
- data.tar.gz: 2814d446200f12db2673a8b2329d353b902fef2ec101809d71d4f9708d77e69733cce40015290b0f80cc9b00b82e7593e44278140ae088c3649796323b284f5b
6
+ metadata.gz: 969c9e634b2262a6e7ce42207e3c55d29d1d9aabbb29a8eacfcebbf17589a98a8969acd74832d50d17709f025766d2e4dd3454792cfa5bf0cdd87b5aae8f2d62
7
+ data.tar.gz: f88ea61c1a8e19a8e31f5eff6a8c1c3f356c8d95c5bd617a4c7dca954612590e581e46bcc0bd159881133fbe0186f98994dba1529f13bd6d901d8ac89d6b5465
data/.travis.yml CHANGED
@@ -1,5 +1,9 @@
1
- sudo: false
1
+ before_install: gem install bundler
2
+ cache: bundler
2
3
  language: ruby
4
+ script: bundle exec rspec
5
+ sudo: false
3
6
  rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.12.4
7
+ - 2.0.0
8
+ - 2.1.0
9
+ - 2.2.0
data/README.md CHANGED
@@ -30,10 +30,24 @@ end
30
30
  # => "1 gem"
31
31
  # => "2 gems"
32
32
 
33
- # alias: Numeric#to_s_with_unit
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,6 @@
1
+ class Integer
2
+ def to_string_with_unit(singular_unit, plural_unit = nil)
3
+ StringWithUnit::Value.new(self, singular_unit, plural_unit).to_s
4
+ end
5
+ alias_method :to_s_with_unit, :to_string_with_unit
6
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module StringWithUnit
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,2 +1,3 @@
1
+ require "string_with_unit/value"
1
2
  require "string_with_unit/version"
2
- require "string_with_unit/core_ext/numeric"
3
+ require "string_with_unit/core_ext/integer"
@@ -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 Numeric#to_string_with_unit to express a number with singular/plural unit"
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.1.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
- description: Add Numeric#to_string_with_unit to express a number with singular/plural
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/numeric.rb
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 Numeric#to_string_with_unit to express a number with singular/plural
114
+ summary: Add Integer#to_string_with_unit to express a number with singular/plural
100
115
  unit
101
116
  test_files: []
@@ -1,7 +0,0 @@
1
- class Numeric
2
- def to_string_with_unit(singular_unit, plural_unit)
3
- unit = (self.abs == 1) ? singular_unit : plural_unit
4
- "#{self} #{unit}"
5
- end
6
- alias_method :to_s_with_unit, :to_string_with_unit
7
- end