Class: MethodModel

Inherits:
Object show all
Includes:
NoDB
Defined in:
app/models/method_model.rb

Overview

Provide ActiveRecord methods to classes with no Database

Constant Summary

Constants included from NoDB

NoDB::DEFAULT_TYPE

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Constants included from GenericTableAssociation::Examples

GenericTableAssociation::Examples::First_stream_method

Instance Attribute Summary collapse

Attributes included from NoDB

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NoDB

#[], #[]=, #clone, #each_pair, #has_key?, #insert_sql, #keys, #table_class, #table_name

Methods included from Generic_Table::ClassMethods

#is_active_record_method?, #model_file_name, #nesting, #one_pass_statistics, #sample, #sample_burst

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Methods included from Generic_Table

activeRecordTableNotCreatedYet?, classDefiniton, classReference, class_of_name, eval_constant, is_ActiveRecord_table?, is_generic_table?, is_generic_table_name?, is_table?, rails_MVC_class?, rubyClassName, table_exists?

Methods included from GenericTableAssociation

#associated_foreign_key_records, #associated_to_s, #association_class, #association_has_data, #association_state, #foreign_key_points_to_me?, #foreign_key_to_association, #foreign_key_value, #logical_primary_key_recursive_value, #logical_primary_key_value, #name_to_association

Methods included from GenericTableAssociation::Assertions::ClassMethods

#assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableAssociation::Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableHtml

#column_html, #rails_route, #row_html

Constructor Details

#initialize(m, owner = nil, scope = nil) ⇒ MethodModel

method_query


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/method_model.rb', line 18

def initialize(m,owner=nil,scope=nil)
	super()
	if !m.nil? && owner.nil? && scope.nil? then # only one argument (method)
		theMethod=m
		m=theMethod.name # elsewhere
		attributes[:name]=theMethod.name
		attributes[:method]=theMethod
		attributes[:scope]=theMethod.owner.class
		attributes[:arity]=theMethod.arity
		attributes[:owner]=theMethod.owner
		@init_path = [:object_space_method]
		if theMethod.respond_to?(:source_location)
			attributes[:source_location]=theMethod.source_location
			@init_path << :source_location
		else
			attributes[:source_location]=nil
			@init_path << :not_source_location
		end #if
		if theMethod.respond_to?(:parameters)
			attributes[:parameters]=theMethod.parameters
			@init_path << :parameters
		end #if
	else #3 arguments
		attributes[:name]=m.to_sym
		attributes[:owner]=owner
		attributes[:scope]=scope
		@init_path = [:init]

		if scope.to_sym==:class then
			theMethod=owner.method(m.to_sym)
			@init_path << :class
		else
			theMethod=MethodModel.method_query(m, owner)
		end #if
		if !theMethod.nil? then
			attributes[:method]=theMethod
			attributes[:arity]=theMethod.arity
			attributes[:owner]=theMethod.owner
			@init_path << :theMethod_not_nil
			if theMethod.respond_to?(:source_location)
				attributes[:source_location]=theMethod.source_location
				@init_path << :source_location
			else
				attributes[:source_location]=nil
				@init_path << :not_source_location
			end #if
			if theMethod.respond_to?(:parameters)
				attributes[:parameters]=theMethod.parameters
				@init_path << :parameters
			end #if
		else
			attributes[:method]=nil
			attributes[:arity]=nil
			attributes[:owner]=nil
			attributes[:source_location]=nil
			attributes[:parameters]=nil
			@init_path << :theMethod_nil
		end #if
	end #if
	begin
		attributes[:protected]=protected_method_defined?(m)
		attributes[:private]=private_method_defined?(m)
		@init_path << :protected
	rescue StandardError => exc
		attributes[:exception]=exc
		attributes[:protected]=nil
		attributes[:private]=nil
		@init_path << :rescue_protected
	end #if
	if m.to_s[/[a-zA-Z0-9_]+/,0]==m.to_s then
		attributes[:instance_variable_defined]=theMethod.instance_variable_defined?(('@'+m.to_s))
		@init_path << :alphanumeric
	else
		attributes[:instance_variable_defined]=nil
		@init_path << :not_alphanumeric
	end #if
	attributes[:singleton]=singleton_methods.include?(m)
