identifies_as 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -7,3 +7,4 @@ Initial release.
7
7
 
8
8
  Renamed :no_longer_identifying_as! and :instances_no_longer_identify_as! to :stop_identifying_as! and :stop_instances_identifying_as!
9
9
  Added support for subclassing and module inclusion.
10
+ Added Module-level support for :<, :>, :<=, :>=, :<==>.
@@ -0,0 +1,180 @@
1
+
2
+ module ::IdentifiesAs::ClassInstance
3
+
4
+ ############################
5
+ # instances_identify_as! #
6
+ ############################
7
+
8
+ # Cause instances of receiver to identify as specified objects.
9
+ # @param [Array<Object>] objects Other objects self should identify as.
10
+ # @return [Object] Self.
11
+ def instances_identify_as!( *objects )
12
+
13
+ return ::IdentifiesAs.object_instances_identify_as!( self, *objects )
14
+
15
+ end
16
+
17
+ ############################
18
+ # instances_identify_as? #
19
+ ############################
20
+
21
+ # Query whether instances of receiver identify as specified object.
22
+ # @param [Object] object Object against which identity is being tested.
23
+ # @return [true,false] Whether instances of receiver identify as object.
24
+ def instances_identify_as?( object )
25
+
26
+ return ::IdentifiesAs.object_instances_identify_as?( self, object )
27
+
28
+ end
29
+
30
+
31
+ #########################
32
+ # instance_identities #
33
+ #########################
34
+
35
+ # Identities that instances of receiver identify as, beyond those which it actually is.
36
+ # @return [Array<Object>] Identities instances of receiver identifies as.
37
+ def instance_identities
38
+
39
+ return ::IdentifiesAs.object_instance_identities( self )
40
+
41
+ end
42
+
43
+ ####################################
44
+ # stop_instances_identifying_as! #
45
+ ####################################
46
+
47
+ # Cause instances of receiver to no longer identify as specified objects.
48
+ # @param [Array<Object>] objects Other objects instance of receiver should no longer identify as.
49
+ # @return [Object] Self.
50
+ def stop_instances_identifying_as!( *objects )
51
+
52
+ return ::IdentifiesAs.object_stop_instances_identifying_as!( self, *objects )
53
+
54
+ end
55
+
56
+ #########
57
+ # === #
58
+ #########
59
+
60
+ def ===( object )
61
+
62
+ is_equal = false
63
+
64
+ if ::IdentifiesAs.object_identifies_as?( object, self )
65
+ is_equal = true
66
+ else
67
+ is_equal = super
68
+ end
69
+
70
+ return is_equal
71
+
72
+ end
73
+
74
+ #######
75
+ # < #
76
+ #######
77
+
78
+ def <( object )
79
+
80
+ is_less_than = super
81
+
82
+ if is_less_than.nil?
83
+ # a class is < another class if it has the other class as one of its ancestors
84
+ if self != object and ::IdentifiesAs.object_instance_identifies_as?( self, object )
85
+ is_less_than = true
86
+ end
87
+ end
88
+
89
+ return is_less_than
90
+
91
+ end
92
+
93
+ #######
94
+ # > #
95
+ #######
96
+
97
+ def >( object )
98
+
99
+ is_greater_than = super
100
+
101
+ if is_greater_than.nil?
102
+ # a class is > another class if the other class has it as one of its ancestors
103
+ if self != object and ::IdentifiesAs.object_instance_identifies_as?( object, self )
104
+ is_greater_than = true
105
+ else
106
+ is_less_than = self < object
107
+ unless is_less_than.nil?
108
+ is_greater_than = ! is_less_than
109
+ end
110
+ end
111
+ end
112
+
113
+ return is_greater_than
114
+
115
+ end
116
+
117
+ ########
118
+ # <= #
119
+ ########
120
+
121
+ def <=( object )
122
+
123
+ is_less_than_or_equal_to = super
124
+
125
+ if is_less_than_or_equal_to.nil?
126
+ # a class is < another class if it has the other class as one of its ancestors
127
+ if self == object or ::IdentifiesAs.object_instance_identifies_as?( self, object )
128
+ is_less_than_or_equal_to = true
129
+ end
130
+ end
131
+
132
+ return is_less_than_or_equal_to
133
+
134
+ end
135
+
136
+ ########
137
+ # >= #
138
+ ########
139
+
140
+ def >=( object )
141
+
142
+ is_greater_than_or_equal_to = super
143
+
144
+ if is_greater_than_or_equal_to.nil?
145
+ # a class is < another class if it has the other class as one of its ancestors
146
+ if self == object or ::IdentifiesAs.object_instance_identifies_as?( object, self )
147
+ is_greater_than_or_equal_to = true
148
+ else
149
+ is_less_than_or_equal_to = self <= object
150
+ unless is_less_than_or_equal_to.nil?
151
+ is_greater_than_or_equal_to = ! is_less_than_or_equal_to
152
+ end
153
+ end
154
+ end
155
+
156
+ return is_greater_than_or_equal_to
157
+
158
+ end
159
+
160
+ #########
161
+ # <=> #
162
+ #########
163
+
164
+ def <=>( object )
165
+
166
+ less_than_equal_to_greater_than = nil
167
+
168
+ if self == object
169
+ less_than_equal_to_greater_than = 0
170
+ elsif self < object
171
+ less_than_equal_to_greater_than = -1
172
+ elsif self > object
173
+ less_than_equal_to_greater_than = 1
174
+ end
175
+
176
+ return less_than_equal_to_greater_than
177
+
178
+ end
179
+
180
+ end
@@ -0,0 +1,75 @@
1
+
2
+ module ::IdentifiesAs::ObjectInstance
3
+
4
+ include ::IdentifiesAs::ActuallyIsA
5
+
6
+ ####################
7
+ # identifies_as! #
8
+ ####################
9
+
10
+ # Cause receiver to identify as specified objects.
11
+ # @param [Array<Object>] objects Other objects self should identify as.
12
+ # @return [Object] Self.
13
+ def identifies_as!( *objects )
14
+
15
+ return ::IdentifiesAs.object_identifies_as!( self, *objects )
16
+
17
+ end
18
+
19
+ ####################
20
+ # identifies_as? #
21
+ ####################
22
+
23
+ # Query whether receiver identifies as specified object.
24
+ # @param [Object] object Object against which identity is being tested.
25
+ # @return [true,false] Whether receiver identifies as object.
26
+ def identifies_as?( object )
27
+
28
+ return ::IdentifiesAs.object_identifies_as?( self, object )
29
+
30
+ end
31
+
32
+ ################
33
+ # identities #
34
+ ################
35
+
36
+ # Identities that receiver identifies as, beyond those which it actually is.
37
+ # @return [Array<Object>] Identities receiver identifies as.
38
+ def identities
39
+
40
+ return ::IdentifiesAs.object_identities( self )
41
+
42
+ end
43
+
44
+ ##########################
45
+ # stop_identifying_as! #
46
+ ##########################
47
+
48
+ # Cause receiver to no longer identify as specified objects.
49
+ # @param [Array<Object>] objects Other objects receiver should no longer identify as.
50
+ # @return [Object] Self.
51
+ def stop_identifying_as!( *objects )
52
+
53
+ return ::IdentifiesAs.object_stop_identifying_as!( self, *objects )
54
+
55
+ end
56
+
57
+ ###########
58
+ # is_a? #
59
+ ###########
60
+
61
+ def is_a?( object )
62
+
63
+ is_type = false
64
+
65
+ if identifies_as?( object )
66
+ is_type = true
67
+ else
68
+ is_type = super
69
+ end
70
+
71
+ return is_type
72
+
73
+ end
74
+
75
+ end
@@ -3,7 +3,8 @@ module ::IdentifiesAs
3
3
 
