Created
November 9, 2021 09:36
-
-
Save prdanelli/bcfb45975753b02d8fa7e2b404b29e6c to your computer and use it in GitHub Desktop.
Create a configuration class
This file contains hidden or 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
# Usage: | |
# | |
# Settings.configure do |c| | |
# c.foo = "biz" | |
# c.bar = "boo" | |
# end | |
# Test.config.foo | |
# => "biz" | |
# Test.configured? | |
# => true | |
# Test.reset! | |
# => nil | |
# Test.configured? | |
# => false | |
# Test.config.foo | |
class Settings | |
def self.config | |
@config ||= Struct.new(:foo, :bar).new | |
end | |
def self.configure | |
yield(config) | |
end | |
def self.configured? | |
!!@config | |
end | |
def self.reset! | |
@config = nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment