Sutto-perennial 0.2.4.0 → 0.2.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/perennial/nash.rb +57 -1
- data/lib/perennial/settings.rb +2 -0
- data/lib/perennial.rb +1 -1
- metadata +1 -1
data/lib/perennial/nash.rb
CHANGED
@@ -1,7 +1,20 @@
|
|
1
|
+
require 'yaml'
|
1
2
|
module Perennial
|
2
3
|
# A ninja hash. Like OpenStruct, but better
|
3
4
|
class Nash
|
4
5
|
|
6
|
+
def self.load_file(path)
|
7
|
+
n = self.new
|
8
|
+
if File.file?(path) && File.readable?(path)
|
9
|
+
contents = YAML.load_file(path)
|
10
|
+
end
|
11
|
+
if contents.is_a?(Hash)
|
12
|
+
contents.to_hash
|
13
|
+
else
|
14
|
+
new(:data => contents).normalized
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
5
18
|
attr_reader :table
|
6
19
|
|
7
20
|
def initialize(initial = {})
|
@@ -104,7 +117,7 @@ module Perennial
|
|
104
117
|
|
105
118
|
def inspect
|
106
119
|
str = ""
|
107
|
-
if
|
120
|
+
if Thread.current[:inspect_stack].nil?
|
108
121
|
Thread.current[:inspect_stack] = [self]
|
109
122
|
str = _inspect
|
110
123
|
Thread.current[:inspect_stack] = nil
|
@@ -129,9 +142,52 @@ module Perennial
|
|
129
142
|
str << ">"
|
130
143
|
return str
|
131
144
|
end
|
145
|
+
|
146
|
+
def hash
|
147
|
+
@table.hash
|
148
|
+
end
|
149
|
+
|
150
|
+
def normalized(n = nil)
|
151
|
+
item = nil
|
152
|
+
if Thread.current[:normalized].nil?
|
153
|
+
n = self.class.new
|
154
|
+
Thread.current[:normalized] = {self => n}
|
155
|
+
item = normalize_nash(n)
|
156
|
+
Thread.current[:normalized] = nil
|
157
|
+
else
|
158
|
+
if Thread.current[:normalized].has_key?(self)
|
159
|
+
return Thread.current[:normalized][self]
|
160
|
+
else
|
161
|
+
n = self.class.new
|
162
|
+
Thread.current[:normalized][self] = n
|
163
|
+
item = normalize_nash(n)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
item
|
167
|
+
end
|
132
168
|
|
133
169
|
protected
|
134
170
|
|
171
|
+
def normalize_nash(n = self.class.new)
|
172
|
+
each_pair do |k, v|
|
173
|
+
n[k] = normalize_item(v)
|
174
|
+
end
|
175
|
+
return n
|
176
|
+
end
|
177
|
+
|
178
|
+
def normalize_item(i)
|
179
|
+
case i
|
180
|
+
when Hash
|
181
|
+
self.class.new(i).normalized
|
182
|
+
when Array
|
183
|
+
i.map { |v| normalize_item(v) }
|
184
|
+
when self.class
|
185
|
+
i.normalized
|
186
|
+
else
|
187
|
+
i
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
135
191
|
def method_missing(name, *args, &blk)
|
136
192
|
name = name.to_s
|
137
193
|
case name.to_s[-1]
|
data/lib/perennial/settings.rb
CHANGED
data/lib/perennial.rb
CHANGED