Created
September 2, 2011 20:37
-
-
Save willb/1189849 to your computer and use it in GitHub Desktop.
Example of the quiescent gem
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 'quiescent' | |
class Foo | |
include Quiescent | |
# This declares a constant named PostCode that | |
# quiesces to a default value of 53706 unless | |
# another is provided before the first time it | |
# is read | |
quiescent :PostCode, 53706 | |
# Let's assume that the awesome features are off | |
# by default. | |
quiescent :EnableTotallyAwesomeFeature, false | |
quiescent :EnableSlightlyLessAwesomeFeature, false | |
# This declares a constant named LazyThrees that | |
# quiesces to a list of all natural numbers less than | |
# 100 that are divisible by three, as calculated | |
# in the block, unless another value is provided. | |
# The block argument will execute at most once. | |
quiescent :LazyThrees do | |
(1..100).to_a.select {|x| x % 3 == 0} | |
end | |
# In this method, we'll see how to force quiescents | |
# to quiesce by giving them values and reading their | |
# values. | |
def self.setup | |
# We only want to do this once | |
return if @setup_done | |
@setup_done = true | |
puts "The postal code is #{Foo::PostCode}" | |
# You can provide non-default values with the | |
# quiesce method... | |
Foo.quiesce(:EnableTotallyAwesomeFeature, "sometimes") | |
# ...or by using a special CONSTNAME= method, which | |
# will be intercepted by method_missing. | |
Foo.EnableSlightlyLessAwesomeFeature = true | |
# Note that this only works for names corresponding | |
# to declared quiescing constants... | |
begin | |
Foo.EnableCrummyFeature = true | |
rescue Exception | |
puts("whoa, failure in aisle 47") | |
end | |
# ...and only once for each quiescing constant. | |
begin | |
Foo.EnableSlightlyLessAwesomeFeature = false | |
rescue Exception | |
puts("nice try, pal") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment