perennial 1.0.0.1 → 1.0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/perennial/nash.rb +20 -3
- data/lib/perennial.rb +1 -1
- metadata +1 -1
data/lib/perennial/nash.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'time'
|
3
|
+
|
2
4
|
module Perennial
|
3
5
|
# A ninja hash. Like OpenStruct, but better
|
4
6
|
class Nash
|
5
7
|
|
8
|
+
cattr_accessor :parse_times
|
9
|
+
@@parse_times = true
|
10
|
+
|
6
11
|
def self.load_file(path)
|
7
12
|
n = self.new
|
8
13
|
if File.file?(path) && File.readable?(path)
|
@@ -19,7 +24,10 @@ module Perennial
|
|
19
24
|
|
20
25
|
def initialize(initial = {})
|
21
26
|
@table = {}
|
22
|
-
initial.to_hash.each_pair
|
27
|
+
initial.to_hash.each_pair do |k,v|
|
28
|
+
v = Time.parse(v) if @@parse_times && k.to_s =~ /_at$/ && v.is_a?(String)
|
29
|
+
self[k] = v
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
def [](key)
|
@@ -35,7 +43,7 @@ module Perennial
|
|
35
43
|
end
|
36
44
|
|
37
45
|
def id
|
38
|
-
|
46
|
+
@table.has_key?(:id) ? @table[:id] : super
|
39
47
|
end
|
40
48
|
|
41
49
|
def dup
|
@@ -165,6 +173,10 @@ module Perennial
|
|
165
173
|
end
|
166
174
|
item
|
167
175
|
end
|
176
|
+
|
177
|
+
def to_nash
|
178
|
+
self
|
179
|
+
end
|
168
180
|
|
169
181
|
protected
|
170
182
|
|
@@ -206,7 +218,6 @@ module Perennial
|
|
206
218
|
name.to_sym
|
207
219
|
end
|
208
220
|
|
209
|
-
|
210
221
|
end
|
211
222
|
end
|
212
223
|
|
@@ -214,4 +225,10 @@ class Hash
|
|
214
225
|
def to_nash
|
215
226
|
Perennial::Nash.new(self)
|
216
227
|
end
|
228
|
+
end
|
229
|
+
|
230
|
+
class NilClass
|
231
|
+
def to_nash
|
232
|
+
Perennial::Nash.new
|
233
|
+
end
|
217
234
|
end
|
data/lib/perennial.rb
CHANGED