duration.rb 0.5.2 → 0.6.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
  SHA256:
3
- metadata.gz: 55bc0f38eb9c213a08b6ca49ea69908c2aeff8679637df50ab7309bc564ad457
4
- data.tar.gz: 14032f8850e3499398761abcf3155c1390d22b89e71e1c2be302cfe6b9863f5b
3
+ metadata.gz: 879170199d5fdc472851ece21f3896aa08d7767484045d281834255ca51d8c84
4
+ data.tar.gz: a35e1867f9712bb9aa415bcb577434e0683908c9f0c2579dfcaf27476770907c
5
5
  SHA512:
6
- metadata.gz: 41cc6f366ad11f553d299616312fbf325bb7979dfd7f978302a3b0afc4697b2d92dd506b35481deb17718add06377b0037df7d022a1f817010da69396a939cf4
7
- data.tar.gz: ef8d3edc58e19d3a2715901904af9be11600e52e1ec6b03cc5a58e920d69c6db1e4e26632169aa887c6d3ddbf99b9d6da928053f505faa4cf5c8890ad4a4229f
6
+ metadata.gz: b4ab866878939cd01e8ab62ff977fb53e974fb67b6ea2385c4837f60d2420b73e418ed75ba21aac52143487dd9b6b6c4818a11aac969ce7e14cc403ce7678928
7
+ data.tar.gz: 8b51e85072dbeede9062953fa1391a848242934c0d09559715a66bfc902cbd1284c7e53ef3e6811cbf859611c6f3d746d412eaf1e127e17e619c2a783446d552
data/CHANGELOG CHANGED
@@ -1,5 +1,18 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 20260812
4
+
5
+ 0.6.0: The unit classes are now autoloaded so as to avoid circular loading messages when running the tests. It's faster too!
6
+
7
+ 1. + lib/duration.rb: Autoload each unit file, and Common and Relative.
8
+ 2. ~ lib/Duration/*.rb: - require_relative './Common', './Relative'
9
+ 3. ~ lib/Duration/*.rb: + require_relative '../duration'. Each unit had required both of its neighbours, so that every adjacent pair was a cycle and Ruby warned upon each under -w. The ring was connected besides, so requiring any one unit dragged in eleven files of the thirteen and no part of the gem could be left unread. Requiring one now brings five, and the rest arrive as they are named.
10
+ 4. ~ lib/duration.rb: Add a note why VERSION is required and not declared, being the one exception.
11
+ 5. ~ README.md: Add a line in the Loading section.
12
+ 6. + test/loading_test.rb: that a single unit file required alone still converts, that a unit never named is never read, that one named is, and that the whole loads without warning under -w. Each runs in a process of its own, the test helper having already loaded everything in this one.
13
+ 7. ~ Duration::VERSION: /0.5.2/0.6.0/
14
+
15
+
3
16
  ## 20260812
4
17
 
5
18
  0.5.2: + gemspec test.
data/README.md CHANGED
@@ -52,8 +52,12 @@ require 'duration-numeric' # opt in to the Numeric sugar (5.minutes, etc.),
52
52
  ```
53
53
 
54
54
  The examples below use the `Numeric` sugar, so they assume
55
- `require 'duration-numeric'`. Without it, construct durations directly with
56
- `Duration::Minutes.new(5)` and friends.
55
+ `require 'duration-numeric'`. Without it durations are constructed directly with
56
+ `Duration::Minutes.new(5)` and so on.
57
+
58
+ Either require declares where the unit classes are without reading them. A unit
59
+ file is read when its class is first named, so a program dealing in seconds and
60
+ minutes reads the files for seconds and minutes and leaves the rest unread.
57
61
 
58
62
  ## Usage
59
63
 
data/lib/Duration/Days.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  # Duration/Days.rb
2
2
  # Duration::Days
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Hours'
7
- require_relative './Weeks'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Days
@@ -1,10 +1,7 @@
1
1
  # Duration/Hours.rb
2
2
  # Duration::Hours
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Minutes'
7
- require_relative './Days'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Hours
@@ -1,10 +1,7 @@
1
1
  # Duration/Microseconds.rb
2
2
  # Duration::Microseconds
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Nanoseconds'
7
- require_relative './Milliseconds'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Microseconds
@@ -1,10 +1,7 @@
1
1
  # Duration/Milliseconds.rb
2
2
  # Duration::Milliseconds
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Microseconds'
7
- require_relative './Seconds'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Milliseconds
@@ -1,10 +1,7 @@
1
1
  # Duration/Minutes.rb
2
2
  # Duration::Minutes
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Seconds'
7
- require_relative './Hours'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Minutes
@@ -1,9 +1,7 @@
1
1
  # Duration/Months.rb
2
2
  # Duration::Months
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Weeks'
4
+ require_relative '../duration'
7
5
 
8
6
  module Duration
9
7
  # A month is not a fixed quantity: its length depends on the calendar. The
@@ -1,9 +1,7 @@
1
1
  # Duration/Nanoseconds.rb
2
2
  # Duration::Nanoseconds
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Microseconds'
4
+ require_relative '../duration'
7
5
 
8
6
  module Duration
9
7
  class Nanoseconds
@@ -1,10 +1,7 @@
1
1
  # Duration/Seconds.rb
2
2
  # Duration::Seconds
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Milliseconds'
7
- require_relative './Minutes'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Seconds
@@ -1,3 +1,3 @@
1
1
  module Duration
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -1,10 +1,7 @@
1
1
  # Duration/Weeks.rb
2
2
  # Duration::Weeks
3
3
 
4
- require_relative './Common'
5
- require_relative './Relative'
6
- require_relative './Days'
7
- require_relative './Months'
4
+ require_relative '../duration'
8
5
 
9
6
  module Duration
10
7
  class Weeks
data/lib/duration.rb CHANGED
@@ -1,15 +1,24 @@
1
1
  # Duration.rb
2
2
  # Duration
3
3
 
4
- module Duration; end
5
-
6
- require_relative 'Duration/Nanoseconds'
7
- require_relative 'Duration/Microseconds'
8
- require_relative 'Duration/Milliseconds'
9
- require_relative 'Duration/Seconds'
10
- require_relative 'Duration/Minutes'
11
- require_relative 'Duration/Hours'
12
- require_relative 'Duration/Days'
13
- require_relative 'Duration/Weeks'
14
- require_relative 'Duration/Months'
4
+ # Previously, neighbouring units which are used conversion methods used to be requires at the top of each file, so that every adjacent pair was a cycle and Ruby warned upon each under -w.
5
+
6
+ # Now, a unit file is read when its class is first named and not before, which is what lets a program dealing in seconds and minutes loads only Seconds and Minutes, so that units are may easily be left unused.
7
+
8
+ module Duration
9
+ autoload :Common, File.expand_path('Duration/Common', __dir__)
10
+ autoload :Relative, File.expand_path('Duration/Relative', __dir__)
11
+ autoload :Nanoseconds, File.expand_path('Duration/Nanoseconds', __dir__)
12
+ autoload :Microseconds, File.expand_path('Duration/Microseconds', __dir__)
13
+ autoload :Milliseconds, File.expand_path('Duration/Milliseconds', __dir__)
14
+ autoload :Seconds, File.expand_path('Duration/Seconds', __dir__)
15
+ autoload :Minutes, File.expand_path('Duration/Minutes', __dir__)
16
+ autoload :Hours, File.expand_path('Duration/Hours', __dir__)
17
+ autoload :Days, File.expand_path('Duration/Days', __dir__)
18
+ autoload :Weeks, File.expand_path('Duration/Weeks', __dir__)
19
+ autoload :Months, File.expand_path('Duration/Months', __dir__)
20
+ end
21
+
22
+ # VERSION is required rather than autoloaded, and is the one exception, as there nothing which will load it at runtime.
23
+
15
24
  require_relative 'Duration/VERSION'
@@ -0,0 +1,32 @@
1
+ # test/loading_test.rb
2
+
3
+ require_relative './test_helper'
4
+
5
+ # Each of these wants a process of its own. Within this one the test helper has already loaded the lot, so nothing here could tell a unit which was declared from a unit which was read.
6
+
7
+ describe 'loading' do
8
+ def ruby(source, *flags)
9
+ lib = File.expand_path('../lib', __dir__)
10
+ IO.popen(['ruby', *flags, "-I#{lib}", '-e', source], err: [:child, :out]){|io| io.read}.strip
11
+ end
12
+
13
+ it "converts when a single unit file is required alone" do
14
+ _(ruby('require "Duration/Seconds"; print Duration::Seconds.new(90).to_minutes.to_f')) \
15
+ .must_equal('1.5')
16
+ end
17
+
18
+ it "does not read a unit which is never named" do
19
+ _(ruby('require "duration.rb"; Duration::Seconds; print $LOADED_FEATURES.grep(/Months/).size')) \
20
+ .must_equal('0')
21
+ end
22
+
23
+ it "reads a unit as soon as it is named" do
24
+ _(ruby('require "duration.rb"; Duration::Months; print $LOADED_FEATURES.grep(/Months/).size')) \
25
+ .must_equal('1')
26
+ end
27
+
28
+ it "loads without warning under -w, no unit requiring another" do
29
+ _(ruby('require "duration.rb"; Duration::Months.new(1).to_nanoseconds; print "ok"', '-w')) \
30
+ .must_equal('ok')
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duration.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -93,6 +93,7 @@ files:
93
93
  - test/Duration/VERSION_test.rb
94
94
  - test/Duration/Weeks_test.rb
95
95
  - test/gemspec_test.rb
96
+ - test/loading_test.rb
96
97
  - test/test_helper.rb
97
98
  homepage: http://github.com/thoran/duration.rb
98
99
  licenses: