Skip to content

Commit

Permalink
Loosen EtOrbi add and subtract, gh-40
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Dec 4, 2024
1 parent fb27fab commit b758c91
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## et-orbi 1.3.0 not yet released

- Let EtOrbi #+ and #- accept instances with #to_i or #to_f (not strs), gh-40
- Introduce EtOrbi.zone_abbreviation(zone_name, time)


Expand Down
1 change: 1 addition & 0 deletions et-orbi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Time zones for fugit and rufus-scheduler. Urbi et Orbi.

s.add_development_dependency 'rspec', '~> 3.8'
s.add_development_dependency 'chronic', '~> 0.10'
#s.add_development_dependency 'activesupport', '~> 8.0'

s.require_path = 'lib'
end
Expand Down
28 changes: 21 additions & 7 deletions lib/et-orbi/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,33 @@ def inc(t, dir=1)

r =
case t
when Numeric
nt = self.dup
nt.seconds += dir * t.to_f
nt
when ::Time, ::EtOrbi::EoTime

fail ArgumentError.new(
"Cannot add #{t.class} to EoTime") if dir > 0
"Cannot add #{t.class} to EoTime instance"
) if dir > 0

@seconds + dir * t.to_f

when String

false

else
fail ArgumentError.new(
"Cannot call add or subtract #{t.class} to EoTime instance")

if t.respond_to?(:to_f)
nt = self.dup; nt.seconds += dir * t.to_f; nt
elsif t.respond_to?(:to_i)
nt = self.dup; nt.seconds += dir * t.to_i; nt
else
false
end
end

fail ArgumentError.new(
"Cannot call add or subtract #{t.class} on EoTime instance"
) unless r

touch

r
Expand Down
22 changes: 20 additions & 2 deletions spec/eo_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@
expect(ot.seconds).to eq(1193898300 + 111)
end

it 'adds anything that has a #to_i and is not a String, gh-40' do

ot =
EtOrbi::EoTime.new(1193898300, 'Asia/Vladivostok') +
SpecActiveSupportDuration.new(3600) # 1.hour

expect(ot.seconds).to eq(1193898300 + 3600)
end

it 'goes into DST' do

ot =
Expand Down Expand Up @@ -366,6 +375,15 @@
expect(ot.seconds).to eq(1193898300 - 111)
end

it 'subtracts anything that has a #to_i and is not a String, gh-40' do

ot =
EtOrbi::EoTime.new(1193898300, 'Asia/Vladivostok') -
SpecActiveSupportDuration.new(3600) # 1.hour

expect(ot.seconds).to eq(1193898300 - 3600)
end

it 'returns self' do

ot = EtOrbi::EoTime.new(1193898300, 'Europe/Paris')
Expand Down Expand Up @@ -468,7 +486,7 @@
expect {
ot + t
}.to raise_error(
ArgumentError, 'Cannot add Time to EoTime'
ArgumentError, 'Cannot add Time to EoTime instance'
)
end

Expand All @@ -480,7 +498,7 @@
expect {
ot + ot1
}.to raise_error(
ArgumentError, 'Cannot add EtOrbi::EoTime to EoTime'
ArgumentError, 'Cannot add EtOrbi::EoTime to EoTime instance'
)
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ def tzinfo; @z; end
def self.make(s); self.new(::TZInfo::Timezone.get(s)); end
end

class SpecActiveSupportDuration

def initialize(i); @i = i; end
def to_i; @i; end
end


RSpec::Matchers.define :be_one_of do |arr|

Expand Down

0 comments on commit b758c91

Please sign in to comment.