infinite_time 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/infinite_time.rb +7 -51
- data/lib/infinite_time/time_compatability.rb +45 -0
- data/lib/infinite_time/version.rb +1 -1
- data/spec/infinite_time_spec.rb +13 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05cc42612c723a43d3c142bb92b8c3d0e07c26ff
|
4
|
+
data.tar.gz: 5006b220408d6ef2486e7aac421fc0218f57ce20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 029d067636b9c74ae1289746e1ce1edd25aa7bc0cc991b8d35cf2c4ba125b23e53e54c0636a8ffece1efb70adde1a1eac9be92a0441d6d282ad5089c5f232a26
|
7
|
+
data.tar.gz: ae722f44230bb2f5593d2a6b527cfbd67a72bebe3da15009a6529be5ffb9ff476317debb49ec7d660374bd063b9e7f3ab403c1b9d0a97dbf69db437b1e56821d
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# InfiniteTime
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/amarshall/infinite_time.png?branch=master)](http://travis-ci.org/amarshall/infinite_time)
|
4
|
+
[![Code Climate rating](https://codeclimate.com/github/amarshall/infinite_time.png)](https://codeclimate.com/github/amarshall/infinite_time)
|
4
5
|
|
5
6
|
Provides a representation of a (positively or negatively) infinite time. This allows easier comparison with times when, for instance, you have an end date for an object but it is not yet known. Using `nil` would cause TypeErrors everywhere and you must then guard against that, this avoids that issue.
|
6
7
|
|
@@ -21,6 +22,13 @@ negative = InfiniteTime.new :-
|
|
21
22
|
positive == InfiniteTime.new #=> true
|
22
23
|
```
|
23
24
|
|
25
|
+
`Time` has also been extended to be comparable with an `InfiniteTime`, as well as provides the `infinite?` method:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Time.new.infinite? #=> false
|
29
|
+
InfiniteTime.new.infinite? #=> true
|
30
|
+
```
|
31
|
+
|
24
32
|
Feel free to read the specs for more details.
|
25
33
|
|
26
34
|
## Contributing
|
data/lib/infinite_time.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require 'infinite_time/version'
|
2
2
|
require 'infinite_time/core_ext'
|
3
|
+
require 'infinite_time/time_compatability'
|
3
4
|
|
4
5
|
class InfiniteTime < Time
|
6
|
+
include TimeCompatability
|
7
|
+
|
5
8
|
class << self
|
6
9
|
undef at
|
7
10
|
undef gm
|
@@ -47,6 +50,10 @@ class InfiniteTime < Time
|
|
47
50
|
self.class.hash ^ @sign.hash
|
48
51
|
end
|
49
52
|
|
53
|
+
def to_f
|
54
|
+
(positive?) ? Float::INFINITY : -Float::INFINITY
|
55
|
+
end
|
56
|
+
|
50
57
|
def to_i; raise TypeError, 'InfiniteTime has no Integer representation'; end
|
51
58
|
def to_r; raise TypeError, 'InfiniteTime has no Rational representation'; end
|
52
59
|
|
@@ -59,56 +66,5 @@ class InfiniteTime < Time
|
|
59
66
|
|
60
67
|
def strftime _; to_s; end
|
61
68
|
|
62
|
-
private
|
63
|
-
|
64
|
-
def signed_float
|
65
|
-
(positive?) ? Float::INFINITY : -Float::INFINITY
|
66
|
-
end
|
67
|
-
|
68
69
|
class InvalidSign < StandardError; end
|
69
|
-
|
70
|
-
module TimeCompatability
|
71
|
-
def self.define_method name, arity = 0, &block
|
72
|
-
super name do |*args|
|
73
|
-
unless arity === args.length
|
74
|
-
raise ArgumentError, "wrong number of arguments (#{args.length} for #{arity})"
|
75
|
-
end
|
76
|
-
instance_exec(*args, &block)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.nil_method name, arity = 0
|
81
|
-
define_method(name, arity) { nil }
|
82
|
-
end
|
83
|
-
def self.nil_methods names
|
84
|
-
names.each { |name| nil_method name }
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.signed_method name, arity = 0
|
88
|
-
define_method(name, arity) { signed_float }
|
89
|
-
end
|
90
|
-
def self.signed_methods names
|
91
|
-
names.each { |name| signed_method name }
|
92
|
-
end
|
93
|
-
|
94
|
-
def self.self_method name, arity = 0
|
95
|
-
define_method(name, arity) { self }
|
96
|
-
end
|
97
|
-
def self.self_methods names
|
98
|
-
names.each { |name| self_method name }
|
99
|
-
end
|
100
|
-
|
101
|
-
signed_methods %i[year month day yday wday hour minute second subsec nsec usec]
|
102
|
-
nil_methods %i[monday? tuesday? wednesday? thursday? friday? saturday? sunday?]
|
103
|
-
nil_methods %i[dst? isdst gmt? gmt_offset gmtoff utc? utc_offset zone]
|
104
|
-
self_method :round, 0..1
|
105
|
-
self_method :succ
|
106
|
-
alias_method :mday, :day
|
107
|
-
alias_method :mon, :month
|
108
|
-
alias_method :sec, :second
|
109
|
-
alias_method :tv_nsec, :nsec
|
110
|
-
alias_method :tv_usec, :usec
|
111
|
-
end
|
112
|
-
private_constant :TimeCompatability
|
113
|
-
include TimeCompatability
|
114
70
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class InfiniteTime < Time
|
2
|
+
module TimeCompatability
|
3
|
+
def self.define_method name, arity = 0, &block
|
4
|
+
super name do |*args|
|
5
|
+
unless arity === args.length
|
6
|
+
raise ArgumentError, "wrong number of arguments (#{args.length} for #{arity})"
|
7
|
+
end
|
8
|
+
instance_exec(*args, &block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.nil_method name, arity = 0
|
13
|
+
define_method(name, arity) { nil }
|
14
|
+
end
|
15
|
+
def self.nil_methods names
|
16
|
+
names.each { |name| nil_method name }
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.signed_method name, arity = 0
|
20
|
+
define_method(name, arity) { to_f }
|
21
|
+
end
|
22
|
+
def self.signed_methods names
|
23
|
+
names.each { |name| signed_method name }
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.self_method name, arity = 0
|
27
|
+
define_method(name, arity) { self }
|
28
|
+
end
|
29
|
+
def self.self_methods names
|
30
|
+
names.each { |name| self_method name }
|
31
|
+
end
|
32
|
+
|
33
|
+
signed_methods %i[year month day yday wday hour minute second subsec nsec usec]
|
34
|
+
nil_methods %i[monday? tuesday? wednesday? thursday? friday? saturday? sunday?]
|
35
|
+
nil_methods %i[dst? isdst gmt? gmt_offset gmtoff utc? utc_offset zone]
|
36
|
+
self_method :round, 0..1
|
37
|
+
self_method :succ
|
38
|
+
alias_method :mday, :day
|
39
|
+
alias_method :mon, :month
|
40
|
+
alias_method :sec, :second
|
41
|
+
alias_method :tv_nsec, :nsec
|
42
|
+
alias_method :tv_usec, :usec
|
43
|
+
end
|
44
|
+
private_constant :TimeCompatability
|
45
|
+
end
|
data/spec/infinite_time_spec.rb
CHANGED
@@ -5,6 +5,10 @@ describe InfiniteTime do
|
|
5
5
|
InfiniteTime.new.is_a?(Time).should == true
|
6
6
|
end
|
7
7
|
|
8
|
+
it "is infinite" do
|
9
|
+
InfiniteTime.new.infinite?.should == true
|
10
|
+
end
|
11
|
+
|
8
12
|
it "is positively infinite by default" do
|
9
13
|
itime = InfiniteTime.new
|
10
14
|
itime.positive?.should == true
|
@@ -110,7 +114,7 @@ describe InfiniteTime do
|
|
110
114
|
negative.month.should == -Float::INFINITY
|
111
115
|
negative.mon.should == -Float::INFINITY
|
112
116
|
negative.day.should == -Float::INFINITY
|
113
|
-
negative.yday.should
|
117
|
+
negative.yday.should == -Float::INFINITY
|
114
118
|
negative.mday.should == -Float::INFINITY
|
115
119
|
negative.wday.should == -Float::INFINITY
|
116
120
|
negative.hour.should == -Float::INFINITY
|
@@ -124,6 +128,14 @@ describe InfiniteTime do
|
|
124
128
|
negative.tv_usec.should == -Float::INFINITY
|
125
129
|
end
|
126
130
|
|
131
|
+
it "returns the appropriate infinity for to_f" do
|
132
|
+
positive = InfiniteTime.new :+
|
133
|
+
negative = InfiniteTime.new :-
|
134
|
+
|
135
|
+
positive.to_f.should == Float::INFINITY
|
136
|
+
negative.to_f.should == -Float::INFINITY
|
137
|
+
end
|
138
|
+
|
127
139
|
it "returns a TypeError for impossible numeric conversions" do
|
128
140
|
itime = InfiniteTime.new
|
129
141
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infinite_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Marshall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- infinite_time.gemspec
|
69
69
|
- lib/infinite_time.rb
|
70
70
|
- lib/infinite_time/core_ext.rb
|
71
|
+
- lib/infinite_time/time_compatability.rb
|
71
72
|
- lib/infinite_time/time_extension.rb
|
72
73
|
- lib/infinite_time/version.rb
|
73
74
|
- spec/infinite_time/core_ext_spec.rb
|