Load embedded Methods in rails console


#1

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))

#2

This is pretty cool @buc ! I could see us making much of this more accessible from the Rails console directly, so you don’t have to resort to calling .send on various private methods. @mkanoor @tinaafitz Thoughts?


#3

Thanks :slight_smile:

While we are thinking about opening some internals to rails console, maybe other people have some rails console hacks, they want to share?

  • We sometimes set the retry counter of a state machine back to 0
  • One thing we discussed last week was the ability set ae_next_state from rails, allowing us to move the request to an earlier step in the state machine

#4

We came up with one more use-case:

  • Setting StateVars of a workflow from rails.