@@ -4,8 +4,46 @@ module Grape
44 module Jwt
55 module Authentication
66 # The configuration for the Grape JWT authentication concern.
7- class Configuration
8- include ActiveSupport ::Configurable
7+ class Configuration < ActiveSupport ::OrderedOptions
8+ # Track our configurations settings (+Symbol+ keys) and their defaults
9+ # as lazy-loaded +Proc+'s values
10+ class_attribute :defaults ,
11+ instance_reader : true ,
12+ instance_writer : false ,
13+ instance_predicate : false ,
14+ default : { }
15+
16+ # Create a new +Configuration+ instance with all settings populated
17+ # with their respective defaults.
18+ #
19+ # @param args [Hash{Symbol => Mixed}] additional settings which
20+ # overwrite the defaults
21+ # @return [Configuration] the new configuration instance
22+ def initialize ( **args )
23+ super ( )
24+ defaults . each { |key , default | self [ key ] = instance_exec ( &default ) }
25+ merge! ( **args )
26+ end
27+
28+ # A simple DSL method to define new configuration accessors/settings
29+ # with their defaults. The defaults can be retrieved with
30+ # +Configuration.defaults+ or +Configuration.new.defaults+.
31+ #
32+ # @param name [Symbol, String] the name of the configuration
33+ # accessor/setting
34+ # @param default [Mixed, nil] a non-lazy-loaded static value, serving
35+ # as a default value for the setting
36+ # @param block [Proc] when given, the default value will be lazy-loaded
37+ # (result of the Proc)
38+ def self . config_accessor ( name , default = nil , &block )
39+ # Save the given configuration accessor default value
40+ defaults [ name . to_sym ] = block || -> { default }
41+
42+ # Compile reader/writer methods so we don't have to go through
43+ # +ActiveSupport::OrderedOptions#method_missing+.
44+ define_method ( name ) { self [ name ] }
45+ define_method ( "#{ name } =" ) { |value | self [ name ] = value }
46+ end
947
1048 # The authenticator function which must be defined by the user to
1149 # verify the given JSON Web Token. Here comes all your logic to lookup
0 commit comments