Last active
August 29, 2015 14:28
-
-
Save 0x5d/110a0bd8214f22ca4151 to your computer and use it in GitHub Desktop.
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? | |
# Call all the registered listeners after configuration has taken place! | |
Configuration.instance.listeners.each { |listener| listener.call } | |
end | |
# A class method to access the configuration instance. | |
def self.configuration | |
Configuration.instance | |
end | |
# The event to listen to. | |
def self.on_configure(&block) | |
# Add the passed block to the list of listeners. | |
Configuration.instance.add_listener block | |
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) | |
# Add a lambda to the list of listeners. | |
def add_listener(listener_lambda) | |
@listeners << listener_lambda | |
end | |
def listeners | |
@listeners | |
end | |
# 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