| |   |
| 1 | # These are for use in Range arguments for View#find. |
| 2 | # We don't provide correct spaceship operator <=> |
| 3 | # to preserve performance. |
| 4 | # |
| 5 | class Object |
| 6 | def infinite? |
| 7 | false |
| 8 | end |
| 9 | end |
| 10 | |
| 11 | class Numeric |
| 12 | def infinite? |
| 13 | self.abs == Infinity |
| 14 | end |
| 15 | end |
| 16 | |
| 17 | InfinityString = Class.new(String) do |
| 18 | def infinite? |
| 19 | true |
| 20 | end |
| 21 | end.new.freeze |
| 22 | |
| 23 | InfinityTime = Class.new(Time) do |
| 24 | def infinite? |
| 25 | true |
| 26 | end |
| 27 | end.new.freeze |
| 28 | |
| 29 | # For use like (SmallestString.."a") in View#find() |
| 30 | # |
| 31 | LargestString = SmallestString = InfinityString |
| 32 | LargestTime = SmallestTime = InfinityTime |
| toggle raw diff |
--- /dev/null
+++ b/lib/strokedb/core_ext/infinity.rb
@@ -0,0 +1,32 @@
+# These are for use in Range arguments for View#find.
+# We don't provide correct spaceship operator <=>
+# to preserve performance.
+#
+class Object
+ def infinite?
+ false
+ end
+end
+
+class Numeric
+ def infinite?
+ self.abs == Infinity
+ end
+end
+
+InfinityString = Class.new(String) do
+ def infinite?
+ true
+ end
+end.new.freeze
+
+InfinityTime = Class.new(Time) do
+ def infinite?
+ true
+ end
+end.new.freeze
+
+# For use like (SmallestString.."a") in View#find()
+#
+LargestString = SmallestString = InfinityString
+LargestTime = SmallestTime = InfinityTime |
| |   |
| 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | |
| 3 | describe "Object#infinity?" do |
| 4 | it "should return false for the finite values" do |
| 5 | Object.should_not be_infinite |
| 6 | Object.new.should_not be_infinite |
| 7 | String.new.should_not be_infinite |
| 8 | Time.now.should_not be_infinite |
| 9 | :sym.should_not be_infinite |
| 10 | nil.should_not be_infinite |
| 11 | false.should_not be_infinite |
| 12 | true.should_not be_infinite |
| 13 | Float::MAX.should_not be_infinite |
| 14 | 2.71828.should_not be_infinite |
| 15 | 42.should_not be_infinite |
| 16 | (1..Infinity).should_not be_infinite # yep, this too. |
| 17 | end |
| 18 | it "should return true for infinite 'values'" do |
| 19 | Infinity.should be_infinite |
| 20 | (-Infinity).should be_infinite |
| 21 | (InfinityString).should be_infinite |
| 22 | (InfinityTime).should be_infinite |
| 23 | end |
| 24 | end |
| 25 | |
| 26 | describe InfinityString do |
| 27 | it "should be used in Range" do |
| 28 | (InfinityString.."a").should be_a_kind_of(Range) |
| 29 | ("a"..InfinityString).should be_a_kind_of(Range) |
| 30 | (InfinityString..InfinityString).should be_a_kind_of(Range) |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | describe InfinityTime do |
| 35 | it "should be used in Range" do |
| 36 | (InfinityTime..Time.now).should be_a_kind_of(Range) |
| 37 | (Time.now..InfinityTime).should be_a_kind_of(Range) |
| 38 | (InfinityTime..InfinityTime).should be_a_kind_of(Range) |
| 39 | end |
| 40 | end |
| toggle raw diff |
--- /dev/null
+++ b/spec/lib/strokedb/core_ext/infinity_spec.rb
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "Object#infinity?" do
+ it "should return false for the finite values" do
+ Object.should_not be_infinite
+ Object.new.should_not be_infinite
+ String.new.should_not be_infinite
+ Time.now.should_not be_infinite
+ :sym.should_not be_infinite
+ nil.should_not be_infinite
+ false.should_not be_infinite
+ true.should_not be_infinite
+ Float::MAX.should_not be_infinite
+ 2.71828.should_not be_infinite
+ 42.should_not be_infinite
+ (1..Infinity).should_not be_infinite # yep, this too.
+ end
+ it "should return true for infinite 'values'" do
+ Infinity.should be_infinite
+ (-Infinity).should be_infinite
+ (InfinityString).should be_infinite
+ (InfinityTime).should be_infinite
+ end
+end
+
+describe InfinityString do
+ it "should be used in Range" do
+ (InfinityString.."a").should be_a_kind_of(Range)
+ ("a"..InfinityString).should be_a_kind_of(Range)
+ (InfinityString..InfinityString).should be_a_kind_of(Range)
+ end
+end
+
+describe InfinityTime do
+ it "should be used in Range" do
+ (InfinityTime..Time.now).should be_a_kind_of(Range)
+ (Time.now..InfinityTime).should be_a_kind_of(Range)
+ (InfinityTime..InfinityTime).should be_a_kind_of(Range)
+ end
+end |