-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.rb
65 lines (54 loc) · 1.3 KB
/
configuration.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
module YourProjectNamespace
class Configuration
attr_accessor :you_want_to_init
def initialize
@you_want_to_init = ""
end
end
def self.configure
yield configuration
end
def self.configuration
@configuration ||= Configuration.new
end
def self.configuration=(configuration)
@configuration = configuration
end
end
# YourProjectNamespace.configure do |config|
# config.you_want_to_init = 'you can config a new value'
# end
# ===========
# And you can use meta-programming for Configurable
# class SomeClass
# include Configurable.with(:foo, :bar)
# end
# SomeClass.configure do |config|
# config.foo = "wat"
# config.bar = "huh"
# end
# SomeClass.config.foo
# => "wat"
module Configurable
def self.with(*attrs)
# Define anonymous class with the configuration attributes
config_class = Class.new do
attr_accessor *attrs
end
# Define anonymous module for the class methods to be "mixed in"
class_methods = Module.new do
define_method :config do
@config ||= config_class.new
end
def configure
yield config
end
end
# Create and return new module
Module.new do
singleton_class.send :define_method, :included do |host_class|
host_class.extend class_methods
end
end
end
end