polaroid 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/polaroid.rb +22 -10
- data/lib/polaroid/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3302b890c49e2f67d5b14ffd22d6407892c07f6a
|
4
|
+
data.tar.gz: 20d01b457f9e55a130bde3d9f55c7d566602bfc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6e09732f7c1ceb1b3a2cc2948acd628bfd1fe9aca9f14a60e223c14a4240405e0e3eb0fec8529c550463962cc837bd31b3d9a9d59be03dbc077ea2eef15b5c8
|
7
|
+
data.tar.gz: 74cb77ff63271c629e9b0a12633519afec08c5f8025034cd6883230d26b69e92deee5645e42bbc64a5506c68e4fd5fec3d0a9c3eae2325f266c2530d5d6d05df
|
data/lib/polaroid.rb
CHANGED
@@ -32,17 +32,29 @@ private #######################################################################
|
|
32
32
|
|
33
33
|
|
34
34
|
module ClassMethods
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
##
|
36
|
+
# This method will try its damnedest to give you a meaningful snapshot object,
|
37
|
+
# but first it needs to somehow get it into a Hash. Either you can let it autodetect
|
38
|
+
# and give it a Hash-like, JSON String-like, or something which responds to #to_h or #to_s,
|
39
|
+
# or you can explicitly pass it the format as :hash or :json.
|
40
|
+
def build_from_snapshot(snapshot, format = :auto)
|
41
|
+
symbolize_keys = ->((key, val), hash) { hash[key.to_sym] = val }
|
42
|
+
from_hash = ->(snap) { snap.each.with_object({}, &symbolize_keys) }
|
43
|
+
from_json = ->(snap) { JSON.parse(snap).each.with_object({}, &symbolize_keys) }
|
44
|
+
snapshot_hash =
|
45
|
+
if :auto == format && snapshot.is_a?(Hash)
|
46
|
+
from_hash.call(snapshot)
|
47
|
+
elsif :auto == format && snapshot.is_a?(String)
|
48
|
+
from_json.call(snapshot)
|
49
|
+
elsif :hash == format
|
50
|
+
from_hash.call(snapshot)
|
51
|
+
elsif :json == format
|
52
|
+
from_json.call(snapshot)
|
53
|
+
elsif snapshot.respond_to?(:to_h)
|
54
|
+
from_hash.call(snapshot)
|
55
|
+
else
|
56
|
+
from_json.call(snapshot.to_s)
|
40
57
|
end
|
41
|
-
when :json
|
42
|
-
snapshot_hash = JSON.parse(snapshot).map.with_object({}) do |(k, v), hash|
|
43
|
-
hash[k.to_sym] = v
|
44
|
-
end
|
45
|
-
end
|
46
58
|
self::Snapshot.new(snapshot_hash)
|
47
59
|
end
|
48
60
|
end
|
data/lib/polaroid/version.rb
CHANGED