Module: PadlockAuth::Config::Option
- Included in:
- PadlockAuth::Config, Token::Strategy
- Defined in:
- lib/padlock_auth/config/option.rb
Overview
PadlockAuth configuration option DSL.
Adds configuration methods to a builder class which will be used to configure the object.
Adds accessor methods to the object being configured.
Class Method Summary collapse
-
.extended(base) ⇒ Object
Uses extended hook to ensure builder_class is defined.
Instance Method Summary collapse
-
#option(name, options = {}) ⇒ Object
Defines configuration option.
Class Method Details
.extended(base) ⇒ Object
Uses extended hook to ensure builder_class is defined.
Implementing classes should define a builder_class method that returns a builder class.
102 103 104 105 106 107 |
# File 'lib/padlock_auth/config/option.rb', line 102 def self.extended(base) return if base.respond_to?(:builder_class) raise NotImplementedError, "Define `self.builder_class` method for #{base} that returns your custom Builder class to use options DSL!" end |
Instance Method Details
#option(name, options = {}) ⇒ Object
Defines configuration option
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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/padlock_auth/config/option.rb', line 64 def option(name, = {}) attribute = [:as] || name builder_class.instance_eval do if method_defined?(name) Kernel.warn "[PADLOCK_AUTH] Option #{self.name}##{name} already defined and will be overridden" remove_method name end define_method name do |*args, &block| if (deprecation_opts = [:deprecated]) warning = "[PADLOCK_AUTH] #{self.class.name}##{name} has been deprecated and will soon be removed" warning = "#{warning}\n#{deprecation_opts.fetch(:message)}" if deprecation_opts.is_a?(Hash) Kernel.warn(warning) end value = block || args.first config.instance_variable_set(:"@#{attribute}", value) end end define_method attribute do |*_args| if instance_variable_defined?(:"@#{attribute}") instance_variable_get(:"@#{attribute}") else [:default] end end public attribute end |