-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
125 lines (99 loc) · 3.24 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module Configurable
module ConfigClasses
# ConfigClasses are used by ConfigHash to delegate get/set configs on a
# receiver and to map configs between user interfaces.
class Config
class << self
attr_reader :matchers
def inherited(base) # :nodoc:
unless base.instance_variable_defined?(:@matchers)
base.instance_variable_set(:@matchers, matchers.dup)
end
end
def subclass(*matchers, &caster)
subclass = Class.new(self)
subclass.matches(*matchers)
subclass.cast(&caster)
subclass
end
def cast(&block)
define_method(:cast, &block) if block
self
end
def uncast(&block)
define_method(:uncast, &block) if block
self
end
def errors(&block)
define_method(:errors, &block) if block
self
end
def matches(*matchers)
@matchers = matchers
self
end
def matches?(value)
matchers.any? {|matcher| matcher === value }
end
end
matches()
# The config key, used as a hash key for access.
attr_reader :key
# The config name used in interfaces where only word-based names are
# appropriate. Names are strings consisting of only word characters.
attr_reader :name
# The reader method called on a receiver during get.
attr_reader :reader
# The writer method called on a receiver during set.
attr_reader :writer
# The default config value.
attr_reader :default
# A hash of information used to render self in various contexts.
attr_reader :desc
# Initializes a new Config. Specify attributes like default, reader,
# writer, type, etc. within attrs.
def initialize(key, attrs={})
@key = key
@name = attrs[:name] || @key.to_s
check_name(@name)
@default = attrs[:default]
check_default(@default)
@reader = (attrs[:reader] || name).to_sym
@writer = (attrs[:writer] || "#{name}=").to_sym
@desc = attrs[:desc] || {}
end
def [](key)
desc[key]
end
# Calls reader on the receiver and returns the result.
def get(receiver)
receiver.send(reader)
end
# Calls writer on the receiver with the value.
def set(receiver, value)
receiver.send(writer, value)
end
def cast(input)
input
end
def uncast(value)
value
end
# Returns an inspect string.
def inspect
"#<#{self.class}:#{object_id} key=#{key} name=#{name} default=#{default.inspect} reader=#{reader} writer=#{writer} >"
end
protected
def check_name(name) # :nodoc
unless name.kind_of?(String)
raise "invalid name: #{name.inspect} (not a String)"
end
unless name =~ /\A\w+\z/
raise NameError.new("invalid name: #{name.inspect} (includes non-word characters)")
end
end
def check_default(default) # :nodoc:
end
end
end
end