ruby on rails - access configuration in gem -
i'm trying build gem company log stats can dropped of rails apps throughout company. part of gem need start statsd client , assign global variable. right looks this:
$stats = statsd.new('127.0.0.1', 8125).tap{|sd| sd.namespace = 'some_hard_coded_namespace'} currently have @ bottom of main gem file gets called on start , available rails app utilize start logging data. problem able specified in configuration instead of beingness hard-coded gem. first thought this:
def get_namespace homecoming rails.application.config.stats_logging_namespace rescue exception => e homecoming rails.application.class.to_s.split("::").first.underscore.dasherize end but since gem loaded before config/application.rb can't access variables. there proper way allow user configure variables gem? instance maybe in yaml file?
the standard way configure gem have initializer in application gem used.
config/initializers/my_gem.rb:
mygem.configure |config| config.namespace = 'the_namespace' end see reply illustration on how can implement configuration api in gem: how utilize activesupport::configurable rails engine
ruby-on-rails ruby ruby-on-rails-4 gem
No comments:
Post a Comment