Commit 3fdca0737403ee4ebae1f5f04a8c42a548407e2d

Object#infinite? added with specs

Commit diff

lib/strokedb/core_ext.rb

 
66require 'core_ext/hash'
77require 'core_ext/fixnum'
88require 'core_ext/float'
9require 'core_ext/blank'
9require 'core_ext/infinity'
10require 'core_ext/blank'
toggle raw diff

lib/strokedb/core_ext/infinity.rb

 
1# These are for use in Range arguments for View#find.
2# We don't provide correct spaceship operator <=>
3# to preserve performance.
4#
5class Object
6 def infinite?
7 false
8 end
9end
10
11class Numeric
12 def infinite?
13 self.abs == Infinity
14 end
15end
16
17InfinityString = Class.new(String) do
18 def infinite?
19 true
20 end
21end.new.freeze
22
23InfinityTime = Class.new(Time) do
24 def infinite?
25 true
26 end
27end.new.freeze
28
29# For use like (SmallestString.."a") in View#find()
30#
31LargestString = SmallestString = InfinityString
32LargestTime = SmallestTime = InfinityTime
toggle raw diff

spec/lib/strokedb/core_ext/infinity_spec.rb

 
1require File.dirname(__FILE__) + '/spec_helper'
2
3describe "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
24end
25
26describe 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
32end
33
34describe 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
40end
toggle raw diff