Class: Object

Inherits:
BasicObject
Defined in:
app/models/global.rb,
app/models/global.rb

Overview

module

Instance Method Summary collapse

Instance Method Details

#canonicalNameObject


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/global.rb', line 126

def canonicalName
	#~ puts "inspect=#{inspect}"
	#~ puts "respond_to?(:to_s)=#{respond_to?(:to_s)}"
	if nil? then
		return "nil"
	elsif instance_of?(Class) then
		return "Class #{name}"
	elsif self.class.name=='Symbol' then
		return "Symbol :#{to_s}"
	elsif self.class.name=='Module' then
		if name=='' then
			return "Module Id:#{sprintf("%X",object_id)} in (#{ancestors.inspect})"
		else
			return "Module #{name}"
		end #if
	elsif self.class.name=='ActiveRecord::Base' then
		return "ActiveRecord::Base #{self.class.name}"		
	elsif instance_of?(Array) then
		return "Array instance"
	elsif !respond_to?(:to_s) then
		return "#{self.class.name} does not respond to :to_s"
	elsif to_s.nil? then
		return "#{self.class.name} to_s returns nil"
	elsif to_s.empty? then
		return "#{self.class.name} to_s returns empty"
	elsif !to_s[/#<ActiveRecord::Relation:/].nil? then
		if !to_s[/#<ActiveRecord::Relation:/].empty? then
			return "#{self.class.name} #<ActiveRecord::Relation:"
		else
			return "#{self.class.name}.!to_s[/#<ActiveRecord::Relation:/] is not nil"
		end
	else
		if respond_to?(:name) then
			if respond_to?(:superclass) then
				return "Class #{name} subclass of #{superclass.name}"
			else
				return "obj Class #{name} has no superclass."
			end
		else
			return "inspect=#{inspect} has no name."
		end
	end

end

#context_names(depth = 0) ⇒ Object

def


298
299
300
# File 'app/models/global.rb', line 298

def context_names(depth=0)
	method_contexts(depth).map{|c| c.canonicalName}
end

#enumerate(enumerator_method, &proc) ⇒ Object

enumerate_single


24
25
26
27
28
29
30
# File 'app/models/global.rb', line 24

def enumerate(enumerator_method, &proc)
	if instance_of?(Array) then
		method(enumerator_method).call(&proc)
	else
		enumerate_single(enumerator_method, &proc)		
	end #if
end

#enumerate_single(enumerator_method, &proc) ⇒ Object


16
17
18
19
20
21
22
23
# File 'app/models/global.rb', line 16

def enumerate_single(enumerator_method, &proc)
	result=[self].enumerate(enumerator_method, &proc) #simulate array
	if result.instance_of?(Array) then # map
		return result[0] #discard simulated array
	else # reduction method (not map)
		return result
	end #if
end

#info(message) ⇒ Object


63
64
65
66
67
# File 'app/models/global.rb', line 63

def info(message)
	if $VERBOSE then
		$stdout.puts message
	end #if
end

#matching_methods_in_context(regexp, depth = 0) ⇒ Object

def


308
309
310
311
312
313
314
315
# File 'app/models/global.rb', line 308

def matching_methods_in_context(regexp,depth=0)
	ret={}
	method_contexts(depth).map do |context|
		instance_meths=context.matching_instance_methods(regexp)
		ret[context.canonicalName]=instance_meths # label level
	end #map
	ret
end

#method_context(methodName) ⇒ Object

def


301
302
303
304
305
306
307
# File 'app/models/global.rb', line 301

def method_context(methodName)
	if respond_to(methodName) then
		matching_methods_in_context(methodName)
	else
		nil
	end #if
end

#method_contexts(depth = 0) ⇒ Object

def


291
292
293
294
295
296
297
# File 'app/models/global.rb', line 291

def method_contexts(depth=0)
	if instance_of?(Class) then
		ancestors[0..depth].uniq
	else
		[self]+self.class.ancestors[0..depth].uniq
	end #if
end

#module?Boolean

relationship


274
275
276
# File 'app/models/global.rb', line 274

def module?
	self.class.name=='Module'
end

#module_included?(symbol) ⇒ Boolean

noninherited_modules


284
285
286
287
288
289
290
# File 'app/models/global.rb', line 284

def module_included?(symbol)
	if symbol.kind_of?(Class) then
		ancestors.map{|a| a.name}.include?(symbol.name)
	else
		ancestors.map{|a| a.name}.include?(symbol.to_s)
	end #if
end

#noninherited_modulesObject

module


277
278
279
280
281
282
283
# File 'app/models/global.rb', line 277

def noninherited_modules
	if module? then
		return ancestors-[self]
	else
		return ancestors-[self]-superclass.ancestors
	end #if
end

#noninherited_public_class_methodsObject

noninherited_public_instance_methods


174
175
176
# File 'app/models/global.rb', line 174

def noninherited_public_class_methods
	self.class.methods-self.class.superclass.methods
end

#noninherited_public_instance_methodsObject


170
171
172
173
# File 'app/models/global.rb', line 170

def noninherited_public_instance_methods
	puts "noninherited_public_instance_methods in class Object called"
	return self.class.public_instance_methods(false)
end

#object_identitiesObject

info


68
69
70
# File 'app/models/global.rb', line 68

def object_identities
	return "<#{objectClass} \##{object_id}{#{hash}},#{to_s},#{inspect}>"
end

#objectClass(verbose = false) ⇒ Object

objectKind


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/global.rb', line 89

def objectClass(verbose=false)
	if nil? then
		return "NilClass"
	elsif self.class.name=='Symbol' then
		return "Symbol"
	elsif self.class.name=='Module' then
		return "Module #{name}"
	else
		if respond_to?(:superclass) then
			return "#{self.class.name} subclass of #{self.class.superclass.name}"
		else
			return "#{self.class.name}"
		end
	end

end

#objectKindObject

object_identities


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/global.rb', line 71

def objectKind
	if nil? then
		return "nil"
	elsif self.class.name=='Symbol' then
		return "Symbol"
	elsif self.class.name=='Module' then
		return "Module"
	elsif self.class.name=='String' then
		return "String"
	else
		if respond_to?(:superclass) then
			return "Class"
		else
			return "Class #{self.class.name} has no superclass."
		end
	end

end

#objectName(verbose = false) ⇒ Object

objectClass


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/global.rb', line 105

def objectName(verbose=false)
	if nil? then
		return "#{obj.inspect} is nil."
	elsif self.class.name=='Symbol' then
		#puts "find_symbol(obj)=#{find_symbol(obj)}"
		return ":#{to_s}"
	elsif self.class.name=='Module' then
		puts("name=#{name}")
		puts("nesting.inspect=#{nesting.inspect}")
		return "Module #{name}"
	elsif activeRecordTableNotCreatedYet?(obj) then
		return "Active_Record #{self.class.inspect}"
	else
		if respond_to?(:name) then
			puts("name=#{name}")
		else
			return "inspect=#{inspect} has no name."
		end
	end

end

#relationship(obj = self) ⇒ Object

whoAmI


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/models/global.rb', line 225

def relationship(obj=self)
	puts "self is #{self.whoAmI}"
	puts "obj is #{obj.whoAmI}"
	if obj.nil? then
		puts "#{obj} is nil."
	elsif obj.class.name=='Symbol' then
	else
		puts "obj.name=#{obj.name}"
		if is_a?(obj) then
			puts "#{self.name} is the class of (or superclass of) #{obj}"
		end
	end
	if self==obj then
		puts "#{obj} is the same(==) as #{self.name}"
	 
	elsif respond_to?(obj) then
		puts "#{self.name} will respond to #{obj}"
	 
	elsif singleton_methods.include?(obj) then
		puts "#{obj} is a include?d modules of #{self.name}"
	elsif included_modules.include?(obj) then
		puts "#{obj} is a include?d modules of #{self.name}"
#	elsif self.class_variable_defined?(obj) then
#		puts "#{obj} is declared as a class variable."
#	elsif self.const_defined? then
#		puts "#{obj} is is a defined constant."
	
	elsif public_instance_methods.include?(obj) then
		puts "#{obj} is a public instance method of #{self.name}"
	elsif public_methods.include?(obj) then
		puts "#{obj} is a public method of #{self.name}"
	elsif protected_methods.include?(obj) then
		puts "#{obj} is a protected method of #{self.name}"
	elsif private_methods.include?(obj) then
		puts "#{obj} is a private method of #{self.name}"
	elsif instance_variables.include?(obj) then
		puts "#{obj} is a instance variables of #{self.name}"
	elsif class_variables.include?(obj) then
		puts "#{obj} is a class variables of #{self.name}"
	elsif included_modules.include?(obj) then
		puts "#{obj} is a module of #{self.name}"
	elsif instance_methods.include?(obj) then
		puts "#{obj} is an instance module of #{self.name}"
	elsif ancestors.include?(obj) then
		puts "#{obj} is an ancestor module of #{self.name}"
	else
		puts "Can't figure out relation between #{obj.inspect} and #{self.inspect}"
	end #if
end

#whoAmI(verbose = false) ⇒ Object

noninherited_public_class_methods


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/global.rb', line 177

def whoAmI(verbose=false)
	obj=self
#	puts("obj=#{obj}") if verbose
#	puts("obj.class=#{obj.class}") if verbose
#	puts("obj.class.name=#{obj.class.name}") if verbose
#	puts("obj.to_s=#{obj.to_s}") if verbose
#	puts("obj.inspect=#{obj.inspect}") if verbose
#	puts("obj.object_id=#{obj.object_id}") if verbose
	if obj.respond_to?(:name) then
		puts("obj.name=#{obj.name}") if verbose
	else
		puts("obj has no name. obj.inspect=#{obj.inspect}.") if verbose
	end
	if obj.respond_to?(:human_name) then
		puts("obj.model_name.collection=#{obj.model_name.collection}") if verbose
		puts("obj.human_name=#{obj.human_name}") if verbose
		puts("obj.model_name.element=#{obj.model_name.element}") if verbose
		puts("obj.model_name.partial_path=#{obj.model_name.partial_path}") if verbose
		puts("obj.model_name.plural=#{obj.model_name.plural}") if verbose
		puts("obj.model_name.singular=#{obj.model_name.singular}") if verbose
	end
	puts("noninherited_public_instance_methods(obj).inspect=#{noninherited_public_instance_methods(obj).inspect}") if verbose
	puts("noninherited_public_class_methods(obj).inspect=#{noninherited_public_class_methods(obj).inspect}") if verbose
	if obj.nil? then
		return "#{obj} is nil."
	elsif obj.class.name=='Symbol' then
		#puts "find_symbol(obj)=#{find_symbol(obj)}"
		return "Symbol :#{obj.to_s}"
	elsif obj.class.name=='Module' then
		puts("obj.name=#{obj.name}") if verbose
		puts("nesting.inspect=#{nesting.inspect}") if verbose
		return "Module #{obj.name}"
	else
		if obj.respond_to?(:name) then
			puts("obj.name=#{obj.name}") if verbose
			if obj.respond_to?(:superclass) then
				puts("obj.superclass=#{obj.superclass}") if verbose
				puts("obj.superclass.name=#{obj.superclass.name}") if verbose
				return "Class #{obj.name} subclass of #{obj.superclass.name}"
			else
				return "obj Class #{obj.name} has no superclass."
			end
		else
			return "obj.inspect=#{obj.inspect} has no name."
		end
	end

end