Hi, I am writing a gem which is ideally just a rack application and can be integrated with other rack applications.
At the moment in the setup stage of my app I do the following
require "hanami"
module ActivityMonitor
def self.setup
if Hanami::app?
# We are in a hanami environment
else
# we are not in a hanami environment
end
end
end
The other way i can see is to check if HANAMI_ENV is set in the ruby environment.
Is this the best way to detect if hanami has been loaded or not ?
Hi @lgflorentino, thanks for considering Hanami for your gem’s integrations!
Yes, Hanami.app? would work for telling you whether a Hanami app class has been loaded.
You may be doing this already, but if not, it would be good to conditionally require "hanami", otherwise you’ll encounter load errors if the gem isn’t bundled at all.
Please let us know if there’s anything else we can do to help!
I didn’t consider conditionally requiring the hanami library but it makes perfect sense.
What is the condition for including the library ?
My first thought was which comes first. Wouldn’t i require it to it to test if it is needed via Hanami::app? ?
i think conditionally requiring "hanami" is a different problem than checking to see if the Hanami app is loaded. its more a suggestion based on the fact that this is an integration (if not in a Hanami environment, presumably this gem wouldn’t be installed and it would throw an error)
my suggestion would be something like this
require "hanami" if Gem.loaded_specs.has_key? "hanami"
module ActivityMonitor
def self.setup
if (defined? Hanami) && Hanami::app?
# We are in a hanami environment
else
# we are not in a hanami environment
end
end
end
so you’re conditionally loading the gem, avoiding load errors if its not included, and then continues with checking if we’re in a Hanami environment