Skip to content

Commit

Permalink
Add Appsignal.configure .env? helper (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombruijn authored Nov 12, 2024
1 parent f5381a0 commit 8b234ca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .changesets/add-appsignal-configure-env--helper.md
Original file line number Diff line number Diff line change
@@ -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
```
9 changes: 9 additions & 0 deletions lib/appsignal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/appsignal/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 8b234ca

Please sign in to comment.