How To Pass A Ruby Object Between Plugins?

Hi,

I am in the middle of teasing apart some plugins that I’ve been working on and have hit a snag in modularizing some of the functionality I have.

I currently do some extra markdown/file parsing/converting whose data is stored in a data structure, which then generates appropriate corresponding graph data. I want to pull out the graph functionality into its own plugin and it would be really helpful if I could basically pass a ruby object from the parsing plugin to the graph plugin.

tldr; How can I pass a ruby object from one jekyll plugin to another?

I’ve tried googling how to attach data/objects to the site object, but haven’t found anything useful for this particular case.

Well, this might end up being a rubber-ducking :duck: post:

I remembered someone else managed to add attributes to jekyll classes, which I mimicked to get something like this:

module Jekyll
  class Site
    attr_accessor :extra_attribute
  end

  class Plugin < Jekyll::Generator
    def generate(site)
      @site = site
      @site.extra_attribute = "Some extra data here"
    end
  end
end

So, when the second plugin runs extra_attribute will exist and be accessible. (Of course, “Some extra data here” would be an object, not a string)

Would still appreciate feedback on whether this will prove problematic though.

Yes you can have one plugin write data to site and another plugin read it. But if you can’t guarantee the order the plugins run then it might not work.

Another approach is to write to a JSON file and read it. Again the ordering issue.

Maybe you can have plugin A which does nothing alone (or maybe it does something anyway). And then plugin B imports code from plugin A and runs it. Then you have a guaranteed top level that runs first and then can send and receive data with the lower plugin

I have a plugin script which I have here to add to site data after it has finished internal calls to other scripts in plugins directory.

That loads the process.rb script in the same directory.

Which in turn loads request.rb

1 Like