Skip to content

Instantly share code, notes, and snippets.

@0x5d
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x5d/7ba5854ba8e15deee7bc to your computer and use it in GitHub Desktop.
Save 0x5d/7ba5854ba8e15deee7bc to your computer and use it in GitHub Desktop.
An example demonstrating the block configuration pattern in Ruby.
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