module Directive
NOTE: This is still a pre-release feature! Use at your own risk - it may change before being released.
A utility for creating read-only gem configuration singletons.
Usage:
# In your gem: module SomeGem module Configuration extend Directive configuration_options do option :some_config_option option :some_option_with_a_default, default: "I probably know what's best" nested :whats_behind do option :door_one, default: "It's a goat" option :door_two, default: "Another goat" option :door_three, default: "It's a car!" end end end end # Then, in the application using the gem: SomeGem::Configuration.configure do |config| config.some_config_option = 12345 config.some_option_with_a_default = "Nope, you really don't" config.whats_behind do |nested| nested.door_one = "It's a boat!" nested.door_three = "The teletubbies on repeat 😱" end end # Then, back in your gem code: puts Configuration.config.some_config_option => 12345 puts Configuration.config.whats_behind.door_one => "It's a boat!" # Or, if you want to select dynamically: doors = %i[door_one door_two door_three] Configuration.config.config_eval(whats_behind, doors.sample).read => "The teletubbies on repeat 😱"
Constants
- VERSION
This constant is managed by spicerack
Public Class Methods
extended(base)
click to toggle source
Calls superclass method
# File lib/directive.rb, line 61 def extended(base) raise TypeError, "#{base} is a class; it must be a module to use Directive" if base.is_a?(Class) super end
Public Instance Methods
after_configure(&block)
click to toggle source
Run a callback after the configure block is evaluated.
Note: if configure is called multiple times for your gem, this block will get run each time!
# File lib/directive.rb, line 85 def after_configure(&block) _config_builder_class.set_callback(:configure, :after, &block) end
before_configure(&block)
click to toggle source
Run a callback before the configure block is evaluated.
Note: if configure is called multiple times for your gem, this block will get run each time!
# File lib/directive.rb, line 77 def before_configure(&block) _config_builder_class.set_callback(:configure, :before, &block) end
config()
click to toggle source
@return [Directive::ConfigReader] A read-only object containing configuration options set inside {#configure}
# File lib/directive.rb, line 69 def config _config_builder.reader end
Private Instance Methods
_config_builder()
click to toggle source
# File lib/directive.rb, line 95 def _config_builder @_config_builder ||= _config_builder_class.new end
_config_builder_class()
click to toggle source
# File lib/directive.rb, line 99 def _config_builder_class @_config_builder_class ||= Class.new(ConfigBuilder).tap do |klass| const_set(:ConfigBuilder, klass) end end
configuration_options(&block)
click to toggle source
# File lib/directive.rb, line 91 def configuration_options(&block) _config_builder.instance_exec(&block) end