Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

shared global ruby configuration dsl example

View global.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
module App
module Global
extend self
 
def self.configure(&block)
instance_eval(&block)
end
 
def [](key)
config[key]
end
 
private
 
def set(key, value)
config[key] = value
end
 
def config
@config ||= Hash.new
end
 
def method_missing(sym, *args)
if sym.to_s =~ /(.+)=$/
config[$1] = args.first
else
config[sym]
end
end
 
end
end
 
App::Global.configure do
set :a, 1
end
 
class A
def test
puts("A: a=#{App::Global.a.inspect}")
puts("A: b=#{App::Global.b.inspect}")
puts("A: c=#{App::Global[:c].inspect}")
end
end
 
 
class B
App::Global.configure do
set :b, 2
end
 
def test
App::Global.configure do
set :c, 3
end
 
puts("B: a=#{App::Global[:a].inspect}")
puts("B: b=#{App::Global[:b].inspect}")
puts("B: c=#{App::Global.c.inspect}")
end
end
 
a = A.new
b = B.new
a.test
b.test
a.test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.