Class: Configatron::Store
- Inherits:
-
Object
- Object
- Configatron::Store
show all
- Extended by:
- Forwardable
- Defined in:
- lib/configatron/store.rb
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Store
Returns a new instance of Store.
7
8
9
10
11
|
# File 'lib/configatron/store.rb', line 7
def initialize(attributes = {})
@__locked = false
@attributes = attributes || {}
@attributes.send(:extend, DeepClone)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/configatron/store.rb', line 87
def method_missing(name, *args, &block)
if block_given?
yield self[name]
else
name = name.to_s
if /(.+)=$/.match(name)
return store($1, args[0])
elsif /(.+)!/.match(name)
key = $1
if self.has_key?(key)
return self[key]
else
raise Configatron::UndefinedKeyError.new($1)
end
else
return self[name]
end
end
end
|
Instance Method Details
63
64
65
66
67
68
69
70
71
|
# File 'lib/configatron/store.rb', line 63
def configure_from_hash(hash)
hash.each do |key, value|
if value.is_a?(Hash)
self[key].configure_from_hash(value)
else
store(key, value)
end
end
end
|
#fetch(key, default_value = nil, &block) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/configatron/store.rb', line 38
def fetch(key, default_value = nil, &block)
val = @attributes[key.to_sym]
if val.nil?
if block_given?
val = block.call
elsif default_value
val = default_value
end
store(key, val)
end
if val.is_a?(Configatron::Proc)
val = val.call
end
return val
end
|
#inspect(name = 'configatron') ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/configatron/store.rb', line 107
def inspect(name = 'configatron')
f_out = []
@attributes.each do |k, v|
if v.is_a?(Configatron::Store)
v.inspect("#{name}.#{k}").each_line do |line|
if line.match(/\n/)
line.each_line do |l|
l.strip!
f_out << l
end
else
line.strip!
f_out << line
end
end
else
f_out << "#{name}.#{k} = #{v.inspect}"
end
end
f_out.compact.sort.join("\n")
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
58
59
60
61
|
# File 'lib/configatron/store.rb', line 58
def key?(key)
val = self[key.to_sym]
!val.is_a?(Configatron::Store)
end
|
#lock!(value = true) ⇒ Object
13
14
15
|
# File 'lib/configatron/store.rb', line 13
def lock!(value=true)
@__locked = value
end
|
#nil? ⇒ Boolean
Also known as:
blank?
54
55
56
|
# File 'lib/configatron/store.rb', line 54
def nil?
@attributes.empty?
end
|
#reset! ⇒ Object
17
18
19
|
# File 'lib/configatron/store.rb', line 17
def reset!
@attributes.clear
end
|
#store(key, value) ⇒ Object
Also known as:
[]=
31
32
33
34
35
36
|
# File 'lib/configatron/store.rb', line 31
def store(key, value)
if @__locked
raise Configatron::LockedError.new("Locked! Can not set key #{key}!")
end
@attributes[key.to_sym] = value
end
|
#temp(&block) ⇒ Object
73
74
75
76
77
|
# File 'lib/configatron/store.rb', line 73
def temp(&block)
temp_start
yield
temp_end
end
|
#temp_end ⇒ Object
83
84
85
|
# File 'lib/configatron/store.rb', line 83
def temp_end
@attributes = @__temp
end
|
#temp_start ⇒ Object
79
80
81
|
# File 'lib/configatron/store.rb', line 79
def temp_start
@__temp = @attributes.deep_clone
end
|