ae 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,3 @@
1
- #--
2
- # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
3
- # All rights reserved.
4
- #
5
- # Permission is granted for use, copying, modification, distribution,
6
- # and distribution of modified versions of this work as long as the
7
- # above copyright notice is included.
8
- #++
9
-
10
1
  if RUBY_VERSION >= '1.9'
11
2
 
12
3
  module AE
@@ -18,9 +9,14 @@ else
18
9
  module AE
19
10
  # BasicObject provides an abstract base class with no predefined
20
11
  # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
21
- # BlankSlate is useful as a base class when writing classes that
12
+ # BasicObject is useful as a base class when writing classes that
22
13
  # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
23
14
  #
15
+ # BasicObject is based on BlankSlate by Jim Weirich.
16
+ #
17
+ # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
18
+ # All rights reserved.
19
+
24
20
  class BasicObject #:nodoc:
25
21
 
26
22
  # Hide the method named +name+ in the BlankSlate class. Don't
data/lib/ae/core_ext.rb CHANGED
@@ -1,214 +1,8 @@
1
- # Nearly all, if not all, of these core extension are
2
- # available from Ruby Facets.
3
-
4
- # hack
5
- NoArgument = Object.new
6
-
7
- module Kernel
8
- # Force an Assertion failure.
9
- #
10
- # TODO: Can we call this #fail (overriding built-in)?
11
- #
12
- def flunk(reason=nil, backtrace=nil)
13
- raise Assertion.new((reason || 'flunk'), :backtrace=>(backtrace || caller))
14
- end
15
-
16
- # Is literally true.
17
- def true?
18
- TrueClass === self
19
- end
20
-
21
- # Is literally false.
22
- def false?
23
- FalseClass === self
24
- end
25
-
26
- # Are identical, eg. object_id's are equal.
27
- def identical?(exp)
28
- exp.object_id == object_id
29
- end
30
-
31
- # Alias for #identical?
32
- alias_method :identical_to?, :identical?
33
-
34
- # Word form of #==. Also can take a block.
35
- def eq?(value=NoArgument) #:yield:
36
- if block_given?
37
- self == yield
38
- else
39
- self == value
40
- end
41
- end
42
-
43
- # Word form of #===. Also can take a block.
44
- def case?(value=NoArgument) #:yield:
45
- if block_given?
46
- self === yield
47
- else
48
- self === value
49
- end
50
- end
51
-
52
- # Word form for #=~. Also can take a block.
53
- def match?(value=NoArgument)
54
- if block_given?
55
- self =~ yield
56
- else
57
- self =~ value
58
- end
59
- end
60
-
61
- # Broad equality.
62
- def equate?(x)
63
- equal?(x) || eql?(x) || self == x || self === x
64
- end
65
-
66
- # Can a message be sent to the receiver successfully?
67
- def send?(method, *args, &block)
68
- begin
69
- __send__(method, *args, &block)
70
- true
71
- rescue NoMethodError
72
- false
73
- end
74
- end
75
-
76
- #
77
- #def returns?(value) #:yield:
78
- # value == yield
79
- #end
80
-
81
- unless method_defined?(:public_send)
82
- #
83
- def public_send(m,*a,&b)
84
- raise NoMethodError unless respond_to?(m)
85
- __send__(m,*a,&b)
86
- end
87
- end
88
- end
89
-
90
-
91
- class Object
92
-
93
- # Allows equal? to take a block.
94
- def equal?(value=NoArgument) #:yield:
95
- if block_given?
96
- super(yield)
97
- else
98
- super
99
- end
100
- end
101
-
102
- # Allows eql? to take a block.
103
- def eql?(value=NoArgument) #:yield:
104
- if block_given?
105
- super(yield)
106
- else
107
- super
108
- end
109
- end
110
-
111
- end
112
-
113
- class Numeric
114
- # Is self and given number within delta tolerance.
115
- #
116
- # 0.05.in_delta?(50000.0 / 10**6, 0.00001)
117
- #
118
- def in_delta?(orig, delta=0.001)
119
- #(num.to_f - to_f).abs <= delta.to_f
120
- delta >= (orig - self).abs
121
- end
122
-
123
- # Alias for #in_delta.
124
- alias_method :close?, :in_delta?
125
-
126
- # Verify epsilon tolerance.
127
- def in_epsilon?(orig, epsilon=0.001)
128
- in_delta?(orig, [orig, self].min * epsilon)
129
- end
130
- end
131
-
132
-
133
- class Module
134
- # Is a given class or module an ancestor of this
135
- # class or module?
136
- #
137
- # class X ; end
138
- # class Y < X ; end
139
- #
140
- # Y.is?(X) #=> true
141
- #
142
- def is?(base)
143
- Module===base && ancestors.slice(1..-1).include?(base)
144
- end
145
- end
146
-
147
- class Proc
148
- #
149
- def raises?(exception=Exception, *args)
150
- begin
151
- call(*args)
152
- false
153
- rescue exception => error
154
- exception === error
155
- end
156
- end
157
-
158
- #
159
- def throws?(sym, *args)
160
- catch(sym) do
161
- begin
162
- call(*args)
163
- rescue ArgumentError # 1.9 exception
164
- rescue NameError # 1.8 exception
165
- end
166
- return false
167
- end
168
- return true
169
- end
170
-
171
- # TODO: Put in facets?
172
- # TODO: wrong place, change yield?
173
- def change?
174
- pre_result = yield
175
- called = call
176
- post_result = yield
177
- pre_result != post_result
178
- end
179
- end
180
-
181
- class Symbol
182
- # Does the block throw this symbol?
183
- #
184
- def thrown?(*args)
185
- catch(self) do
186
- begin
187
- yield(*args)
188
- rescue ArgumentError # 1.9 exception
189
- rescue NameError # 1.8 exception
190
- end
191
- return false
192
- end
193
- return true
194
- end
195
- end
196
-
197
- class Exception
198
- #
199
- def self.raised? #:yeild:
200
- begin
201
- yield
202
- false
203
- rescue self
204
- true
205
- end
206
- end
207
- end
1
+ require 'ae/core_ext/exception'
2
+ require 'ae/core_ext/helpers'
208
3
 
