polaroid 0.0.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a8763fe87a6c9ee53c561598522032c5fc8cb1e
4
- data.tar.gz: 8c47aaaba2d6f15ea7e8ce2489fc5cdfea84b7ef
3
+ metadata.gz: 3302b890c49e2f67d5b14ffd22d6407892c07f6a
4
+ data.tar.gz: 20d01b457f9e55a130bde3d9f55c7d566602bfc8
5
5
  SHA512:
6
- metadata.gz: 136cfacbbe47466f4bcd335c94d00cef7745c3dbe505284fbd85132c7663e5ef05e027974f8c7d4ca572cd21fc8f669226cb46dc8e344711015c2298cc27023f
7
- data.tar.gz: 4523e17ffa547f4cd3d56a0d17a650cd4e17012c5ca4aabdf0d726e8ce888db84784a936f68013f7513c9c4ed5b6c8d061bcf37dd480b1c2303d4e91047efc6a
6
+ metadata.gz: f6e09732f7c1ceb1b3a2cc2948acd628bfd1fe9aca9f14a60e223c14a4240405e0e3eb0fec8529c550463962cc837bd31b3d9a9d59be03dbc077ea2eef15b5c8
7
+ data.tar.gz: 74cb77ff63271c629e9b0a12633519afec08c5f8025034cd6883230d26b69e92deee5645e42bbc64a5506c68e4fd5fec3d0a9c3eae2325f266c2530d5d6d05df
@@ -32,17 +32,29 @@ private #######################################################################
32
32
 
33
33
 
34
34
  module ClassMethods
35
- def build_from_snapshot(snapshot, format = :hash)
36
- case format
37
- when :hash
38
- snapshot_hash = snapshot.map.with_object({}) do |(k, v), hash|
39
- hash[k.to_sym] = v
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
@@ -1,3 +1,3 @@
1
1
  class Polaroid < Module
2
- VERSION = "0.0.5"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polaroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Kwiatkowski