end

Instance Attribute Details

#init_pathObject (readonly)

Returns the value of attribute init_path


4
5
6
# File 'app/models/method_model.rb', line 4

def init_path
  @init_path
end

Class Method Details

.allObject

all_singleton_methods


143
144
145
146
# File 'app/models/method_model.rb', line 143

def self.all
	@@ALL||=(all_methods+all_instance_methods+all_class_methods+all_singleton_methods)
	return @@ALL
end

.all_class_methodsObject

all_instance_methods


137
138
139
# File 'app/models/method_model.rb', line 137

def self.all_class_methods
	return classes_and_modules.map { |c| c.methods(false).map { |m| new(m,c,:class) } }.flatten
end

.all_instance_methodsObject

classes_and_modules


134
135
136
# File 'app/models/method_model.rb', line 134

def self.all_instance_methods
	return classes_and_modules.map { |c| c.instance_methods(false).map { |m| new(m,c,:instance) } }.flatten
end

.all_methodsObject

constantized


106
107
108
109
110
111
112
113
114
# File 'app/models/method_model.rb', line 106

def self.all_methods
	ret=[]
	ObjectSpace.each_object(Method) do |m| 
		ret << m
	end #each_object
	ret=ret.sort{|x,y| (x.name)<=>(y.name)}
	ret=ret.map {|method| new(method)}
	return ret #.uniq
end

.all_singleton_methodsObject

all_class_methods


140
141
142
# File 'app/models/method_model.rb', line 140

def self.all_singleton_methods
	return classes_and_modules.map { |c| c.singleton_methods(false).map { |m| new(m,c,:singleton) } }.flatten
end

.classesObject

methods


115
116
117
118
119
120
121
122
# File 'app/models/method_model.rb', line 115

def self.classes
	ret=[]
	ObjectSpace.each_object(Class) do |c| 
		ret << c
	end #each_object
	ret=ret.sort{|x,y| (x.name)<=>(y.name)}
	return ret 
end

.classes_and_modulesObject

modules


131
132
133
# File 'app/models/method_model.rb', line 131

def self.classes_and_modules
	return @@CLASSES_AND_MODULES||=classes+modules
end

.constantizedObject

new


96
97
98
99
100
101
102
103
104
105
# File 'app/models/method_model.rb', line 96

def self.constantized
	@@CONSTANTIZED||=Module.constants.map do |c|
		begin
			c=c.constantize
		rescue
			 puts "constant #{c.inspect} fails constanization"
			 nil
		 end #begin
	end #map
end

.find_by_name(name) ⇒ Object

first


150
151
152
# File 'app/models/method_model.rb', line 150

def self.find_by_name(name)
	self.all.find_all{|i| i[:name].to_sym==name.to_sym}
end

.firstObject

all


147
148
149
# File 'app/models/method_model.rb', line 147

def self.first
	self.all.first
end

.method_query(m, owner) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/method_model.rb', line 5

def self.method_query(m, owner)
	ObjectSpace.each_object(owner) do |object| 
		if object.respond_to?(m.to_sym) then
		begin
			theMethod=object.method(m.to_sym)
			return theMethod
		rescue ArgumentError, NameError => exc
			puts "exc=#{exc}, object=#{object.inspect}"
		end #begin
		end #if
	end #each_object
	return nil #no object found, new has side effects
end

.modulesObject

classes


123
124
125
126
127
128
129
130
# File 'app/models/method_model.rb', line 123

def self.modules
	ret=[]
	ObjectSpace.each_object(Module) do |mod| 
		ret << mod
	end #each_object
	ret=ret.sort{|x,y| x.name<=>y.name}
	return ret-classes
end

.owners_of(method_name) ⇒ Object

find_by_name


153
154
155
156
# File 'app/models/method_model.rb', line 153

def self.owners_of(method_name)
	find_by_name(method_name).map {|i| {:owner => i[:owner],:scope => i[:scope]}}

end