Metadata

R

Ralph Mason

I am playing with dynamicaly creating proxy ruby objects, but need more
data than can be provided by a class or function name.

And so am wondering if anyone has a way to do this?

eg - something like

class MyClass

Metadata.new({"returns"=>"int","params"=>"int,int")
def my_function(x)

end

end


Somewhere later I could look at metadata by class and function to get
the hash of information about these things

eg

Metadata.each_class{| data |
data.each_function{ | funct_data |
Here I would have information about the function
and the paramers passrd to the metadata
}
}

Thanks
Ralph


//
 
R

Ryan Pavlik

I am playing with dynamicaly creating proxy ruby objects, but need
more data than can be provided by a class or function name.

And so am wondering if anyone has a way to do this?

Everyone will hate me for this, but I wrote two modules for doing just
these two things: MetaTags and Strongtyping. Unfortunately the server
has been down this weekend due to hardware death. I've just reloaded
the mephle.org stuff, so those should be there now.

You may or may not care for how metatags works; it works conveniently
for me, but you could build a compatible structure from various means.
If you come up with another interesting way of doing this, I would be
interested.
 
R

Ralph Mason

Ryan said:
Everyone will hate me for this, but I wrote two modules for doing just
these two things: MetaTags and Strongtyping. Unfortunately the server
has been down this weekend due to hardware death. I've just reloaded
the mephle.org stuff, so those should be there now.

You may or may not care for how metatags works; it works conveniently
for me, but you could build a compatible structure from various means.
If you come up with another interesting way of doing this, I would be
interested.
I will check out your stuff. Thanks

I guess I would just like to be able to do something like my example.
Also being to subclass Metadata would be handy

eg

class PlRubyFunction < Metadata
def initialize(ret,params)
....
end
end



...sometime later

def MyClass

PlRubyFunction.new("int",["int","int"])
def my_func(x)

end
end

....

Metadata.each(PlRubyFunction){| func|
puts func.class
puts func.name
puts func.ret
puts func.params
#I would really like this one
puts func.code
}

Or somesuch like that so you can make metadata self vaidating.

It seems like something that would have an unlimited number of useful
and creative applications.

Ralph
 
R

Ryan Pavlik

On Tue, 20 Jan 2004 13:36:26 +0900

Or somesuch like that so you can make metadata self vaidating.

Yeah, I don't validate, but then there's no data that's really for
validation. Strongtyping takes care of argument types (so you can
just ask), and it's pretty immediately apparent if that's wrong.
It seems like something that would have an unlimited number of useful
and creative applications.

Definitely. I use it to automatically generate "dialogs" for calling
methods. Saves a ton of work to have everything once in once place.

It occurs to me I could have provided URL's, sorry:

http://mephle.org/MetaTags
http://mephle.org/StrongTyping

hth,
 
R

Richard Kilmer

Ralph,

Some time ago I came up with a syntax for method metadata as well.

Assuming a class definition like this:

class Account
def Account.open(first, last)
end
def close
end
def add(amount)
end
def remove(amount)
end
def balance
end
def transfer(amount, account)
end
end

And assuming you wanted to make that accessible for statically typed
systems you would just do this:

require 'extern'

class Account
extern :Account.open, :return[Account], :first[String], :last[String]
def Account.open(first, last)
end

extern :close
def close
end

extern :add, :amount[Float]
def add(amount)
end

extern :remove, :amount[Float]
def remove(amount)
end

extern :balance, :return[Float]
def balance
end

extern :transfer, :amount[Float], :account[Account]
def transfer(amount, account)
end
end

So, for each method you want to externalize you just include the
'extern' call.

Usage: extern <method symbol>, [ [<:return[Classname]>],
<:param[Classname]>, ... ]

To reflect on this metadata you do:

Account.each_externalized_method do | method |
puts method.to_s
end

Which outputs:

Account Account.open(String first, String last)
NilClass close()
NilClass add(Float amount)
NilClass remove(Float amount)
Float balance()
NilClass transfer(Float amount, Account account)

You can, of course, inspect the method (ExternalMethodDefinition
instance) instead of printing it out. So this creates a runtime
structure to store Class information about methods returns or
parameters, which again is useful is you want to bridge Ruby with
statically typed languages like Java, .NET, etc.

Of course, since Ruby's classes are open, you can define this extern
metadata on existing classes:

class ThreadGroup
extern :ThreadGroup.new, :return[ThreadGroup]
extern :add, :return[ThreadGroup], :thread[Thread]
extern :list, :return[Array]
end

I have the code to make this syntax work...which actually adds behavior
to the symbol class (I know that's a bit odd, but it creates a cool
look...I think). Let me know if you want it.

- Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top