Class: Doc::ConfigObject
- Inherits:
-
Object
show all
- Defined in:
- lib/doc/config_object.rb
Instance Method Summary
collapse
Constructor Details
#initialize(default_key, *arguments, &block) ⇒ ConfigObject
Returns a new instance of ConfigObject.
3
4
5
6
7
8
9
10
11
12
13
14
|
# File 'lib/doc/config_object.rb', line 3
def initialize(default_key, *arguments, &block)
@hash = {}
arguments = arguments.dup
if arguments.last.is_a?(Hash)
@hash.merge!(arguments.pop)
end
unless arguments.empty?
@hash[default_key] = arguments
end
block.call(self) if block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/doc/config_object.rb', line 28
def method_missing(method, *arguments)
case method.to_s
when /\!$/
check_argument_count arguments, 0
@hash[$`.to_sym] = true
when /\?$/
check_argument_count arguments, 0
@hash[$`.to_sym] && true
else
if arguments.empty?
@hash[method]
else
check_argument_count arguments, 1
@hash[method] = arguments.first
end
end
end
|
Instance Method Details
#[](key) ⇒ Object
16
17
18
|
# File 'lib/doc/config_object.rb', line 16
def [](key)
@hash[key]
end
|
#[]=(key, value) ⇒ Object
20
21
22
|
# File 'lib/doc/config_object.rb', line 20
def []=(key, value)
@hash[key] = value
end
|
#check_options!(required_keys, optional_keys) ⇒ Object
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
|
# File 'lib/doc/config_object.rb', line 46
def check_options!(required_keys, optional_keys)
errors = []
unless (missing_keys = required_keys - keys).empty?
errors << "missing required keys: #{missing_keys.join(', ')}"
end
left_keys = keys - required_keys
optional_keys.each do |key_group|
key_group = Array(key_group)
if key_group.length > 1
if (clashing_keys = keys & key_group).length > 1
errors << "clash of mutually exclusive keys: #{clashing_keys.join(', ')}"
end
end
left_keys -= key_group
end
unless left_keys.empty?
errors << "unknown keys: #{left_keys.join(', ')}"
end
unless errors.empty?
raise ConfigError.new(self, errors.join('; '))
end
end
|
#keys ⇒ Object
24
25
26
|
# File 'lib/doc/config_object.rb', line 24
def keys
@hash.keys
end
|