From 8b234caee4c29f9550c4dc77d02ebd21f8dbfa30 Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Tue, 12 Nov 2024 15:35:11 +0100 Subject: [PATCH] Add Appsignal.configure .env? helper (#1333) --- .../add-appsignal-configure-env--helper.md | 22 +++++++++++++++++++ lib/appsignal/config.rb | 9 ++++++++ spec/lib/appsignal/config_spec.rb | 12 ++++++++++ 3 files changed, 43 insertions(+) create mode 100644 .changesets/add-appsignal-configure-env--helper.md diff --git a/.changesets/add-appsignal-configure-env--helper.md b/.changesets/add-appsignal-configure-env--helper.md new file mode 100644 index 00000000..f33c453a --- /dev/null +++ b/.changesets/add-appsignal-configure-env--helper.md @@ -0,0 +1,22 @@ +--- +bump: patch +type: add +--- + +Add `Appsignal.configure` context `env?` helper method. Check if the loaded environment matches the given environment using the `.env?(:env_name)` helper. + +Example: + +```ruby +Appsignal.configure do |config| + # Symbols work as the argument + if config.env?(:production) + config.ignore_actions << "My production action" + end + + # Strings also work as the argument + if config.env?("staging") + config.ignore_actions << "My staging action" + end +end +``` diff --git a/lib/appsignal/config.rb b/lib/appsignal/config.rb index 3210de76..5ae6a857 100644 --- a/lib/appsignal/config.rb +++ b/lib/appsignal/config.rb @@ -609,6 +609,15 @@ def env @config.env end + # Returns true if the given environment name matches the loaded + # environment name. + # + # @param given_env [String, Symbol] + # @return [TrueClass, FalseClass] + def env?(given_env) + env == given_env.to_s + end + def activate_if_environment(*envs) self.active = envs.map(&:to_s).include?(env) end diff --git a/spec/lib/appsignal/config_spec.rb b/spec/lib/appsignal/config_spec.rb index f4c705be..8edcfac8 100644 --- a/spec/lib/appsignal/config_spec.rb +++ b/spec/lib/appsignal/config_spec.rb @@ -1367,6 +1367,18 @@ def log_file_path expect(dsl.env).to eq("production") end + describe "#env?" do + it "returns true if the env matches" do + expect(dsl.env?("production")).to be(true) + expect(dsl.env?(:production)).to be(true) + end + + it "returns false if the env doesn't match" do + expect(dsl.env?("staging")).to be(false) + expect(dsl.env?(:staging)).to be(false) + end + end + it "sets config options" do dsl.push_api_key = "my push key" dsl.ignore_actions = ["My ignored action"]