i.script
3/14/2006 9:47:00 AM
In search for a simple database system that's also an an universal
application service ( similar to Frontier) I've started again to play
around with FSDB. By accident I've found out that this way every file
system can be turned into an application. Using the incredible powerful
"method_missing" construct the following code was implemented:
require 'fsdb'
@@root = FSDB::Database.new('/tmp/examples')
@@path = ''
script = <<MY_SCRIPT
def show_it(a,b)
puts a.to_s + b.to_s
end
MY_SCRIPT
script2 = <<MY_SCRIPT
def make_it(a,b)
puts a + ' : ' + b
end
MY_SCRIPT
script3 = <<MY_SCRIPT
def hello_method
write_it("Hello World!")
end
def write_it(txt)
puts txt
end
MY_SCRIPT
@@root['/scripts/myscripts/show_it'] = script
@@root['/scripts/make_it'] = script2
@@root['/scripts/hello_method'] = script3
@@root['/values/a'] = 25
@@root['/values/b'] = 35
@@root['/values/array'] = [31,2,3,4]
class <<
def method_missing(method_name,*args )
@@path << '/' + method_name.to_s
if !(File.directory?(@@root.absolute("#{@@path}")))
res = @@root["/#{@@path}"]
if res.to_s.include? "def"
if !(methods.include? method_name)
eval res
@@path = ''
return send(method_name,*args)
end
end
@@path = ''
return res
end
end
end
scripts.myscripts.show_it("one","two")
scripts.make_it("one","two")
scripts.hello_method()
puts values.a + values.b
myVal = values.array + [5,6,7,8]
puts myVal.to_s
So my question is: has someone else used "method_missing" this way and
are there any "show stoppers" to use "method missing" this way. I'm
planning to use this idea for a very important project and I want to
try to avoid to run into troubles because of a lack of experience.
- Tom