Module: Directive

Defined in:
lib/directive.rb,
lib/directive/reader.rb,
lib/directive/version.rb,
lib/directive/evaluator.rb,
lib/directive/config_object.rb,
lib/directive/config_builder.rb,
lib/directive/spec_helper/dsl.rb,
lib/directive/config_delegation.rb,
lib/directive/spec_helper/matchers.rb,
lib/directive/config_builder/double_configure.rb,
lib/directive/spec_helper/matchers/global/delegate_config_to.rb,
lib/directive/spec_helper/matchers/configuration/define_config_option.rb

Overview

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 😱"

Defined Under Namespace

Modules: ConfigDelegation, DoubleConfigure, SpecHelper Classes: ConfigBuilder, ConfigObject, Evaluator, Reader

Constant Summary collapse

VERSION =

This constant is managed by spicerack

"0.29.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Raises:

  • (TypeError)


61
62
63
64
65
# 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

Instance Method Details

#after_configure(&block) ⇒ Object

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!



85
86
87
# File 'lib/directive.rb', line 85

def after_configure(&block)
  _config_builder_class.set_callback(:configure, :after, &block)
end

#before_configure(&block) ⇒ Object

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!



77
78
79
# File 'lib/directive.rb', line 77

def before_configure(&block)
  _config_builder_class.set_callback(:configure, :before, &block)
end

#configDirective::ConfigReader

Returns A read-only object containing configuration options set inside #configure.

Returns:

  • (Directive::ConfigReader)

    A read-only object containing configuration options set inside #configure



69
70
71
# File 'lib/directive.rb', line 69

def config
  _config_builder.reader
end