Skip to content

Commit

Permalink
refactor: Split Numeric core extensions out.
Browse files Browse the repository at this point in the history
These are not used directly by Time::Iterator, but are for convenience.
They may be removed later.
  • Loading branch information
schwern committed Dec 14, 2023
1 parent d0a9679 commit 87b3071
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 1 addition & 8 deletions lib/time/iterator.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require "active_support"
require "active_support/core_ext/integer/time"
require_relative "iterator/core_ext/numeric"
require_relative "iterator/core_ext/time"

# Inject 3.quarters
class Numeric
def quarter
(3 * self).months
end
alias quarters quarter
end

class Time
# Time iteration, and extra Time methods.
module Iterator
Expand Down
7 changes: 7 additions & 0 deletions lib/time/iterator/core_ext/numeric.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Inject 3.quarters
class Numeric
def quarter
(3 * self).months
end
alias quarters quarter
end
13 changes: 13 additions & 0 deletions spec/time/core_ext/numeric_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RSpec.describe Numeric do
let(:time) { Time.now }

describe '#quarter(s)' do
[:quarter, :quarters].each do |method|
it 'adds 3 months per quarter' do
expect( time + 1.send(method) ).to eq time + 3.months
expect( time + 3.send(method) ).to eq time + 9.months
expect( time + 4.send(method) ).to eq time + 1.year
end
end
end
end
10 changes: 0 additions & 10 deletions spec/time/iterator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
RSpec.describe Time::Iterator do
let(:time) { Time.now }

describe '#quarter(s)' do
[:quarter, :quarters].each do |method|
it 'adds 3 months per quarter' do
expect( time + 1.send(method) ).to eq time + 3.months
expect( time + 3.send(method) ).to eq time + 9.months
expect( time + 4.send(method) ).to eq time + 1.year
end
end
end

describe '#iterate' do
it 'uses an Enumerator' do
expect( time.iterate(by: :day) ).to be_a(Enumerator)
Expand Down

0 comments on commit 87b3071

Please sign in to comment.