ruby-units 2.3.2 → 3.0.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/.github/dependabot.yml +16 -0
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/tests.yml +10 -1
- data/.ruby-version +1 -1
- data/.solargraph.yml +1 -1
- data/.tool-versions +3 -0
- data/CHANGELOG.txt +4 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +87 -67
- data/lib/ruby-units.rb +0 -1
- data/lib/ruby_units/array.rb +17 -7
- data/lib/ruby_units/cache.rb +27 -14
- data/lib/ruby_units/configuration.rb +12 -11
- data/lib/ruby_units/date.rb +46 -53
- data/lib/ruby_units/definition.rb +6 -2
- data/lib/ruby_units/math.rb +136 -113
- data/lib/ruby_units/namespaced.rb +0 -3
- data/lib/ruby_units/numeric.rb +21 -5
- data/lib/ruby_units/string.rb +34 -21
- data/lib/ruby_units/time.rb +76 -69
- data/lib/ruby_units/unit.rb +408 -377
- data/lib/ruby_units/version.rb +1 -1
- data/ruby-units.gemspec +4 -2
- metadata +40 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e423976ff1953c233f4085f282b9431d9dc8f7fa840403221eb42d363384809
|
4
|
+
data.tar.gz: 22c656c48317d6af891eb3e3e1fbfcecc48fd113a9fff76004c829417b4ed305
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '096f48bed8904c0e450151e1df844493826b665ffaad305b08c3315c215e97b82643ee450b8e6208930aa936128b835d62a661bc9823302442311da16a02bca9'
|
7
|
+
data.tar.gz: 51814f9dc8b31fea8a4bb789a8f7e474a453737d4e9f6eba7ed2548285d99d21608ec14111ec024017abc04e57ea50556be1b597f6cb3688d4d7cd0ba665dfcc
|
@@ -0,0 +1,16 @@
|
|
1
|
+
version: 2
|
2
|
+
updates:
|
3
|
+
- package-ecosystem: bundler
|
4
|
+
directory: "/"
|
5
|
+
schedule:
|
6
|
+
interval: daily
|
7
|
+
time: "10:00"
|
8
|
+
open-pull-requests-limit: 10
|
9
|
+
ignore:
|
10
|
+
- dependency-name: solargraph
|
11
|
+
versions:
|
12
|
+
- 0.40.2
|
13
|
+
- 0.40.3
|
14
|
+
- dependency-name: nokogiri
|
15
|
+
versions:
|
16
|
+
- 1.11.1
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# For most projects, this workflow file will not need changing; you simply need
|
2
|
+
# to commit it to your repository.
|
3
|
+
#
|
4
|
+
# You may wish to alter this file to override the set of languages analyzed,
|
5
|
+
# or to provide custom queries or build logic.
|
6
|
+
#
|
7
|
+
# ******** NOTE ********
|
8
|
+
# We have attempted to detect the languages in your repository. Please check
|
9
|
+
# the `language` matrix defined below to confirm you have the correct set of
|
10
|
+
# supported CodeQL languages.
|
11
|
+
#
|
12
|
+
name: "CodeQL"
|
13
|
+
|
14
|
+
on:
|
15
|
+
push:
|
16
|
+
branches: [ master ]
|
17
|
+
pull_request:
|
18
|
+
# The branches below must be a subset of the branches above
|
19
|
+
branches: [ master ]
|
20
|
+
schedule:
|
21
|
+
- cron: '15 17 * * 6'
|
22
|
+
|
23
|
+
jobs:
|
24
|
+
analyze:
|
25
|
+
name: Analyze
|
26
|
+
runs-on: ubuntu-latest
|
27
|
+
permissions:
|
28
|
+
actions: read
|
29
|
+
contents: read
|
30
|
+
security-events: write
|
31
|
+
|
32
|
+
strategy:
|
33
|
+
fail-fast: false
|
34
|
+
matrix:
|
35
|
+
language: [ 'ruby' ]
|
36
|
+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
37
|
+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
|
38
|
+
|
39
|
+
steps:
|
40
|
+
- name: Checkout repository
|
41
|
+
uses: actions/checkout@v2
|
42
|
+
|
43
|
+
# Initializes the CodeQL tools for scanning.
|
44
|
+
- name: Initialize CodeQL
|
45
|
+
uses: github/codeql-action/init@v2
|
46
|
+
with:
|
47
|
+
languages: ${{ matrix.language }}
|
48
|
+
# If you wish to specify custom queries, you can do so here or in a config file.
|
49
|
+
# By default, queries listed here will override any specified in a config file.
|
50
|
+
# Prefix the list here with "+" to use these queries and those in the config file.
|
51
|
+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
52
|
+
|
53
|
+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
54
|
+
# If this step fails, then you should remove it and run the build manually (see below)
|
55
|
+
- name: Autobuild
|
56
|
+
uses: github/codeql-action/autobuild@v2
|
57
|
+
|
58
|
+
# ℹ️ Command-line programs to run using the OS shell.
|
59
|
+
# 📚 https://git.io/JvXDl
|
60
|
+
|
61
|
+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
62
|
+
# and modify them (or add more) to build your code if your project
|
63
|
+
# uses a compiled language
|
64
|
+
|
65
|
+
#- run: |
|
66
|
+
# make bootstrap
|
67
|
+
# make release
|
68
|
+
|
69
|
+
- name: Perform CodeQL Analysis
|
70
|
+
uses: github/codeql-action/analyze@v2
|
data/.github/workflows/tests.yml
CHANGED
@@ -15,7 +15,7 @@ jobs:
|
|
15
15
|
strategy:
|
16
16
|
fail-fast: false
|
17
17
|
matrix:
|
18
|
-
ruby: ['2.
|
18
|
+
ruby: ['2.6', '2.7', '3.0', '3.1', 'jruby-9.3']
|
19
19
|
steps:
|
20
20
|
- uses: actions/checkout@v2
|
21
21
|
- uses: ruby/setup-ruby@v1
|
@@ -38,3 +38,12 @@ jobs:
|
|
38
38
|
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
39
39
|
with:
|
40
40
|
coverageCommand: bundle exec rake
|
41
|
+
yard:
|
42
|
+
runs-on: ubuntu-latest
|
43
|
+
steps:
|
44
|
+
- uses: actions/checkout@v2
|
45
|
+
- uses: ruby/setup-ruby@v1
|
46
|
+
with:
|
47
|
+
bundler-cache: true
|
48
|
+
- name: Build YARD docs
|
49
|
+
run: bundle exec yard doc --fail-on-warning
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.6.10
|
data/.solargraph.yml
CHANGED
data/.tool-versions
ADDED
data/CHANGELOG.txt
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
Change Log for Ruby-units
|
2
2
|
=========================
|
3
|
+
|
4
|
+
see GitHub releases (https://github.com/olbrich/ruby-units/releases) for more recent releases. This file will no longer
|
5
|
+
be updated.
|
6
|
+
|
3
7
|
2020-12-29 2.3.2 * Remove Jeweler (see #178) also adds Code of Conduct
|
4
8
|
* Fix specs related to Complex comparisons (see #213)
|
5
9
|
* Add support for Ruby 3.0 (also drop support for 2.3 and 2.4) (see #211)
|
data/Gemfile
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
group :development do
|
4
|
-
gem '
|
4
|
+
gem 'debug', '>= 1.0.0', platform: :mri
|
5
|
+
gem 'redcarpet', platform: :mri # redcarpet doesn't support jruby
|
5
6
|
gem 'ruby-maven', platform: :jruby
|
6
7
|
gem 'ruby-prof', platform: :mri
|
7
8
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,29 +1,31 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-units (
|
4
|
+
ruby-units (3.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
ast (2.4.
|
10
|
-
backport (1.
|
11
|
-
benchmark (0.
|
12
|
-
byebug (11.1.3)
|
9
|
+
ast (2.4.2)
|
10
|
+
backport (1.2.0)
|
11
|
+
benchmark (0.2.0)
|
13
12
|
coderay (1.1.3)
|
14
|
-
|
15
|
-
|
13
|
+
debug (1.6.2)
|
14
|
+
irb (>= 1.3.6)
|
15
|
+
reline (>= 0.3.1)
|
16
|
+
diff-lcs (1.5.0)
|
17
|
+
docile (1.4.0)
|
16
18
|
e2mmap (0.1.0)
|
17
|
-
ffi (1.
|
18
|
-
ffi (1.
|
19
|
-
formatador (
|
20
|
-
guard (2.
|
19
|
+
ffi (1.15.5)
|
20
|
+
ffi (1.15.5-java)
|
21
|
+
formatador (1.1.0)
|
22
|
+
guard (2.18.0)
|
21
23
|
formatador (>= 0.2.4)
|
22
24
|
listen (>= 2.7, < 4.0)
|
23
25
|
lumberjack (>= 1.0.12, < 2.0)
|
24
26
|
nenv (~> 0.1)
|
25
27
|
notiffany (~> 0.0)
|
26
|
-
pry (>= 0.
|
28
|
+
pry (>= 0.13.0)
|
27
29
|
shellany (~> 0.0)
|
28
30
|
thor (>= 0.18.1)
|
29
31
|
guard-compat (1.2.1)
|
@@ -31,97 +33,109 @@ GEM
|
|
31
33
|
guard (~> 2.1)
|
32
34
|
guard-compat (~> 1.1)
|
33
35
|
rspec (>= 2.99.0, < 4.0)
|
36
|
+
io-console (0.5.11)
|
37
|
+
irb (1.4.1)
|
38
|
+
reline (>= 0.3.0)
|
34
39
|
jaro_winkler (1.5.4)
|
35
40
|
jaro_winkler (1.5.4-java)
|
36
|
-
|
41
|
+
json (2.6.2)
|
42
|
+
json (2.6.2-java)
|
43
|
+
kramdown (2.4.0)
|
37
44
|
rexml
|
38
45
|
kramdown-parser-gfm (1.1.0)
|
39
46
|
kramdown (~> 2.0)
|
40
|
-
listen (3.
|
47
|
+
listen (3.7.1)
|
41
48
|
rb-fsevent (~> 0.10, >= 0.10.3)
|
42
49
|
rb-inotify (~> 0.9, >= 0.9.10)
|
43
50
|
lumberjack (1.2.8)
|
44
51
|
method_source (1.0.0)
|
45
|
-
mini_portile2 (2.
|
52
|
+
mini_portile2 (2.8.0)
|
46
53
|
nenv (0.3.0)
|
47
|
-
nokogiri (1.
|
48
|
-
mini_portile2 (~> 2.
|
49
|
-
|
54
|
+
nokogiri (1.13.8)
|
55
|
+
mini_portile2 (~> 2.8.0)
|
56
|
+
racc (~> 1.4)
|
57
|
+
nokogiri (1.13.8-java)
|
58
|
+
racc (~> 1.4)
|
59
|
+
nokogiri (1.13.8-x86_64-darwin)
|
60
|
+
racc (~> 1.4)
|
50
61
|
notiffany (0.1.3)
|
51
62
|
nenv (~> 0.1)
|
52
63
|
shellany (~> 0.0)
|
53
|
-
parallel (1.
|
54
|
-
parser (
|
64
|
+
parallel (1.22.1)
|
65
|
+
parser (3.1.2.1)
|
55
66
|
ast (~> 2.4.1)
|
56
|
-
pry (0.
|
67
|
+
pry (0.14.1)
|
57
68
|
coderay (~> 1.1)
|
58
69
|
method_source (~> 1.0)
|
59
|
-
pry (0.
|
70
|
+
pry (0.14.1-java)
|
60
71
|
coderay (~> 1.1)
|
61
72
|
method_source (~> 1.0)
|
62
73
|
spoon (~> 0.0)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
rb-fsevent (0.10.4)
|
74
|
+
racc (1.6.0)
|
75
|
+
racc (1.6.0-java)
|
76
|
+
rainbow (3.1.1)
|
77
|
+
rake (13.0.6)
|
78
|
+
rb-fsevent (0.11.2)
|
69
79
|
rb-inotify (0.10.1)
|
70
80
|
ffi (~> 1.0)
|
71
|
-
|
72
|
-
|
81
|
+
redcarpet (3.5.1)
|
82
|
+
regexp_parser (2.6.0)
|
83
|
+
reline (0.3.1)
|
84
|
+
io-console (~> 0.5)
|
85
|
+
reverse_markdown (2.1.1)
|
73
86
|
nokogiri
|
74
|
-
rexml (3.2.
|
75
|
-
rspec (3.
|
76
|
-
rspec-core (~> 3.
|
77
|
-
rspec-expectations (~> 3.
|
78
|
-
rspec-mocks (~> 3.
|
79
|
-
rspec-core (3.
|
80
|
-
rspec-support (~> 3.
|
81
|
-
rspec-expectations (3.
|
87
|
+
rexml (3.2.5)
|
88
|
+
rspec (3.11.0)
|
89
|
+
rspec-core (~> 3.11.0)
|
90
|
+
rspec-expectations (~> 3.11.0)
|
91
|
+
rspec-mocks (~> 3.11.0)
|
92
|
+
rspec-core (3.11.0)
|
93
|
+
rspec-support (~> 3.11.0)
|
94
|
+
rspec-expectations (3.11.1)
|
82
95
|
diff-lcs (>= 1.2.0, < 2.0)
|
83
|
-
rspec-support (~> 3.
|
84
|
-
rspec-mocks (3.
|
96
|
+
rspec-support (~> 3.11.0)
|
97
|
+
rspec-mocks (3.11.1)
|
85
98
|
diff-lcs (>= 1.2.0, < 2.0)
|
86
|
-
rspec-support (~> 3.
|
87
|
-
rspec-support (3.
|
88
|
-
rubocop (1.
|
99
|
+
rspec-support (~> 3.11.0)
|
100
|
+
rspec-support (3.11.1)
|
101
|
+
rubocop (1.36.0)
|
102
|
+
json (~> 2.3)
|
89
103
|
parallel (~> 1.10)
|
90
|
-
parser (>= 2.
|
104
|
+
parser (>= 3.1.2.1)
|
91
105
|
rainbow (>= 2.2.2, < 4.0)
|
92
106
|
regexp_parser (>= 1.8, < 3.0)
|
93
|
-
rexml
|
94
|
-
rubocop-ast (>= 1.
|
107
|
+
rexml (>= 3.2.5, < 4.0)
|
108
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
95
109
|
ruby-progressbar (~> 1.7)
|
96
|
-
unicode-display_width (>= 1.4.0, <
|
97
|
-
rubocop-ast (1.
|
98
|
-
parser (>=
|
99
|
-
rubocop-rake (0.
|
100
|
-
rubocop
|
101
|
-
rubocop-rspec (2.1.0)
|
110
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
111
|
+
rubocop-ast (1.21.0)
|
112
|
+
parser (>= 3.1.1.0)
|
113
|
+
rubocop-rake (0.6.0)
|
102
114
|
rubocop (~> 1.0)
|
103
|
-
|
104
|
-
|
115
|
+
rubocop-rspec (2.13.2)
|
116
|
+
rubocop (~> 1.33)
|
117
|
+
ruby-maven (3.3.13)
|
105
118
|
ruby-maven-libs (~> 3.3.9)
|
106
119
|
ruby-maven-libs (3.3.9)
|
107
|
-
ruby-prof (1.4.
|
108
|
-
ruby-progressbar (1.
|
120
|
+
ruby-prof (1.4.3)
|
121
|
+
ruby-progressbar (1.11.0)
|
109
122
|
shellany (0.0.1)
|
110
|
-
simplecov (0.
|
123
|
+
simplecov (0.21.2)
|
111
124
|
docile (~> 1.1)
|
112
125
|
simplecov-html (~> 0.11)
|
113
126
|
simplecov_json_formatter (~> 0.1)
|
114
127
|
simplecov-html (0.12.3)
|
115
|
-
simplecov_json_formatter (0.1.
|
116
|
-
solargraph (0.
|
117
|
-
backport (~> 1.
|
128
|
+
simplecov_json_formatter (0.1.4)
|
129
|
+
solargraph (0.47.2)
|
130
|
+
backport (~> 1.2)
|
118
131
|
benchmark
|
119
132
|
bundler (>= 1.17.2)
|
133
|
+
diff-lcs (~> 1.4)
|
120
134
|
e2mmap
|
121
135
|
jaro_winkler (~> 1.5)
|
122
136
|
kramdown (~> 2.3)
|
123
137
|
kramdown-parser-gfm (~> 1.1)
|
124
|
-
parser (~>
|
138
|
+
parser (~> 3.0)
|
125
139
|
reverse_markdown (>= 1.0.5, < 3)
|
126
140
|
rubocop (>= 0.52)
|
127
141
|
thor (~> 1.0)
|
@@ -131,22 +145,26 @@ GEM
|
|
131
145
|
ffi
|
132
146
|
terminal-notifier (2.0.0)
|
133
147
|
terminal-notifier-guard (1.7.0)
|
134
|
-
thor (1.
|
135
|
-
tilt (2.0.
|
136
|
-
unicode-display_width (
|
137
|
-
|
148
|
+
thor (1.2.1)
|
149
|
+
tilt (2.0.11)
|
150
|
+
unicode-display_width (2.3.0)
|
151
|
+
webrick (1.7.0)
|
152
|
+
yard (0.9.28)
|
153
|
+
webrick (~> 1.7.0)
|
138
154
|
|
139
155
|
PLATFORMS
|
140
156
|
java
|
141
157
|
ruby
|
142
158
|
universal-java-11
|
159
|
+
universal-java-18
|
143
160
|
x86_64-darwin-19
|
144
161
|
|
145
162
|
DEPENDENCIES
|
163
|
+
debug (>= 1.0.0)
|
146
164
|
guard-rspec
|
147
165
|
pry
|
148
|
-
pry-byebug
|
149
166
|
rake
|
167
|
+
redcarpet
|
150
168
|
rspec (~> 3.0)
|
151
169
|
rubocop
|
152
170
|
rubocop-rake
|
@@ -159,6 +177,8 @@ DEPENDENCIES
|
|
159
177
|
solargraph
|
160
178
|
terminal-notifier
|
161
179
|
terminal-notifier-guard
|
180
|
+
webrick
|
181
|
+
yard
|
162
182
|
|
163
183
|
BUNDLED WITH
|
164
|
-
2.
|
184
|
+
2.3.13
|
data/lib/ruby-units.rb
CHANGED
data/lib/ruby_units/array.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module RubyUnits
|
2
|
+
# Extra methods for [::Array] to support conversion to [RubyUnits::Unit]
|
3
|
+
module Array
|
4
|
+
# Construct a unit from an array
|
5
|
+
#
|
6
|
+
# @example [1, 'mm'].to_unit => RubyUnits::Unit.new("1 mm")
|
7
|
+
# @param [RubyUnits::Unit, String] other convert to same units as passed
|
8
|
+
# @return [RubyUnits::Unit]
|
9
|
+
def to_unit(other = nil)
|
10
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
11
|
+
end
|
8
12
|
end
|
9
13
|
end
|
14
|
+
|
15
|
+
# @note Do this instead of Array.prepend(RubyUnits::Array) to avoid YARD warnings
|
16
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
17
|
+
class Array
|
18
|
+
prepend(RubyUnits::Array)
|
19
|
+
end
|
data/lib/ruby_units/cache.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
1
|
module RubyUnits
|
2
|
-
|
3
|
-
|
2
|
+
# Performance optimizations to avoid creating units unnecessarily
|
3
|
+
class Cache
|
4
|
+
attr_accessor :data
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def initialize
|
7
|
+
clear
|
8
|
+
end
|
9
|
+
|
10
|
+
# @param key [String, #to_unit]
|
11
|
+
# @return [RubyUnits::Unit, nil]
|
12
|
+
def get(key)
|
13
|
+
key = key&.to_unit&.units unless key.is_a?(String)
|
14
|
+
data[key]
|
15
|
+
end
|
9
16
|
|
10
|
-
|
11
|
-
|
12
|
-
|
17
|
+
# @param key [String, #to_unit]
|
18
|
+
# @return [void]
|
19
|
+
def set(key, value)
|
20
|
+
key = key.to_unit.units unless key.is_a?(String)
|
21
|
+
data[key] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Array<String>]
|
25
|
+
def keys
|
26
|
+
data.keys
|
27
|
+
end
|
13
28
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
Unit.new(1)
|
18
|
-
end
|
29
|
+
# Reset the cache
|
30
|
+
def clear
|
31
|
+
@data = {}
|
19
32
|
end
|
20
33
|
end
|
21
34
|
end
|
@@ -1,10 +1,3 @@
|
|
1
|
-
# allow for optional configuration of RubyUnits
|
2
|
-
#
|
3
|
-
# Usage:
|
4
|
-
#
|
5
|
-
# RubyUnits.configure do |config|
|
6
|
-
# config.separator = false
|
7
|
-
# end
|
8
1
|
module RubyUnits
|
9
2
|
class << self
|
10
3
|
attr_writer :configuration
|
@@ -18,15 +11,22 @@ module RubyUnits
|
|
18
11
|
@configuration = Configuration.new
|
19
12
|
end
|
20
13
|
|
14
|
+
# allow for optional configuration of RubyUnits
|
15
|
+
#
|
16
|
+
# Usage:
|
17
|
+
#
|
18
|
+
# RubyUnits.configure do |config|
|
19
|
+
# config.separator = false
|
20
|
+
# end
|
21
21
|
def self.configure
|
22
22
|
yield configuration
|
23
23
|
end
|
24
24
|
|
25
25
|
# holds actual configuration values for RubyUnits
|
26
26
|
class Configuration
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
27
|
+
# Used to separate the scalar from the unit when generating output. A value
|
28
|
+
# of `true` will insert a single space, and `false` will prevent adding a
|
29
|
+
# space to the string representation of a unit.
|
30
30
|
attr_reader :separator
|
31
31
|
|
32
32
|
def initialize
|
@@ -34,7 +34,8 @@ module RubyUnits
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def separator=(value)
|
37
|
-
raise ArgumentError, "configuration 'separator' may only be true or false" unless
|
37
|
+
raise ArgumentError, "configuration 'separator' may only be true or false" unless [true, false].include?(value)
|
38
|
+
|
38
39
|
@separator = value ? ' ' : nil
|
39
40
|
end
|
40
41
|
end
|
data/lib/ruby_units/date.rb
CHANGED
@@ -1,64 +1,57 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
|
-
|
4
|
-
# Date
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
module RubyUnits
|
4
|
+
# Extra methods for [::Date] to allow it to be used as a [RubyUnits::Unit]
|
5
|
+
module Date
|
6
|
+
# Allow date objects to do offsets by a time unit
|
7
|
+
#
|
8
|
+
# @example Date.today + Unit.new("1 week") => gives today+1 week
|
9
|
+
# @param [RubyUnits::Unit, Object] other
|
10
|
+
# @return [RubyUnits::Unit]
|
11
|
+
def +(other)
|
12
|
+
case other
|
13
|
+
when RubyUnits::Unit
|
14
|
+
other = other.convert_to('d').round if %w[y decade century].include? other.units
|
15
|
+
super(other.convert_to('day').scalar)
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
16
19
|
end
|
17
|
-
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
# Allow date objects to do offsets by a time unit
|
22
|
+
#
|
23
|
+
# @example Date.today - Unit.new("1 week") => gives today-1 week
|
24
|
+
# @param [RubyUnits::Unit, Object] other
|
25
|
+
# @return [RubyUnits::Unit]
|
26
|
+
def -(other)
|
27
|
+
case other
|
28
|
+
when RubyUnits::Unit
|
29
|
+
other = other.convert_to('d').round if %w[y decade century].include? other.units
|
30
|
+
super(other.convert_to('day').scalar)
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
29
34
|
end
|
30
|
-
end
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
other
|
38
|
-
|
39
|
-
|
40
|
-
# :nocov_19:
|
41
|
-
unless Date.instance_methods.include?(:to_time)
|
42
|
-
# @return [Time]
|
43
|
-
def to_time
|
44
|
-
Time.local(*ParseDate.parsedate(to_s))
|
36
|
+
# Construct a unit from a Date. This returns the number of days since the
|
37
|
+
# start of the Julian calendar as a Unit.
|
38
|
+
#
|
39
|
+
# @example Date.today.to_unit => Unit
|
40
|
+
# @return [RubyUnits::Unit]
|
41
|
+
# @param other [RubyUnits::Unit, String] convert to same units as passed
|
42
|
+
def to_unit(other = nil)
|
43
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
45
44
|
end
|
46
|
-
end
|
47
|
-
# :nocov_19:
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
return units_datetime_inspect if dump
|
53
|
-
to_s
|
54
|
-
end
|
55
|
-
|
56
|
-
unless Date.instance_methods.include?(:to_date)
|
57
|
-
# :nocov_19:
|
58
|
-
# @return [Date]
|
59
|
-
def to_date
|
60
|
-
Date.civil(year, month, day)
|
46
|
+
# @deprecated
|
47
|
+
def inspect(dump = false)
|
48
|
+
dump ? super : to_s
|
61
49
|
end
|
62
|
-
# :nocov_19:
|
63
50
|
end
|
64
51
|
end
|
52
|
+
|
53
|
+
# @note Do this instead of Date.prepend(RubyUnits::Date) to avoid YARD warnings
|
54
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
55
|
+
class Date
|
56
|
+
prepend RubyUnits::Date
|
57
|
+
end
|