4
4
  extend ::ModuleCluster::Define::Cluster
5
5
 
6
- include_also_extends( self )
6
+ include self::ObjectInstance
7
+ include_also_extends( self::ClassInstance )
7
8
 
8
9
  @identities = { }
9
10
  @instance_identities = { }
@@ -54,6 +55,22 @@ module ::IdentifiesAs
54
55
 
55
56
  def self.object_identifies_as?( object, other_object_type )
56
57
 
58
+ object_identifies = object_instance_identifies_as?( object, other_object_type )
59
+
60
+ unless object_identifies
61
+ object_identifies = object_instances_identify_as?( object.class, other_object_type )
62
+ end
63
+
64
+ return object_identifies
65
+
66
+ end
67
+
68
+ #########################################
69
+ # self.object_instance_identifies_as? #
70
+ #########################################
71
+
72
+ def self.object_instance_identifies_as?( object, other_object_type )
73
+
57
74
  object_identifies = false
58
75
 
59
76
  object_ancestor_chain = nil
@@ -76,10 +93,6 @@ module ::IdentifiesAs
76
93
  end
77
94
 
78
95
  end
79
-
80
- unless object_identifies
81
- object_identifies = object_instances_identify_as?( object.class, other_object_type )
82
- end
83
96
 
84
97
  return object_identifies
