I have an API wrapper with the following code:
module MyApi
mattr_accessor :app_id
end
I'm trying to set the value of app_id
in an initializer like this:
# config/initializers/my_api.rb
MyApi.app_id = Rails.application.secrets.my_api["app_id"]
In my secrets file I have:
# secrets.yml
development:
my_api:
app_id: foo
But when I open my console or run tests I get this:
master ✗ $ rails c
Loading development environment (Rails 4.1.4)
2.2.0 :001 > MyApi.app_id
=> nil
2.2.0 :002 > MyApi.app_id = Rails.application.secrets.my_api["app_id"]
=> "foo"
2.2.0 :003 > MyApi.app_id
=> "foo"
I've followed gem readme's about using initializers but have never implemented one myself. Is there something I'm missing here? Using Rails 4.1.4.
Try adding this inside your module:
And in the initializer change it to have a setup block:
I have only built a few gems with initializers, but this worked for me.