identifies_as 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,4 @@
1
+
2
+ == 6/12/2012
3
+
4
+ Initial release.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Identifies As #
2
+
3
+ http://rubygems.org/gems/identifies_as
4
+
5
+ # Summary #
6
+
7
+ Permits objects to identify as if they are other types of objects. Effects :is_a? and :===.
8
+
9
+ # Description #
10
+
11
+ Provides IdentifiesAs, which offers :identifies_as!, :instances_identify_as!, :identifies_as?, :instances_identify_as?, :no_longer_identifies_as!, :instances_no_longer_identify_as, :identities, :instance_identities, and overrides :is_a? and :===.
12
+
13
+ # Install #
14
+
15
+ * sudo gem install identifies_as
16
+
17
+ # Usage #
18
+
19
+ * Example One:
20
+
21
+ ```ruby
22
+
23
+ class SomeOtherClass
24
+ end
25
+
26
+ class NotAnArray
27
+ include IdentifiesAs
28
+ instances_identify_as!( Array )
29
+ identifies_as!( SomeOtherClass )
30
+ end
31
+
32
+ NotAnArray.is_a?( SomeOtherClass )
33
+ # => true
34
+
35
+ instance = NotAnArray.new
36
+
37
+ instance.identifies_as?( Array )
38
+ # => true
39
+
40
+ instance.is_a?( Array )
41
+ # => true
42
+
43
+ Array === instance
44
+ # => true
45
+ ```
46
+
47
+ * Example Two:
48
+
49
+ ```ruby
50
+ instance = Object.new
51
+
52
+ instance.extend( IdentifiesAs )
53
+
54
+ instance.identifies_as!( Array )
55
+
56
+ instance.identifies_as?( Array )
57
+ # => true
58
+
59
+ instance.is_a?( Array )
60
+ # => true
61
+
62
+ Array === instance
63
+ # => true
64
+ ```
65
+
66
+ # License #
67
+
68
+ (The MIT License)
69
+
70
+ Copyright (c) Asher
71
+
72
+ Permission is hereby granted, free of charge, to any person obtaining
73
+ a copy of this software and associated documentation files (the
74
+ 'Software'), to deal in the Software without restriction, including
75
+ without limitation the rights to use, copy, modify, merge, publish,
76
+ distribute, sublicense, and/or sell copies of the Software, and to
77
+ permit persons to whom the Software is furnished to do so, subject to
78
+ the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be
81
+ included in all copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
84
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
85
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
86
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
87
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
88
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
89
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,85 @@
1
+ == Identifies As
2
+
3
+ http://rubygems.org/gems/identifies_as
4
+
5
+ == Summary
6
+
7
+ Permits objects to identify as if they are other types of objects. Effects :is_a? and :===.
8
+
9
+ == Description
10
+
11
+ Provides IdentifiesAs, which offers :identifies_as!, :instances_identify_as!, :identifies_as?, :instances_identify_as?, :no_longer_identifies_as!, :instances_no_longer_identify_as, :identities, :instance_identities, and overrides :is_a? and :===.
12
+
13
+ == Install
14
+
15
+ * sudo gem install identifies_as
16
+
17
+ == Usage
18
+
19
+ * Example One:
20
+
21
+ class SomeOtherClass
22
+ end
23
+
24
+ class NotAnArray
25
+ include IdentifiesAs
26
+ instances_identify_as!( Array )
27
+ identifies_as!( SomeOtherClass )
28
+ end
29
+
30
+ NotAnArray.is_a?( SomeOtherClass )
31
+ # => true
32
+
33
+ instance = NotAnArray.new
34
+
35
+ instance.identifies_as?( Array )
36
+ # => true
37
+
38
+ instance.is_a?( Array )
39
+ # => true
40
+
41
+ Array === instance
42
+ # => true
43
+
44
+ * Example Two:
45
+
46
+ instance = Object.new
47
+
48
+ instance.extend( IdentifiesAs )
49
+
50
+ instance.identifies_as!( Array )
51
+
52
+ instance.identifies_as?( Array )
53
+ # => true
54
+
55
+ instance.is_a?( Array )
56
+ # => true
57
+
58
+ Array === instance
59
+ # => true
60
+
61
+
62
+ == License
63
+
64
+ (The MIT License)
65
+
66
+ Copyright (c) Asher
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ 'Software'), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
82
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
83
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
84
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+
2
+ module ::IdentifiesAs::EqualsEqualsEquals
3
+
4
+ #########
5
+ # === #
6
+ #########
7
+
8
+ def ===( object )
9
+
10
+ is_equal = false
11
+
12
+ if identifies_as_hash = ::IdentifiesAs.object_identities_hash( object )
13
+ identifies_as_hash.each do |this_object, true_value|
14
+ if super( this_object ) or ( self < this_object ) != nil
15
+ is_equal = true
16
+ break
17
+ end
18
+ end
19
+ end
20
+
21
+ if identifies_as_hash = ::IdentifiesAs.object_instance_identities_hash( object.class )
22
+ identifies_as_hash.each do |this_object, true_value|
23
+ if super( this_object ) or ( self < this_object ) != nil
24
+ is_equal = true
25
+ break
26
+ end
27
+ end
28
+ end
29
+
30
+ unless is_equal
31
+ is_equal = super
32
+ end
33
+
34
+ return is_equal
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,290 @@
1
+
2
+ module ::IdentifiesAs
3
+
4
+ extend ::ModuleCluster::Define::Cluster
5
+
6
+ include_also_extends( self )
7
+
8
+ @identities = { }
9
+ @instance_identities = { }
10
+
11
+ ################################
12
+ # self.object_identifies_as! #
13
+ ################################
14
+
15
+ def self.object_identifies_as!( object, *objects )
16
+
17
+ object_id = object.__id__
18
+
19
+ unless identifies_as_hash = @identities[ object_id ]
20
+ identifies_as_hash = { }
21
+ @identities[ object_id ] = identifies_as_hash
22
+ end
23
+
24
+ objects.each do |this_object|
25
+ identifies_as_hash[ this_object ] = true
26
+ end
27
+
28
+ return identifies_as_hash
29
+
30
+ end
31
+
32
+ ########################################
33
+ # self.object_instances_identify_as! #
34
+ ########################################
35
+
36
+ def self.object_instances_identify_as!( object_class, *objects )
37
+
38
+ unless identifies_as_hash = @instance_identities[ object_class ]
39
+ identifies_as_hash = { }
40
+ @instance_identities[ object_class ] = identifies_as_hash
41
+ end
42
+
43
+ objects.each do |this_object|
44
+ identifies_as_hash[ this_object ] = true
45
+ end
46
+
47
+ return identifies_as_hash
48
+
49
+ end
50
+
51
+ ################################
52
+ # self.object_identifies_as? #
53
+ ################################
54
+
55
+ def self.object_identifies_as?( object, other_object_type )
56
+
57
+ object_identifies = false
58
+
59
+ if identifies_as_hash = @identities[ object.__id__ ]
60
+ object_identifies = identifies_as_hash.has_key?( other_object_type )
61
+ end
62
+
63
+ unless object_identifies
64
+ object_identifies = object_instances_identify_as?( object.class, other_object_type )
65
+ end
66
+
67
+ return object_identifies
68
+
69
+ end
70
+
71
+ ########################################
72
+ # self.object_instances_identify_as? #
73
+ ########################################
74
+
75
+ def self.object_instances_identify_as?( object_class, other_object_type )
76
+
77
+ object_identifies = false
78
+
79
+ if identifies_as_hash = @instance_identities[ object_class ]
80
+ object_identifies = identifies_as_hash.has_key?( other_object_type )
81
+ end
82
+
83
+ return object_identifies
84
+
85
+ end
86
+
87
+ ############################
88
+ # self.object_identities #
89
+ ############################
90
+
91
+ def self.object_identities( object )
92
+
93
+ return object_identities_hash( object ).keys
94
+
95
+ end
96
+
97
+ #####################################
98
+ # self.object_instance_identities #
99
+ #####################################
100
+
101
+ def self.object_instance_identities( object_class )
102
+
103
+ return object_instance_identities_hash( object_class ).keys
104
+
105
+ end
106
+
107
+ #################################
108
+ # self.object_identities_hash #
109
+ #################################
110
+
111
+ def self.object_identities_hash( object )
112
+
113
+ return @identities[ object.__id__ ]
114
+
115
+ end
116
+
117
+ ##########################################
118
+ # self.object_instance_identities_hash #
119
+ ##########################################
120
+
121
+ def self.object_instance_identities_hash( object_class )
122
+
123
+ return @instance_identities[ object_class ]
124
+
125
+ end
126
+
127
+ ##########################################
128
+ # self.object_no_longer_identifies_as! #
129
+ ##########################################
130
+
131
+ def self.object_no_longer_identifies_as!( object, *objects )
132
+
133
+ if identifies_as_hash = @identities[ object ]
134
+ object_identifies = identifies_as_hash.has_key?( object )
135
+ objects.each do |this_object|
136
+ object_identifies.delete( this_object.__id__ )
137
+ end
138
+ end
139
+
140
+ return identifies_as_hash
141
+
142
+ end
143
+
144
+ ##################################################
145
+ # self.object_instances_no_longer_identify_as! #
146
+ ##################################################
147
+
148
+ def self.object_instances_no_longer_identify_as!( object_class, *objects )
149
+
150
+ if identifies_as_hash = @instance_identities[ object_class ]
151
+ object_identifies = identifies_as_hash.has_key?( object_class )
152
+ objects.each do |this_object|
153
+ object_identifies.delete( this_object )
154
+ end
155
+ end
156
+
157
+ return identifies_as_hash
158
+
159
+ end
160
+
161
+ ####################
162
+ # identifies_as! #
163
+ ####################
164
+
165
+ # Cause receiver to identify as specified objects.
166
+ # @param [Array<Object>] objects Other objects self should identify as.
167
+ # @return [Object] Self.
168
+ def identifies_as!( *objects )
169
+
170
+ return ::IdentifiesAs.object_identifies_as!( self, *objects )
171
+
172
+ end
173
+
174
+ ############################
175
+ # instances_identify_as! #
176
+ ############################
177
+
178
+ # Cause instances of receiver to identify as specified objects.
179
+ # @param [Array<Object>] objects Other objects self should identify as.
180
+ # @return [Object] Self.
181
+ def instances_identify_as!( *objects )
182
+
183
+ return ::IdentifiesAs.object_instances_identify_as!( self, *objects )
184
+
185
+ end
186
+
187
+ ####################
188
+ # identifies_as? #
189
+ ####################
190
+
191
+ # Query whether receiver identifies as specified object.
192
+ # @param [Object] object Object against which identity is being tested.
193
+ # @return [true,false] Whether receiver identifies as object.
194
+ def identifies_as?( object )
195
+
196
+ return ::IdentifiesAs.object_identifies_as?( self, object )
197
+
198
+ end
199
+
200
+ ############################
201
+ # instances_identify_as? #
202
+ ############################
203
+
204
+ # Query whether instances of receiver identify as specified object.
205
+ # @param [Object] object Object against which identity is being tested.
206
+ # @return [true,false] Whether instances of receiver identify as object.
207
+ def instances_identify_as?( object )
208
+
209
+ return ::IdentifiesAs.object_instances_identify_as?( self, object )
210
+
211
+ end
212
+
213
+ ################
214
+ # identities #
215
+ ################
216
+
217
+ # Identities that receiver identifies as, beyond those which it actually is.
218
+ # @return [Array<Object>] Identities receiver identifies as.
219
+ def identities
220
+
221
+ return ::IdentifiesAs.object_identities( self )
222
+
223
+ end
224
+
225
+ #########################
226
+ # instance_identities #
227
+ #########################
228
+
229
+ # Identities that instances of receiver identify as, beyond those which it actually is.
230
+ # @return [Array<Object>] Identities instances of receiver identifies as.
231
+ def instance_identities
232
+
233
+ return ::IdentifiesAs.object_instance_identities( self )
234
+
235
+ end
236
+
237
+ ##############################
238
+ # no_longer_identifies_as! #
239
+ ##############################
240
+
241
+ # Cause receiver to no longer identify as specified objects.
242
+ # @param [Array<Object>] objects Other objects receiver should no longer identify as.
243
+ # @return [Object] Self.
244
+ def no_longer_identifies_as!( *objects )
245
+
246
+ return ::IdentifiesAs.object_no_longer_identifies_as!( self, *objects )
247
+
248
+ end
249
+
250
+ ######################################
251
+ # instances_no_longer_identify_as! #
252
+ ######################################
253
+
254
+ # Cause instances of receiver to no longer identify as specified objects.
255
+ # @param [Array<Object>] objects Other objects instance of receiver should no longer identify as.
256
+ # @return [Object] Self.
257
+ def no_longer_identifies_as!( *objects )
258
+
259
+ return ::IdentifiesAs.object_instances_no_longer_identify_as!( self, *objects )
260
+
261
+ end
262
+
263
+ ####################
264
+ # actually_is_a? #
265
+ ####################
266
+
267
+ # Alias to the original :is_a? method without IdentifyAs functionality.
268
+ # @param [Object] objects Other object to test identity against.
269
+ # @return [true,false] Whether receiver is actually instance of specified object.
270
+ alias_method :actually_is_a?, :is_a?
271
+
272
+ ###########
273
+ # is_a? #
274
+ ###########
275
+
276
+ def is_a?( object )
277
+
278
+ is_type = false
279
+
280
+ if identifies_as?( object )
281
+ is_type = true
282
+ else
283
+ is_type = super
284
+ end
285
+
286
+ return is_type
287
+
288
+ end
289
+
290
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require 'module-cluster'
3
+
4
+ require_relative 'identifies_as/IdentifiesAs.rb'
5
+ require_relative 'identifies_as/IdentifiesAs/EqualsEqualsEquals.rb'
6
+
7
+ class ::Object
8
+ extend ::IdentifiesAs::EqualsEqualsEquals
9
+ end
@@ -0,0 +1,48 @@
1
+
2
+ require_relative '../lib/identifies_as.rb'
3
+
4
+ describe ::IdentifiesAs do
5
+
6
+ ######################################
7
+ # identifies_as! #
8
+ # identifies_as? #
9
+ # instances_identify_as! #
10
+ # instances_identify_as? #
11
+ # identities #
12
+ # no_longer_identifies_as! #
13
+ # instances_no_longer_identify_as! #
14
+ # is_a? #
15
+ # === #
16
+ ######################################
17
+
18
+ it 'lets an object pretend it is another object' do
19
+
20
+ instance = Object.new
21
+ instance.extend( IdentifiesAs )
22
+
23
+ instance.identifies_as!( Array )
24
+ instance.identifies_as?( Array ).should == true
25
+ instance.identities.should == [ Array ]
26
+
27
+ instance.is_a?( Array ).should == true
28
+ ( Array === instance ).should == true
29
+
30
+ class SomeOtherClass
31
+ end
32
+ class NotAnArray
33
+ include IdentifiesAs
34
+ identifies_as!( Array )
35
+ instances_identify_as!( Array )
36
+ identifies_as!( SomeOtherClass )
37
+ end
38
+
39
+ NotAnArray.is_a?( SomeOtherClass ).should == true
40
+
41
+ instance = NotAnArray.new
42
+ instance.identifies_as?( Array ).should == true
43
+ instance.is_a?( Array ).should == true
44
+ ( Array === instance ).should == true
45
+
46
+ end
47
+
48
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: identifies_as
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Asher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: module-cluster
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Provides IdentifiesAs, which offers :identifies_as!, :identifies_as?,
31
+ :no_longer_identifies_as! and :identities, and overrides :is_a? and :===.
32
+ email: asher@ridiculouspower.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/identifies_as/IdentifiesAs/EqualsEqualsEquals.rb
38
+ - lib/identifies_as/IdentifiesAs.rb
39
+ - lib/identifies_as.rb
40
+ - spec/IdentifiesAs_spec.rb
41
+ - README.md
42
+ - README.rdoc
43
+ - CHANGELOG.rdoc
44
+ homepage: http://rubygems.org/gems/identifies_as
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: identifies_as
64
+ rubygems_version: 1.8.23
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Permits objects to identify as if they are other types of objects. Effects
68
+ :is_a? and :===.
69
+ test_files: []
70
+ has_rdoc: