Module: Doorkeeper::JWT::Config::Option

Included in:
Doorkeeper::JWT::Config
Defined in:
lib/doorkeeper/jwt/config.rb

Instance Method Summary collapse

Instance Method Details

#extended(base) ⇒ Object



111
112
113
# File 'lib/doorkeeper/jwt/config.rb', line 111

def extended(base)
  base.send(:private, :option)
end

#option(name, options = {}) ⇒ Object

Defines configuration options.

When you call option, it defines two methods. One method will take place in the Config class and the other method will take place in the Builder class.

The name parameter will set both builder method and config attribute. If the :as option is defined, the builder method will be the specified option while the config attribute will be the name parameter.

If you want to introduce another level of config DSL you can define builder_class parameter. Builder should take a block as the initializer parameter and respond to function build that returns the value of the config attribute.

Options

  • :as

    Set the builder method that goes inside configure block.

  • :default

    The default value in case no option was set.

Examples

option :name
option :name, as: :set_name
option :name, default: 'My Name'
option :scopes, builder_class: ScopesBuilder


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/doorkeeper/jwt/config.rb', line 81

def option(name, options = {})
  attribute = options[:as] || name
  attribute_builder = options[:builder_class]
  attribute_symbol = :"@#{attribute}"

  Builder.instance_eval do
    define_method name do |*args, &block|
      # TODO: is builder_class option being used?
      value =
        if attribute_builder
          attribute_builder.new(&block).build
        else
          block || args.first
        end

      @config.instance_variable_set(attribute_symbol, value)
    end
  end

  define_method attribute do |*|
    if instance_variable_defined?(attribute_symbol)
      instance_variable_get(attribute_symbol)
    else
      options[:default]
    end
  end

  public attribute
end