samedi 27 janvier 2018

Can you instantialise a `Data` class's data inside Ruby?

An application I use at work has an embedded Ruby engine. The company have also embedded several Ruby C add-ons into the engine. The application has made several methods available in the UI mode, however not all of them. The methods are still there in the UI, however when calling them they raise an error:

Error: This method cannot be used within the User Interface

I would like to document which of these is usable within the User Interface and which are not. So I made this function:

def try(object,method,args)
    begin
        object.method(method).call(*args)
    rescue Exception => e
        message = e.to_s
        if(message == "Error: This method cannot be used within the User Interface"
            puts "#{object.to_s}::#{method.to_s} is not runnable in the UI"
        else
            puts "#{object.to_s}::#{method.to_s} is runnable in the UI"
        end
    end
end

This works great for static methods of classes! For instance, a class such as:

class A
    def A.greatMethod(a,b)
        puts "hello #{a} and #{b}"
    end
end

Could be tested like:

try(A,:greatMethod,[1,2]) #=> A::greatMethod is runnable in the UI

However, this doesn't work for instance methods of C-addons! For example:

try(MSCollection.new,:each,[1])

returns:

Error: wrong argument type MSCollection (expected Data)

Currently the only work around I have is to get a MSCollection object legitimately. For example:

col = Application.getWindows() #=> Returns collection of window objects
try(col,:each,[1]) #try method

Ideally I would be able to do something along the lines of:

try(constructIntoData(MSCollection),:each,[1])

as there are many objects to test. In the end, ideally I'd be able to do something like this:

Module.constants.each do |const|
    if const.to_s[/MS.+/]
        cls = Module.const_get(const) #class
        methods = cls.singleton_methods - Object.methods
        instance_methods = cls.instance_methods - Object.methods
        methods.each do |method|
            args = getTestArgs(cls,method)
            try(cls,method,args)
        end
        icls = constructIntoData(cls)
        instance_methods.each do |method|
            args = getTestArgs(cls,method)
            try(icls,method,args)
        end
    end
end

Any ideas?





Aucun commentaire:

Enregistrer un commentaire