Skip to content

Instantly share code, notes, and snippets.

@pat
Last active November 28, 2023 22:50
Show Gist options
  • Select an option

  • Save pat/336b679079fd9b1e469e8051a5c6edfc to your computer and use it in GitHub Desktop.

Select an option

Save pat/336b679079fd9b1e469e8051a5c6edfc to your computer and use it in GitHub Desktop.

Revisions

  1. pat revised this gist Nov 28, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    To ensure it's available for all initialisers, you can require the file within `config/application.rb`
    by adding `require_relative "settings"` as the last line within the Application class definition.

    The defined settings will be available via the Settings constant: e.g. `Settings.default_host`

    - Does not pay attention to `Rails.configuration` / `Rails.application.credentials` - it's expecting all secrets as ENV variables.
    - Requires dry-types and dry-system gems.
    - Allows for typed values (even if the above examples are all strings 😅).
  2. pat revised this gist Nov 28, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,3 +5,4 @@ by adding `require_relative "settings"` as the last line within the Application
    - Requires dry-types and dry-system gems.
    - Allows for typed values (even if the above examples are all strings 😅).
    - Allows for optional values and defaults.
    - Inspired by [Hanami's approach](https://guides.hanamirb.org/v2.0/app/settings/) to such things.
  3. pat revised this gist Nov 28, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    To ensure it's available for all initialisers, you can require the file within `config/application.rb`
    by adding `require_relative "settings"` as the last line within the Application class definition.

    - Does not pay attention to `Rails.configuration` / `Rails.application.credentials` - it's expecting all secrets as ENV variables.
    - Requires dry-types and dry-system gems.
    - Allows for typed values (even if the above examples are all strings 😅).
    - Allows for optional values and defaults.
  4. pat created this gist Nov 28, 2023.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    To ensure it's available for all initialisers, you can require the file within `config/application.rb`
    by adding `require_relative "settings"` as the last line within the Application class definition.

    - Requires dry-types and dry-system gems.
    - Allows for typed values (even if the above examples are all strings 😅).
    - Allows for optional values and defaults.
    31 changes: 31 additions & 0 deletions settings.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # config/settings.rb

    # frozen_string_literal: true

    require "dry/system/provider_sources/settings/config"
    require "dry/types"

    class SettingsSchema < Dry::System::ProviderSources::Settings::Config
    include Dry.Types()

    setting :bugsnag_api_key, constructor: Strict::String, default: "placeholder"
    setting :bugsnag_release_stage, constructor: Strict::String, default: "production"

    setting :default_host, constructor: Strict::String, default: "calmcalendar.com"
    setting :postmark_api_token, constructor: Strict::String, default: "placeholder"

    setting :sidekiq_username, constructor: Strict::String.optional, default: nil
    setting :sidekiq_password, constructor: Strict::String.optional, default: nil

    def verify!
    config._settings.each do |setting|
    self[setting.name]
    rescue Dry::Types::ConstraintError
    raise ArgumentError, "Missing environment variable #{setting.name.upcase}"
    end

    finalize!
    end
    end

    Settings = SettingsSchema.load(root: Rails.root, env: Rails.env).tap(&:verify!)