Class: StreamMethod

Inherits:
ActiveRecord::Base show all
Includes:
Generic_Table
Defined in:
app/models/stream_method.rb

Overview

like a method def

Constant Summary collapse

@@Default_Return_Code =

eval_method

''
@@Default_Rescue_Code =
acquireBody

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

Class Method Summary collapse

Instance Method Summary collapse

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 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 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

Methods included from GenericTableAssociation::ClassMethods

#associated_foreign_key_name, #association_arity, #association_class, #association_default_class_name?, #association_method_plurality, #association_method_symbol, #association_methods, #association_names, #association_names_to_many, #association_names_to_one, #association_patterns, #foreign_key_association_names, #foreign_key_names, #foreign_key_to_association_name, #is_association?, #is_association_patterns?, #is_association_to_many?, #is_association_to_one?, #is_foreign_key_name?, #is_matching_association?, #is_polymorphic_association?, #match_association_patterns?, #name_symbol

Constructor Details

#initialize(hash = nil) ⇒ StreamMethod

after_initialize :compile_code! functions of ActiveRecord instances


26
27
28
# File 'app/models/stream_method.rb', line 26

def initialize(hash=nil)
	super(hash)
end

Class Method Details

.logical_primary_keyObject


21
22
23
# File 'app/models/stream_method.rb', line 21

def self.logical_primary_key
	return [:name]
end

Instance Method Details

#compile_code!Object


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

def compile_code!
	errors.clear # since code has presumably changed, old errors are irrelevant
	if library.nil? then
		eval_method('interface_code',interface_code)
	else
		eval_method('interface_code',"require '#{library}'\n#{interface_code}")
	end # if
	
	if return_code.nil? then
		eval_method('return_code',@@Default_Return_Code)
	else
		eval_method('return_code',return_code)
	end #if
	if rescue_code.nil? then
		eval_method('rescue_code',@@Default_Rescue_Code)
	else
		eval_method('rescue_code',"rescue #{rescue_code}\n")
	end
end

#default_methodObject

initialize


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/stream_method.rb', line 29

def default_method
	rhs=case input_streams.size
	when 0 
		'123'
	when 1
		input_streams[0].gui_name
	else
		'['+input_streams.map{|stream| stream.gui_name}.join(',')+']'		
	end #case
	return case output_streams.size
	when 0 
		'puts '+rhs
	when 1
		gui_name(output_streams[0])+'='+rhs
	else
		output_streams.map{|stream| stream.gui_name}.join(',')+'='+rhs
	end #case
	
#	return lhs+'='+rhs
end

#eval_method(name, code) ⇒ Object

create a instance (singleton) method with the supplied code. the methods have no arguments and communicate through instance variables.


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/stream_method.rb', line 63

def eval_method(name,code)
	if code.nil? || code.empty? then
		rows=0
		cols=0
	else
		code_lines=code.split("\n")
		rows=code_lines.size
		cols=code_lines.map {|l|l.length}.max
	end #if
	
	instance_eval("def #{name}_rows\n#{rows}\nend\n")
	instance_eval("def #{name}_cols\n#{cols}\nend\n")
	method_def= "def #{name}_method\n#{map_io(code)}\nend\n"
	return instance_eval(method_def)
rescue  SyntaxError => exception_raised
	errors.add(name, 'SyntaxError: ' + exception_raised.inspect, options = {}) 
	return nil
#~ else
	#~ self[:errorz]=self[:errorz].add(name, "Not subclass of SyntaxError: " + "couldn't compile string #{method_def} in context of a ruby_class object.")

end

#fire!Object

output_streams


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
# File 'app/models/stream_method.rb', line 140

def fire!
	errors.clear
	interface_code_method
	return_code_method
	rescue  StandardError => exception_raised
		errors.add(:interface_code,exception_raised.inspect)
		rescue_code_method
	else
		errors.add(:interface_code,"Not subclass of StandardError: " + "couldn't acquire data from #{url}")
	ensure
		output_streams.each do |name|
			if self[name.to_sym].nil? then
				errors.add(name,'is empty.')
				if errors.empty? then
					errors.add(:errors,'was not set.')
				end #if
			else
				if errors.empty? then
					errors.add(:errors,'was not set.')
				else
					puts "errors=#{errors.inspect},output name=#{name.inspect}"
				end #if
			end #
		end #each
		return self
end

#input_streamsObject

def


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

def input_streams
	if stream_pattern.nil? then
		return [] # pattern undefined
	else
		stream_pattern_arguments=stream_pattern.stream_pattern_arguments
		stream_inputs=stream_pattern_arguments.select{|a| a.direction=='Input'}
#		return stream_inputs.map{|a| a.name.downcase}
	end #if
end

#map_io(code) ⇒ Object

Allow external setting of instance variables


50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/stream_method.rb', line 50

def map_io(code)
	input_streams.each do |stream|
		code=code.gsub(stream.gui_name, stream.instance_name_reference)
#		code=code+":input(#{stream.gui_name}, #{instance_name_reference(stream)})"
	end #each
	output_streams.each do |stream|
		code=code.gsub(stream.gui_name, instance_name_reference(stream))
#		code=code+":output(#{stream.gui_name}, #{instance_name_reference(stream)})"
	end #each
	return code
end

#output_streamsObject

input_streams


131
132
133
134
135
136
137
138
139
# File 'app/models/stream_method.rb', line 131

def output_streams
	if stream_pattern.nil? then
		return [] # pattern undefined
	else
		stream_pattern_arguments=stream_pattern.stream_pattern_arguments
		stream_outputs=stream_pattern_arguments.select{|a| a.direction=='Output'}
#		return stream_outputs.map{|a| a.name.downcase}
	end #if
end

#short_error_messageObject

syntax_error


113
114
115
116
117
118
119
120
# File 'app/models/stream_method.rb', line 113

def short_error_message
	error_message= syntax_error?
	if error_message.nil? then
		return nil
	else
		return error_message.sub(%r{^\(eval\):\d+:in `syntax_error': compile error},'').gsub(%r{\(eval\):\d+: syntax error, },'').gsub(%r{\(eval\):\d+: },'')
	end #if
end

#syntax_errors?Boolean

syntax errors aggregated here errors in general see api.rubyonrails.org/classes/ActiveModel/Errors.html


109
110
111
112
# File 'app/models/stream_method.rb', line 109

def syntax_errors?
	errors[:interface_code] || errors[:return_code] || errors[:rescue_code]

end