Skip to content

Commit

Permalink
Merge pull request #5 from MatteoPierro/configure_via_accessors
Browse files Browse the repository at this point in the history
feat: configure gem using accessors
  • Loading branch information
anakinj authored Oct 6, 2022
2 parents 3e08236 + ed67798 commit 518b336
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ JwkLoader.configure do |config|
end
```

or in alternative

```ruby
require "jwt-loader"

JwkLoader.configure do |config|
config.cache = YetAnotherCache.new
config.cache_grace_period = 999
end
```

## Development

After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
10 changes: 10 additions & 0 deletions lib/jwk_loader/config/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def [](key)
registry[key] || (raise ConfigurationNotFound, key)
end

def method_missing(name, *args)
return send(:[]=, name.to_s[0..-2].to_sym, *args) if name.to_s.end_with?("=")

send(:[], name, *args)
end

def respond_to_missing?(_name, _include_private)
true
end

private

def registry
Expand Down
14 changes: 14 additions & 0 deletions spec/jwk-loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ class YetAnotherCache; end
expect(provider.cache).to be_a(YetAnotherCache)
expect(provider.cache_grace_period).to eq(cache_grace_period)
end

context "when using accessors" do
before do
JwkLoader.configure do |cfg|
cfg.cache = cache
cfg.cache_grace_period = cache_grace_period
end
end

it "returns a provider with the given configuration" do
expect(provider.cache).to be_a(YetAnotherCache)
expect(provider.cache_grace_period).to eq(cache_grace_period)
end
end
end
end
end
15 changes: 15 additions & 0 deletions spec/jwk_loader/config/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@
expect { config["key"] }.to raise_error(JwkLoader::Config::ConfigurationNotFound)
end
end

describe ".some_value_123" do
let(:value) { Object.new }

it "adds a cache configuration" do
config.some_value_123 = value
expect(config.some_value_123).to be(value)
end
end

describe ".foo(1)" do
it "raises an ArgumentError" do
expect { config.foo(1) }.to raise_error(ArgumentError)
end
end
end

0 comments on commit 518b336

Please sign in to comment.