85
98
 
@@ -159,9 +172,9 @@ module ::IdentifiesAs
159
172
 
160
173
  end
161
174
 
162
- ##################################################
175
+ ################################################
163
176
  # self.object_stop_instances_identifying_as! #
164
- ##################################################
177
+ ################################################
165
178
 
166
179
  def self.object_stop_instances_identifying_as!( object_class, *objects )
167
180
 
@@ -175,125 +188,5 @@ module ::IdentifiesAs
175
188
  return identifies_as_hash
176
189
 
177
190
  end
178
-
179
- ####################
180
- # identifies_as! #
181
- ####################
182
-
183
- # Cause receiver to identify as specified objects.
184
- # @param [Array<Object>] objects Other objects self should identify as.
185
- # @return [Object] Self.
186
- def identifies_as!( *objects )
187
-
188
- return ::IdentifiesAs.object_identifies_as!( self, *objects )
189
-
190
- end
191
-
192
- ############################
193
- # instances_identify_as! #
194
- ############################
195
-
196
- # Cause instances of receiver to identify as specified objects.
197
- # @param [Array<Object>] objects Other objects self should identify as.
198
- # @return [Object] Self.
199
- def instances_identify_as!( *objects )
200
-
201
- return ::IdentifiesAs.object_instances_identify_as!( self, *objects )
202
-
203
- end
204
-
205
- ####################
206
- # identifies_as? #
207
- ####################
208
-
209
- # Query whether receiver identifies as specified object.
210
- # @param [Object] object Object against which identity is being tested.
211
- # @return [true,false] Whether receiver identifies as object.
212
- def identifies_as?( object )
213
-
214
- return ::IdentifiesAs.object_identifies_as?( self, object )
215
-
216
- end
217
-
218
- ############################
219
- # instances_identify_as? #
220
- ############################
221
-
222
- # Query whether instances of receiver identify as specified object.
223
- # @param [Object] object Object against which identity is being tested.
224
- # @return [true,false] Whether instances of receiver identify as object.
225
- def instances_identify_as?( object )
226
-
227
- return ::IdentifiesAs.object_instances_identify_as?( self, object )
228
-
229
- end
230
-
231
- ################
232
- # identities #
233
- ################
234
-
235
- # Identities that receiver identifies as, beyond those which it actually is.
236
- # @return [Array<Object>] Identities receiver identifies as.
237
- def identities
238
-
239
- return ::IdentifiesAs.object_identities( self )
240
-
241
- end
242
-
243
- #########################
244
- # instance_identities #
245
- #########################
246
-
247
- # Identities that instances of receiver identify as, beyond those which it actually is.
248
- # @return [Array<Object>] Identities instances of receiver identifies as.
249
- def instance_identities
250
-
251
- return ::IdentifiesAs.object_instance_identities( self )
252
-
253
- end
254
-
255
- ##############################
256
- # stop_identifying_as! #
257
- ##############################
258
-
259
- # Cause receiver to no longer identify as specified objects.
260
- # @param [Array<Object>] objects Other objects receiver should no longer identify as.
261
- # @return [Object] Self.
262
- def stop_identifying_as!( *objects )
263
-
264
- return ::IdentifiesAs.object_stop_identifying_as!( self, *objects )
265
-
266
- end
267
-
268
- ####################################
269
- # stop_instances_identifying_as! #
270
- ####################################
271
-
272
- # Cause instances of receiver to no longer identify as specified objects.
273
- # @param [Array<Object>] objects Other objects instance of receiver should no longer identify as.
274
- # @return [Object] Self.
275
- def stop_instances_identifying_as!( *objects )
276
-
277
- return ::IdentifiesAs.object_stop_instances_identifying_as!( self, *objects )
278
-
279
- end
280
-
281
- ###########
282
- # is_a? #
283
- ###########
284
-
285
- def is_a?( object )
286
-
287
- is_type = false
288
-
289
- if identifies_as?( object )
290
- is_type = true
291
- else
292
- is_type = super
293
- end
294
-
295
- return is_type
296
-
297
- end
298
-
191
+
299
192
  end
