Skip to content

Commit

Permalink
Improve comparison to Times
Browse files Browse the repository at this point in the history
  • Loading branch information
docelic committed Dec 21, 2023
1 parent 1ff6b22 commit 02a5eed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: virtualtime
version: 1.1.5
version: 1.1.6

authors:
- Davor Ocelic <[email protected]>
Expand Down
27 changes: 14 additions & 13 deletions src/virtualtime.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end
class VirtualTime
VERSION_MAJOR = 1
VERSION_MINOR = 1
VERSION_REVISION = 5
VERSION_REVISION = 6
VERSION = [VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION].join '.'

include Comparable(Time)
Expand Down Expand Up @@ -330,29 +330,30 @@ class VirtualTime
end

# :ditto:
def self.materialize(value : Enumerable(Int), default : Int32, max = nil, strict = true)
if max && value.any?(&.<(0))
value = value.map { |e| e < 0 ? max + e + 1 : e }
def self.materialize(value : Range(Int, Int), default : Int32, max = nil, strict = true)
if max && (value.begin < 0 || value.end < 0)
ab = value.begin < 0 ? max + value.begin + 1 : value.begin
ae = value.end < 0 ? max + value.end + 1 : value.end
value = ab..ae
end

if !strict || value.includes? default
default
else
value.min
value.begin
end
end

# :ditto:
def self.materialize(value : Range(Int, Int), default : Int32, max = nil, strict = true)
if max && (value.begin < 0 || value.end < 0)
ab = value.begin < 0 ? max + value.begin + 1 : value.begin
ae = value.end < 0 ? max + value.end + 1 : value.end
value = ab..ae
def self.materialize(value : Enumerable(Int), default : Int32, max = nil, strict = true)
value = value.dup.to_a
if max && value.any?(&.<(0))
value = value.map { |e| e < 0 ? max + e + 1 : e }
end

if !strict || value.includes? default
default
else
value.begin
value.min
end
end

Expand Down Expand Up @@ -381,7 +382,7 @@ class VirtualTime

# Compares `VirtualTime` to `Time` instance
def <=>(other : Time)
to_time <=> other
to_time(other) <=> other
end

# "Rewinds" `day` forward enough to reach `acceptable_day`.
Expand Down

0 comments on commit 02a5eed

Please sign in to comment.