Module: Y::Redis::Config::Option
- Included in:
- Y::Redis::Config
- Defined in:
- lib/y/redis/config/option.rb
Overview
Y::Redis configuration option DSL
Class Method Summary collapse
Instance Method Summary collapse
-
#option(name, options = {}) ⇒ Object
Defines configuration option.
Class Method Details
.extended(base) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/y/redis/config/option.rb', line 77 def self.extended(base) return if base.respond_to?(:builder_class) raise Y::Redis::MissingConfigurationBuilderClass, "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.
Options
- :
as
-
Set the builder method that goes inside
configure
block
- :
:default
-
The default value in case no option was set
:builder_class
-
Configuration option builder class
Examples
option :name
option :name, as: :set_name
option :name, default: 'My Name'
option :scopes builder_class: ScopesBuilder
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/y/redis/config/option.rb', line 36 def option(name, = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity attribute = [:as] || name attribute_builder = [:builder_class] builder_class.instance_eval do if method_defined?(name) Kernel.warn "[Y::Redis] Option #{name} already defined and will be overridden" remove_method name end define_method name do |*args, &block| # rubocop:disable Metrics/MethodLength if (deprecation_opts = [:deprecated]) warning = "[Y::Redis] #{name} has been deprecated and will soon be removed" if deprecation_opts.is_a?(Hash) warning = "#{warning}\n#{deprecation_opts.fetch(:message)}" end Kernel.warn(warning) end value = if attribute_builder attribute_builder.new(&block).build else block || args.first end @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 |