Last active
August 29, 2015 14:28
-
-
Save 0x5d/7ba5854ba8e15deee7bc to your computer and use it in GitHub Desktop.
An example demonstrating the block configuration pattern in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'singleton' | |
# The gem's main module. | |
module AwesomeGem | |
# A class method which yields the configuration to a block. | |
def self.configure | |
yield Configuration.instance if block_given? | |
end | |
# A class method to access the configuration instance. | |
def self.configuration | |
Configuration.instance | |
end | |
# Our gem's configuration class, includin Ruby's singleton model. | |
class Configuration | |
include Singleton | |
# The list of configuration options our module will have. | |
CONFIG_OPTIONS = [:do_magic, :awesome_string] | |
attr_writer(*CONFIG_OPTIONS) | |
# Accessor methods for the options. | |
def do_magic? | |
@do_magic | |
end | |
def awesome_string | |
@awesome_string | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment