ruby-ext 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.git/COMMIT_EDITMSG +1 -1
- data/.git/index +0 -0
- data/.git/logs/HEAD +3 -0
- data/.git/logs/refs/heads/master +3 -0
- data/.git/logs/refs/remotes/origin/master +3 -0
- data/.git/objects/3a/5fe0dc6aaa6e41eb8dd865fa7d7100f4e68ced +0 -0
- data/.git/objects/49/3774d80093f4ec3c6c3bfad21e1c8c9eaf41ec +0 -0
- data/.git/objects/52/94913c6107b381d1378279e58551fa2be7cecb +0 -0
- data/.git/objects/57/ab6c10ddf4eff75d98b5770a2c2bf573b692cd +0 -0
- data/.git/objects/61/beeaa4bc6790a469babafbdc7395e9e19dcb5d +0 -0
- data/.git/objects/62/3a67b104c04e24316da21f3e229e1bc2ba9d31 +0 -0
- data/.git/objects/6d/1da4d8a2949a1a9714d5f9979c7ee9613d7cfd +0 -0
- data/.git/objects/7f/2c583d1fdcc5313140c57778be8b0d45824be9 +0 -0
- data/.git/objects/9f/e2acc9bded3dc0d29b1dfc7dc1b3bed5d72c8f +0 -0
- data/.git/objects/e1/3f643da891453763504616e4f5a8195b8ffdb9 +0 -0
- data/.git/objects/ee/69f9fad83c5b0668b62622037094e989d3c394 +0 -0
- data/.git/refs/heads/master +1 -1
- data/.git/refs/remotes/origin/master +1 -1
- data/Rakefile +2 -2
- data/lib/ruby_ext/should.rb +14 -0
- data/old/README +860 -0
- data/old/lib/RubyExt/ClassLoader.rb +142 -0
- data/old/lib/RubyExt/ExtraBlankSlate.rb +14 -0
- data/old/lib/RubyExt/ImportAll.rb +9 -0
- data/old/lib/RubyExt/Localization/Module.rb +12 -0
- data/old/lib/RubyExt/Localization/Object.rb +19 -0
- data/old/lib/RubyExt/Localization/require.rb +3 -0
- data/old/lib/RubyExt/Localization.rb +28 -0
- data/old/lib/RubyExt/Log.config.sample +57 -0
- data/old/lib/RubyExt/Log.rb +59 -0
- data/old/lib/RubyExt/Path.rb +91 -0
- data/old/lib/RubyExt/Resource/file_system_provider.rb +273 -0
- data/old/lib/RubyExt/StateMashine.rb +176 -0
- data/old/lib/RubyExt/array.rb +24 -0
- data/old/lib/RubyExt/class.rb +0 -0
- data/old/lib/RubyExt/debug.rb +71 -0
- data/old/lib/RubyExt/false_class.rb +5 -0
- data/old/lib/RubyExt/kernel.rb +3 -0
- data/old/lib/RubyExt/module.rb +26 -0
- data/old/lib/RubyExt/nil_class.rb +5 -0
- data/old/lib/RubyExt/observable.rb +25 -0
- data/old/lib/RubyExt/require.rb +1 -0
- data/old/lib/RubyExt/require_base.rb +1 -0
- data/old/lib/RubyExt/resource.rb +172 -0
- data/old/lib/RubyExt/true_class.rb +5 -0
- data/old/rakefile +52 -0
- data/old/require.rb +3 -0
- data/old/require_base.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/ClassA.rb +4 -0
- data/old/spec/RubyExt/ForClassLoader/LoadCount.rb +1 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/ClassB.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/ClassInsideAnonymousClass.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/InfinityLoop.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/ModuleB/ClassC.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/SameName/SomeClass.rb +2 -0
- data/old/spec/RubyExt/ForClassLoader/ModuleA/SameName.rb +2 -0
- data/old/spec/RubyExt/ForClassLoader/Scope/ScopeA1.rb +3 -0
- data/old/spec/RubyExt/ForClassLoader/Scope/ScopeB1.rb +5 -0
- data/old/spec/RubyExt/ForKernel/Raise2.rb +5 -0
- data/old/spec/RubyExt/ForKernel/RaiseWithoutSelf.rb +9 -0
- data/old/spec/RubyExt/ForLocalization/NS/A.rb +3 -0
- data/old/spec/RubyExt/ForLocalization/NS/A.ru.localization.yaml +2 -0
- data/old/spec/RubyExt/ForLocalization/NS/B.rb +22 -0
- data/old/spec/RubyExt/ForLocalization/NS/B.ru.localization.yaml +3 -0
- data/old/spec/RubyExt/ForLocalization/NS.ru.localization.yaml +2 -0
- data/old/spec/RubyExt/ForModule/NS1/A.data +1 -0
- data/old/spec/RubyExt/ForModule/NS1/A.rb +1 -0
- data/old/spec/RubyExt/ForModule/NS1/A2.rb +1 -0
- data/old/spec/RubyExt/ForModule/NS1/B.rb +2 -0
- data/old/spec/RubyExt/ForModule/NS1/B2.rb +1 -0
- data/old/spec/RubyExt/ForModule/NS1.data +1 -0
- data/old/spec/RubyExt/ForModule/NS2/A.data +1 -0
- data/old/spec/RubyExt/ForModule/NS2/A.rb +3 -0
- data/old/spec/RubyExt/ForModule/NS2/B.rb +3 -0
- data/old/spec/RubyExt/ForModule/NS2/M.data +1 -0
- data/old/spec/RubyExt/ForModule/NS2/M.rb +3 -0
- data/old/spec/RubyExt/ForResource/ChangedClass.rb +1 -0
- data/old/spec/RubyExt/ForResource/ChangedClass.res/Text.txt +0 -0
- data/old/spec/RubyExt/ForResource/ChangedClass.txt +0 -0
- data/old/spec/RubyExt/ForResource/ProviderChaining/ProviderABaseDir/ChainTest.rb +3 -0
- data/old/spec/RubyExt/ForResource/ProviderChaining/ProviderABaseDir/ChainTest.resource +1 -0
- data/old/spec/RubyExt/ForResource/ProviderChaining/ProviderABaseDir/ClassExistOnlyInProviderA.rb +3 -0
- data/old/spec/RubyExt/ForResource/ProviderChaining/ProviderBBaseDir/ChainTest.rb +3 -0
- data/old/spec/RubyExt/ForResource/ProviderChaining/ProviderBBaseDir/ChainTest.resource +1 -0
- data/old/spec/RubyExt/ForResource/ResourceExtension.rb +1 -0
- data/old/spec/RubyExt/ForResource/ResourceExtension.res/Data.yaml +2 -0
- data/old/spec/RubyExt/ForResource/ResourceTest/Test.rb +11 -0
- data/old/spec/RubyExt/ForResource/ResourceTest/Test.res/Data.txt +1 -0
- data/old/spec/RubyExt/ForResource/ResourceTest/Test.res/txt +1 -0
- data/old/spec/array_spec.rb +27 -0
- data/old/spec/class_loader_spec.rb +94 -0
- data/old/spec/import_all_spec.rb +21 -0
- data/old/spec/kernel_spec.rb +29 -0
- data/old/spec/localization_spec.rb +49 -0
- data/old/spec/module_spec.rb +14 -0
- data/old/spec/object_spec.rb +8 -0
- data/old/spec/observable_spec.rb +44 -0
- data/old/spec/path_spec.rb +169 -0
- data/old/spec/resource_spec.rb +136 -0
- data/old/spec/state_machine_spec.rb +201 -0
- metadata +93 -2
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
module RubyExt
|
|
2
|
+
class FileSystemProvider
|
|
3
|
+
attr_accessor :directories
|
|
4
|
+
|
|
5
|
+
def initialize directories = lambda{[File.expand_path('.')]}
|
|
6
|
+
@directories = directories
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def class_get class_name
|
|
10
|
+
path = real_class_path class_name
|
|
11
|
+
raise Resource::NotExist unless path
|
|
12
|
+
|
|
13
|
+
if File.directory? path
|
|
14
|
+
"module #{File.basename(path)}; end;"
|
|
15
|
+
else
|
|
16
|
+
File.read path
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# It updates existing class, if class doesn't exist it creates one in directory provided by filter
|
|
21
|
+
def class_set class_name, data, directory_for_class_file = nil
|
|
22
|
+
directory_for_class_file ||= Dir.getwd
|
|
23
|
+
directory_for_class_file = File.expand_path directory_for_class_file
|
|
24
|
+
unless @directories.call.include? directory_for_class_file
|
|
25
|
+
raise "Dirctories should include '#{directory_for_class_file}'!"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
path = real_class_path class_name
|
|
29
|
+
unless path
|
|
30
|
+
path = class_to_files(class_name).select{|f| f.include?(directory_for_class_file)}.first
|
|
31
|
+
dir = path.sub(/\w+\.rb/, "")
|
|
32
|
+
FileUtils.mkdir dir unless dir.empty? or File.exist? dir
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
length = File.write(path, data)
|
|
36
|
+
FileSystemProvider.remember_file path
|
|
37
|
+
return length
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def class_exist? class_name
|
|
41
|
+
real_class_path(class_name) != nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def class_delete class_name
|
|
45
|
+
path = real_class_path class_name
|
|
46
|
+
File.delete path if path
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# def class_namespace_exist? namespace_name
|
|
50
|
+
# File.exist? class_to_basefile(namespace_name)
|
|
51
|
+
# end
|
|
52
|
+
|
|
53
|
+
# First search for "./class_name.resource_name" files
|
|
54
|
+
# And then search for "./class_name.res/resource_name" files
|
|
55
|
+
def resource_get class_name, resource_name
|
|
56
|
+
path = real_resource_path class_name, resource_name
|
|
57
|
+
raise Resource::NotExist unless path
|
|
58
|
+
|
|
59
|
+
File.read path
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def resource_delete class_name, resource_name
|
|
63
|
+
path = real_resource_path class_name, resource_name
|
|
64
|
+
File.delete path if path
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# It can only update existing resource, it can't create a new one
|
|
68
|
+
# First search for the same resource and owerwrites it
|
|
69
|
+
# If such resource doesn't exists writes to
|
|
70
|
+
# "./class_name.res/resource_name" file.
|
|
71
|
+
def resource_set class_name, resource_name, data
|
|
72
|
+
class_path = real_class_path class_name
|
|
73
|
+
raise Resource::NotExist unless class_path
|
|
74
|
+
|
|
75
|
+
path = real_resource_path class_name, resource_name
|
|
76
|
+
unless path
|
|
77
|
+
class_path = class_path.sub(/\.rb$/, "")
|
|
78
|
+
dir = "#{class_path}.res"
|
|
79
|
+
FileUtils.mkdir dir unless File.exist? dir
|
|
80
|
+
path = "#{dir}/#{resource_name}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
length = File.write path, data
|
|
84
|
+
FileSystemProvider.remember_file path
|
|
85
|
+
return length
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def resource_exist? class_name, resource_name
|
|
89
|
+
class_path = real_class_path class_name
|
|
90
|
+
raise Resource::NotExist unless class_path
|
|
91
|
+
|
|
92
|
+
real_resource_path(class_name, resource_name) != nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# def class_to_virtual_path class_name
|
|
96
|
+
# result = nil
|
|
97
|
+
# if @cache.include? class_name
|
|
98
|
+
# result = @cache[class_name]
|
|
99
|
+
# else
|
|
100
|
+
# result = nil
|
|
101
|
+
# path = "#{base_dir}/#{class_name.gsub("::", "/")}"
|
|
102
|
+
# if File.exist? path
|
|
103
|
+
# result = path
|
|
104
|
+
# else
|
|
105
|
+
# path2 = "#{path}.rb"
|
|
106
|
+
# if File.exist? path2
|
|
107
|
+
# result = path
|
|
108
|
+
# else
|
|
109
|
+
# path3 = "#{path}.res"
|
|
110
|
+
# if File.exist? path3
|
|
111
|
+
# result = path
|
|
112
|
+
# end
|
|
113
|
+
# end
|
|
114
|
+
# end
|
|
115
|
+
# @cache[class_name] = result
|
|
116
|
+
# end
|
|
117
|
+
#
|
|
118
|
+
# if result
|
|
119
|
+
# return result
|
|
120
|
+
# else
|
|
121
|
+
# raise Resource::NotExist, "Class '#{class_name}' doesn't Exist!"
|
|
122
|
+
# end
|
|
123
|
+
# end
|
|
124
|
+
|
|
125
|
+
# Different Providers can use different class path interpretation.
|
|
126
|
+
# So we return virtual path only if this path really exist.
|
|
127
|
+
def class_to_virtual_file class_name
|
|
128
|
+
path = nil
|
|
129
|
+
if FileSystemProvider.cache.include? class_name
|
|
130
|
+
path = FileSystemProvider.cache[class_name]
|
|
131
|
+
else
|
|
132
|
+
path = real_class_path class_name
|
|
133
|
+
raise Resource::NotExist, "Class '#{class_name}' doesn't Exist!" unless path
|
|
134
|
+
|
|
135
|
+
FileSystemProvider.cache[class_name] = path
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
File.expand_path path
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Reloading
|
|
142
|
+
@files, @cache = {}, {}
|
|
143
|
+
class << self
|
|
144
|
+
attr_reader :cache
|
|
145
|
+
|
|
146
|
+
def start_watching interval = 2, directories = Dir.glob("**/lib")
|
|
147
|
+
stop_watching
|
|
148
|
+
@watch_thread = Thread.new do
|
|
149
|
+
reset_changed_files directories
|
|
150
|
+
while true
|
|
151
|
+
sleep interval
|
|
152
|
+
begin
|
|
153
|
+
check_for_changed_files(directories).each do |type, klass, res|
|
|
154
|
+
Resource.notify_observers :update_resource, type, klass, res
|
|
155
|
+
end
|
|
156
|
+
rescue Exception => e
|
|
157
|
+
warn e
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def stop_watching
|
|
164
|
+
if @watch_thread
|
|
165
|
+
@watch_thread.kill
|
|
166
|
+
@watch_thread = nil
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def remember_file path
|
|
171
|
+
@files[path] = File.mtime(path)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
protected
|
|
175
|
+
def check_for_changed_files directories
|
|
176
|
+
changed = []
|
|
177
|
+
directories.inject([]){|list, dir| list + Dir.glob("#{dir}/**/**")}.each do |path|
|
|
178
|
+
if file_changed? path, directories
|
|
179
|
+
remember_file path
|
|
180
|
+
changed << file_changed(path, directories)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
return changed
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def reset_changed_files directories
|
|
187
|
+
@files = {}
|
|
188
|
+
directories.inject([]){|list, dir| list + Dir.glob("#{dir}/**/**")}.each do |path|
|
|
189
|
+
remember_file path
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def file_changed? path, directories
|
|
194
|
+
old_time = @files[path]
|
|
195
|
+
old_time == nil or old_time != File.mtime(path)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def file_changed path, directories
|
|
199
|
+
begin
|
|
200
|
+
if path =~ /\.rb$/
|
|
201
|
+
path = path.sub(/\.rb$/, "")
|
|
202
|
+
class_name = path_to_class(path, directories)
|
|
203
|
+
|
|
204
|
+
# ClassLoader.reload_class()
|
|
205
|
+
klass = eval class_name, TOPLEVEL_BINDING, __FILE__, __LINE__
|
|
206
|
+
cache.delete class_name
|
|
207
|
+
return :class, klass, nil
|
|
208
|
+
else
|
|
209
|
+
if path =~ /\.res/
|
|
210
|
+
class_path = path.sub(/\.res.+/, "")
|
|
211
|
+
resource_name3 = path.sub("#{class_path}.res/", "")
|
|
212
|
+
class_name = path_to_class class_path, directories
|
|
213
|
+
else
|
|
214
|
+
resource_name3 = path.sub(/.+\./, "")
|
|
215
|
+
class_name = path_to_class path.sub(/\.#{resource_name3}$/, ""), directories
|
|
216
|
+
end
|
|
217
|
+
klass = eval class_name, TOPLEVEL_BINDING, __FILE__, __LINE__
|
|
218
|
+
return :resource, klass, resource_name3
|
|
219
|
+
end
|
|
220
|
+
rescue Exception => e
|
|
221
|
+
warn "Can't reload file '#{path}' #{e.message}!"
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def path_to_class path, directories
|
|
226
|
+
base_dir = directories.select{|f| path.include? f}.max{|a, b| a.size <=> b.size}
|
|
227
|
+
path.gsub(/^#{base_dir}\//, "").gsub("/", "::")
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
protected
|
|
232
|
+
def real_class_path class_name
|
|
233
|
+
path = class_to_files(class_name).find{|f| File.exist? f}
|
|
234
|
+
path = class_to_basefiles(class_name).find{|f| File.exist? f} unless path
|
|
235
|
+
return path
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def real_resource_path class_name, resource_name
|
|
239
|
+
file = class_to_basefiles(class_name).collect{|f| "#{f}.#{resource_name}"}.find{|f| File.exist? f}
|
|
240
|
+
file = class_to_basefiles(class_name).collect{|f| "#{f}.res/#{resource_name}"}.find{|f| File.exist? f} unless file
|
|
241
|
+
return file
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def class_to_files class_name
|
|
245
|
+
class_to_basefiles(class_name).collect{|name| "#{name}.rb"}
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
# Different Providers can use different class path interpretation.
|
|
249
|
+
# So we return virtual path only if this path really exist.
|
|
250
|
+
def class_to_basefiles class_name
|
|
251
|
+
relative = class_name.gsub("::", "/")
|
|
252
|
+
return @directories.call.collect{|dir| "#{dir}/#{relative}"}
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Doesn't make Sense for Virtual Resource System
|
|
256
|
+
# def resource_path class_name, resource_name
|
|
257
|
+
# @monitor.synchronize do
|
|
258
|
+
# path = "#{class_to_path(class_name)}.#{resource_name}"
|
|
259
|
+
# if File.exist? path
|
|
260
|
+
# return path
|
|
261
|
+
# else
|
|
262
|
+
# path = "#{class_to_path(class_name)}.res/#{resource_name}"
|
|
263
|
+
# if File.exist? path
|
|
264
|
+
# return path
|
|
265
|
+
# else
|
|
266
|
+
# nil
|
|
267
|
+
# end
|
|
268
|
+
# end
|
|
269
|
+
# end
|
|
270
|
+
# end
|
|
271
|
+
|
|
272
|
+
end
|
|
273
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
class StateMashine
|
|
2
|
+
attr_accessor :object
|
|
3
|
+
|
|
4
|
+
def initialize object = nil;
|
|
5
|
+
@object = object
|
|
6
|
+
@on_entry, @on_exit, @on_event = {}, {}, {}
|
|
7
|
+
|
|
8
|
+
@custom_states = {}
|
|
9
|
+
if self.class.custom_state_get
|
|
10
|
+
self.class.custom_state_get.each do |state, klass|
|
|
11
|
+
@custom_states[state] = klass.new
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
is = self.class.initial_state_get
|
|
16
|
+
raise_without_self "Initial State not defined!" unless is
|
|
17
|
+
fire_action @on_entry[is]
|
|
18
|
+
@state = is
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def on_entry state, method = nil, &b
|
|
22
|
+
if method or b
|
|
23
|
+
@on_entry[state] = method || b
|
|
24
|
+
else
|
|
25
|
+
@on_entry.delete state
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def on_event from, event, method = nil, &b
|
|
30
|
+
if method or b
|
|
31
|
+
@on_event[from + event] = method || b
|
|
32
|
+
else
|
|
33
|
+
@on_event.delete from + event
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def on_exit state, method = nil, &b
|
|
38
|
+
if method or b
|
|
39
|
+
@on_exit[state] = method || b
|
|
40
|
+
else
|
|
41
|
+
@on_exit.delete state
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def event event
|
|
46
|
+
custom_state = @custom_states[state]
|
|
47
|
+
|
|
48
|
+
to = if custom_state
|
|
49
|
+
begin
|
|
50
|
+
custom_state.send event
|
|
51
|
+
rescue NoMethodError
|
|
52
|
+
raise_without_self "There is no :#{event} Event in :#{state} State!"
|
|
53
|
+
end
|
|
54
|
+
else
|
|
55
|
+
actions = self.class.from_actions[state]
|
|
56
|
+
raise_without_self "There is no :#{event} Event in :#{state} State!" unless actions
|
|
57
|
+
actions[event]
|
|
58
|
+
end
|
|
59
|
+
raise_without_self "There is no :#{event} Event in :#{state} State!" unless to
|
|
60
|
+
|
|
61
|
+
# p [state, event, to]
|
|
62
|
+
|
|
63
|
+
old_state = state
|
|
64
|
+
|
|
65
|
+
@state = to
|
|
66
|
+
|
|
67
|
+
begin
|
|
68
|
+
fire_action @on_exit[old_state]
|
|
69
|
+
fire_action @on_event[old_state + event]
|
|
70
|
+
fire_action @on_entry[to]
|
|
71
|
+
rescue Exception => e
|
|
72
|
+
@state = old_state
|
|
73
|
+
raise e
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def state; @state end
|
|
78
|
+
|
|
79
|
+
def state= new_state
|
|
80
|
+
custom_state = @custom_states[state]
|
|
81
|
+
|
|
82
|
+
event = if custom_state
|
|
83
|
+
begin
|
|
84
|
+
custom_state.state new_state
|
|
85
|
+
rescue Exception => e
|
|
86
|
+
raise e
|
|
87
|
+
end
|
|
88
|
+
else
|
|
89
|
+
states = self.class.from_states[state]
|
|
90
|
+
raise_without_self "There is no way from :#{state} State into :#{new_state} State!" unless states
|
|
91
|
+
states[new_state]
|
|
92
|
+
end
|
|
93
|
+
raise_without_self "There is no way from :#{state} State into :#{new_state} State!" unless event
|
|
94
|
+
|
|
95
|
+
old_state = state
|
|
96
|
+
|
|
97
|
+
@state = new_state
|
|
98
|
+
|
|
99
|
+
begin
|
|
100
|
+
fire_action @on_exit[old_state]
|
|
101
|
+
fire_action @on_event[old_state + event]
|
|
102
|
+
fire_action @on_entry[new_state]
|
|
103
|
+
rescue Exception => e
|
|
104
|
+
@state = old_state
|
|
105
|
+
raise e
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def == other
|
|
110
|
+
return true if equal? other
|
|
111
|
+
return state == other if other.is_a? Symbol
|
|
112
|
+
return state == other.state if other.is_a? StateMashine
|
|
113
|
+
return false
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def hash
|
|
117
|
+
state.hash
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def eql? other
|
|
121
|
+
return true if equal? other
|
|
122
|
+
return state == other.state if other.is_a? StateMashine
|
|
123
|
+
return false
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
class << self
|
|
127
|
+
def from_actions; @from_actions end
|
|
128
|
+
def from_states; @from_states end
|
|
129
|
+
def initial_state_get; @initial_state end
|
|
130
|
+
def custom_state_get; @custom_state end
|
|
131
|
+
|
|
132
|
+
def transitions *args
|
|
133
|
+
@from_actions, @from_states = {}, {}
|
|
134
|
+
args.each do |from, action, to|
|
|
135
|
+
actions = @from_actions[from]
|
|
136
|
+
unless actions
|
|
137
|
+
actions = {}
|
|
138
|
+
@from_actions[from] = actions
|
|
139
|
+
end
|
|
140
|
+
actions[action] = to
|
|
141
|
+
|
|
142
|
+
states = @from_states[from]
|
|
143
|
+
unless states
|
|
144
|
+
states = {}
|
|
145
|
+
@from_states[from] = states
|
|
146
|
+
end
|
|
147
|
+
states[to] = action
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def custom_state state, klass;
|
|
152
|
+
@custom_state ||= {}
|
|
153
|
+
@custom_state[state] = klass
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def initial_state state; @initial_state = state end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def to_s
|
|
160
|
+
"#<#{self.class.name} state = :#{state}"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def inspect
|
|
164
|
+
to_s
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
protected
|
|
168
|
+
def fire_action a
|
|
169
|
+
return unless a
|
|
170
|
+
if a.is_a? Symbol
|
|
171
|
+
@object.send a if @object
|
|
172
|
+
else
|
|
173
|
+
a.call
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class Array
|
|
2
|
+
def sort_by_weight weight
|
|
3
|
+
clone.sort_by_weight! weight.clone
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def sort_by_weight! weight
|
|
7
|
+
size.times do |i|
|
|
8
|
+
iteration = size - i - 1
|
|
9
|
+
break if iteration < 0
|
|
10
|
+
iteration.times do |j|
|
|
11
|
+
if weight[j] > weight[j+1]
|
|
12
|
+
buf = self[j]
|
|
13
|
+
self[j] = self[j+1]
|
|
14
|
+
self[j+1] = buf
|
|
15
|
+
|
|
16
|
+
buf = weight[j]
|
|
17
|
+
weight[j] = weight[j+1]
|
|
18
|
+
weight[j+1] = buf
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
return self
|
|
23
|
+
end
|
|
24
|
+
end
|
|
File without changes
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
$debug = true
|
|
2
|
+
if $debug
|
|
3
|
+
warn 'DEBUG MODE'
|
|
4
|
+
|
|
5
|
+
#require 'ruby-debug'
|
|
6
|
+
|
|
7
|
+
Thread.abort_on_exception = true
|
|
8
|
+
|
|
9
|
+
RubyExt::ClassLoader.error_on_defined_constant = true
|
|
10
|
+
|
|
11
|
+
module Kernel
|
|
12
|
+
STACK_TRACE_EXCLUDE = [
|
|
13
|
+
/\/rspec/,
|
|
14
|
+
/\/ruby-debug/,
|
|
15
|
+
/\/monitor.rb/,
|
|
16
|
+
/\/timeout.rb/,
|
|
17
|
+
# /gems/,
|
|
18
|
+
# /WGUI/,
|
|
19
|
+
/\/MicroContainer/,
|
|
20
|
+
/\/RubyExt/,
|
|
21
|
+
/\/kernel.rb/,
|
|
22
|
+
/\/mongrel/,
|
|
23
|
+
/\/rack/,
|
|
24
|
+
/\/sync/,
|
|
25
|
+
/\/require/,
|
|
26
|
+
/\/site_ruby/,
|
|
27
|
+
/OpenConstructor/,
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
alias_method :old_caller, :caller
|
|
31
|
+
def caller int = 0
|
|
32
|
+
stack = old_caller
|
|
33
|
+
stack = stack[(int+1)..stack.size].delete_if do |line|
|
|
34
|
+
STACK_TRACE_EXCLUDE.any?{|re| line =~ re}
|
|
35
|
+
end
|
|
36
|
+
return stack
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def where?
|
|
40
|
+
puts "\nwhere:"
|
|
41
|
+
puts caller
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# alias_method :old_raise, :raise
|
|
45
|
+
# def raise *p
|
|
46
|
+
# case p.size
|
|
47
|
+
# when 3
|
|
48
|
+
# e, m, c = p
|
|
49
|
+
# when 2
|
|
50
|
+
# e, m = p
|
|
51
|
+
# c = caller
|
|
52
|
+
# when 1
|
|
53
|
+
# if p[0].is_a? Exception
|
|
54
|
+
# e = p[0].class
|
|
55
|
+
# m = p[0].message
|
|
56
|
+
# else
|
|
57
|
+
# e = RuntimeError
|
|
58
|
+
# m = p[0]
|
|
59
|
+
# end
|
|
60
|
+
# c = caller
|
|
61
|
+
# when 0
|
|
62
|
+
# e, m, c = RuntimeError, "", caller
|
|
63
|
+
# else
|
|
64
|
+
# old_raise "Invalid Usage!"
|
|
65
|
+
# end
|
|
66
|
+
# old_raise e, m, c
|
|
67
|
+
# end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
#def p; end
|
|
71
|
+
#def puts; end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def resource_exist? resource_name
|
|
8
|
+
self_ancestors_and_namespaces do |klass|
|
|
9
|
+
return true if RubyExt::Resource.resource_exist? klass, resource_name
|
|
10
|
+
end
|
|
11
|
+
return false
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def [] resource_name
|
|
15
|
+
self_ancestors_and_namespaces do |klass|
|
|
16
|
+
if RubyExt::Resource.resource_exist? klass, resource_name
|
|
17
|
+
return RubyExt::Resource.resource_get(klass, resource_name)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
raise RubyExt::Resource::NotExist, "Resource '#{resource_name}' for Class '#{self.name}' doesn't exist!", caller
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def []= resource_name, value
|
|
24
|
+
RubyExt::Resource.resource_set self.name, resource_name, value
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module RubyExt
|
|
2
|
+
module Observable
|
|
3
|
+
def add_observer observer
|
|
4
|
+
@observable_observers ||= []
|
|
5
|
+
@observable_observers << observer unless @observable_observers.include? observer
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def notify_observers method, *args
|
|
9
|
+
method.should! :be_a, Symbol
|
|
10
|
+
@observable_observers.each{|observer| observer.respond_to method, *args} if @observable_observers
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def delete_observer observer
|
|
14
|
+
@observable_observers.delete observer if @observable_observers
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def delete_observers
|
|
18
|
+
@observable_observers.clear if @observable_observers
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def observers_count
|
|
22
|
+
@observable_observers ? @observable_observers.size : 0
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'RubyExt/_require'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'RubyExt/_require_base'
|