data/lib/identifies_as.rb CHANGED
@@ -1,12 +1,26 @@
1
1
 
2
2
  require 'module-cluster'
3
3
 
4
- require_relative 'identifies_as/IdentifiesAs.rb'
5
- require_relative 'identifies_as/IdentifiesAs/ActuallyIsA.rb'
6
- require_relative 'identifies_as/IdentifiesAs/EqualsEqualsEquals.rb'
4
+ module ::IdentifiesAs
5
+ end
6
+
7
+ basepath = 'identifies_as/IdentifiesAs'
8
+
9
+ files = [
10
+
11
+ 'ActuallyIsA',
12
+
13
+ 'ClassInstance',
14
+ 'ObjectInstance'
15
+
16
+ ]
17
+
18
+ files.each do |this_file|
19
+ require_relative( File.join( basepath, this_file ) + '.rb' )
20
+ end
21
+
22
+ require_relative( basepath + '.rb' )
7
23
 
8
24
  class ::Object
9
- extend ::IdentifiesAs::EqualsEqualsEquals
10
- include ::IdentifiesAs::ActuallyIsA
11
- extend ::IdentifiesAs::ActuallyIsA
25
+ include ::IdentifiesAs
12
26
  end
@@ -31,7 +31,7 @@ describe ::IdentifiesAs do
31
31
  instance.identities.should == [ ]
32
32
  instance.is_a?( Array ).should == false
33
33
  ( Array === instance ).should == false
34
-
34
+
35
35
  end
36
36
 
37
37
  ####################################
@@ -60,7 +60,32 @@ describe ::IdentifiesAs do
60
60
  class AlsoNotAnArray < NotAnArray
61
61
  end
62
62
 
63
+ module Unrelated
64
+ end
65
+
63
66
  NotAnArray.is_a?( SomeOtherClass ).should == true
67
+ ( NotAnArray < Array ).should == true
68
+ ( NotAnArray > Array ).should == false
69
+ ( NotAnArray <= Array ).should == true
70
+ ( NotAnArray >= Array ).should == false
71
+
72
+ ( NotAnArray <=> Array ).should == -1
73
+ ( Array <=> NotAnArray ).should == 1
74
+ ( NotAnArray <=> NotAnArray ).should == 0
75
+ ( NotAnArray <=> Unrelated ).should == nil
76
+
77
+ AlsoNotAnArray.is_a?( SomeOtherClass ).should == true
78
+ ( AlsoNotAnArray < Array ).should == true
79
+ ( AlsoNotAnArray > Array ).should == false
80
+ ( AlsoNotAnArray <= Array ).should == true
81
+ ( AlsoNotAnArray >= Array ).should == false
82
+
83
+ ( AlsoNotAnArray <=> NotAnArray ).should == -1
84
+ ( NotAnArray <=> AlsoNotAnArray ).should == 1
85
+ ( AlsoNotAnArray <=> Array ).should == -1
86
+ ( Array <=> AlsoNotAnArray ).should == 1
87
+ ( AlsoNotAnArray <=> AlsoNotAnArray ).should == 0
88
+ ( AlsoNotAnArray <=> Unrelated ).should == nil
64
89
 
65
90
  instance = NotAnArray.new
66
91
  instance.identifies_as?( Array ).should == true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identifies_as
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,7 +37,8 @@ extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
39
  - lib/identifies_as/IdentifiesAs/ActuallyIsA.rb
40
- - lib/identifies_as/IdentifiesAs/EqualsEqualsEquals.rb
40
+ - lib/identifies_as/IdentifiesAs/ClassInstance.rb
41
+ - lib/identifies_as/IdentifiesAs/ObjectInstance.rb
41
42
  - lib/identifies_as/IdentifiesAs.rb
42
43
  - lib/identifies_as.rb
43
44
  - spec/IdentifiesAs_spec.rb
@@ -1,22 +0,0 @@
1
-
2
- module ::IdentifiesAs::EqualsEqualsEquals
3
-
4
- #########
5
- # === #
6
- #########
7
-
8
- def ===( object )
9
-
10
- is_equal = false
11
-
12
- if ::IdentifiesAs.object_identifies_as?( object, self )
13
- is_equal = true
14
- else
15
- is_equal = super
16
- end
17
-
18
- return is_equal
19
-
20
- end
21
-
22
- end