Problem
While debugging on the rails console this week, I needed to load some code that we use as embedded methods. Usually I would just copy and paste it from the UI, but since Hammer can load embedded methods recursively now, this does no longer work…
After a day of digging in the code I came up with a solution, which is probably helpful for a bunch of you guys.
Full code: https://gist.github.com/ThomasBuchinger/a34b9c242c01da3315680f5b546b0ee5
Short Description
The code without the fluff boils down to this:
$evm = AeLoader.get_evm()
aem = MiqAeMethod.where(name: 'lib_validation')
data = MiqAeEngine::MiqAeMethod.send(:bodies_and_line_numbers, $evm, aem)
eval(data[0].join("\n"))
- Obtain a working
MiqAeWorkspace
(i.e.$evm
) - Get the Method from the database (MiqAeMethod), with a given path
- Use
MiqAeEngine::MiqAeMethod
to resolve the embedded methods and return the contents as string - evaluate the code in the current context
Usage
ae = AeLoader.new($evm) # if you already have a workspace
ae = AeLoader.new # or let AeLoader create a new workspace
# :include_embedded_methods returns the content of all embedded methods as one long
# string, ready to be evaluated in the current context
eval(ae.include_embedded_methods('StdLib/Automate/Validation/lib_validation'))
# :include_method will also includes the data of the actual method, not only the embedded ones
eval(ae.include_method('StdLib/Automate/Validation/lib_validation'))
# both methods also accept a MiqAeMethod object, in case the URL translation fails
eval(ae.include_method(MiqAeMethod.where(name: 'lib_validation').first))