209
4
  # We need BasicObject for Assertor.
210
5
  unless defined?(BasicObject)
211
6
  require 'ae/basic_object'
212
7
  end
213
8
 
214
- # Copyright (c) 2008,2009 Thomas Sawyer
@@ -0,0 +1,13 @@
1
+ class Exception
2
+ # Is this exception the result of an assertion?
3
+ def assertion?
4
+ @assertion ||= false
5
+ end
6
+
7
+ # Set +true+/+false+ if the this exception is
8
+ # an assertion.
9
+ def set_assertion(boolean)
10
+ @assertion = !!boolean
11
+ end
12
+ end
13
+
@@ -0,0 +1,198 @@
1
+ # Nearly all, if not all, of these core extension are available from Ruby Facets.
2
+
3
+ # hack
4
+ NoArgument = Object.new
5
+
6
+ module Kernel
7
+ # Is literally true.
8
+ def true?
9
+ TrueClass === self
10
+ end
11
+
12
+ # Is literally false.
13
+ def false?
14
+ FalseClass === self
15
+ end
16
+
17
+ # Are identical, eg. object_id's are equal.
18
+ def identical?(exp)
19
+ exp.object_id == object_id
20
+ end
21
+
22
+ # Alias for #identical?
23
+ alias_method :identical_to?, :identical?
24
+
25
+ # Word form of #==. Also can take a block.
26
+ def eq?(value=NoArgument) #:yield:
27
+ if block_given?
28
+ self == yield
29
+ else
30
+ self == value
31
+ end
32
+ end
33
+
34
+ # Word form of #===. Also can take a block.
35
+ def case?(value=NoArgument) #:yield:
36
+ if block_given?
37
+ self === yield
38
+ else
39
+ self === value
40
+ end
41
+ end
42
+
43
+ # Word form for #=~. Also can take a block.
44
+ def match?(value=NoArgument)
45
+ if block_given?
46
+ self =~ yield
47
+ else
48
+ self =~ value
49
+ end
50
+ end
51
+
52
+ # Broad equality.
53
+ def equate?(x)
54
+ equal?(x) || eql?(x) || self == x || self === x
55
+ end
56
+
57
+ # Can a message be sent to the receiver successfully?
58
+ def send?(method, *args, &block)
59
+ begin
60
+ __send__(method, *args, &block)
61
+ true
62
+ rescue NoMethodError
63
+ false
64
+ end
65
+ end
66
+
67
+ #
68
+ #def returns?(value) #:yield:
69
+ # value == yield
70
+ #end
71
+
72
+ unless method_defined?(:public_send)
73
+ #
74
+ def public_send(m,*a,&b)
75
+ raise NoMethodError unless respond_to?(m)
76
+ __send__(m,*a,&b)
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ class Object
83
+ # Allows equal? to take a block.
84
+ def equal?(value=NoArgument) #:yield:
85
+ if block_given?
86
+ super(yield)
87
+ else
88
+ super
89
+ end
90
+ end
91
+
92
+ # Allows eql? to take a block.
93
+ def eql?(value=NoArgument) #:yield:
94
+ if block_given?
95
+ super(yield)
96
+ else
97
+ super
98
+ end
99
+ end
100
+ end
101
+
102
+ class Numeric
103
+ # Is self and given number within delta tolerance.
104
+ #
105
+ # 0.05.in_delta?(50000.0 / 10**6, 0.00001)
106
+ #
107
+ def in_delta?(orig, delta=0.001)
108
+ #(num.to_f - to_f).abs <= delta.to_f
109
+ delta >= (orig - self).abs
110
+ end
111
+
112
+ # Alias for #in_delta.
113
+ alias_method :close?, :in_delta?
114
+
115
+ # Verify epsilon tolerance.
116
+ def in_epsilon?(orig, epsilon=0.001)
117
+ in_delta?(orig, [orig, self].min * epsilon)
118
+ end
119
+ end
120
+
121
+
122
+ class Module
123
+ # Is a given class or module an ancestor of this
124
+ # class or module?
125
+ #
126
+ # class X ; end
127
+ # class Y < X ; end
128
+ #
129
+ # Y.is?(X) #=> true
130
+ #
131
+ def is?(base)
132
+ Module===base && ancestors.slice(1..-1).include?(base)
133
+ end
134
+ end
135
+
136
+ class Proc
137
+ #
138
+ def raises?(exception=Exception, *args)
139
+ begin
140
+ call(*args)
141
+ false
142
+ rescue exception => error
143
+ exception === error
144
+ end
145
+ end
146
+
147
+ #
148
+ def throws?(sym, *args)
149
+ catch(sym) do
150
+ begin
151
+ call(*args)
152
+ rescue ArgumentError # 1.9 exception
153
+ rescue NameError # 1.8 exception
154
+ end
155
+ return false
156
+ end
157
+ return true
158
+ end
159
+
160
+ # TODO: Put in facets?
161
+ # TODO: wrong place, change yield?
162
+ def change?
163
+ pre_result = yield
164
+ called = call
165
+ post_result = yield
166
+ pre_result != post_result
167
+ end
168
+ end
169
+
170
+ class Symbol
171
+ # Does the block throw this symbol?
172
+ #
173
+ def thrown?(*args)
174
+ catch(self) do
175
+ begin
176
+ yield(*args)
177
+ rescue ArgumentError # 1.9 exception
178
+ rescue NameError # 1.8 exception
179
+ end
180
+ return false
181
+ end
182
+ return true
183
+ end
184
+ end
185
+
186
+ class Exception
187
+ #
188
+ def self.raised? #:yeild:
189
+ begin
190
+ yield
191
+ false
192
+ rescue self
193
+ true
194
+ end
195
+ end
196
+ end
197
+
198
+ # Copyright (c) 2008,2009 Thomas Sawyer