colon_rocket 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/colon_rocket.rb +14 -20
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ed7f151985b25c1f602bcb776f29dca49fce26ce236c580b9b1d794e3c11bca
4
- data.tar.gz: df0f8e5eeac0a5d36f35797cdfa92672d64a5569092475c8b3a530e88004bdb6
3
+ metadata.gz: 2ecb70c4c2d3a1c3103b9f932f0a6cde1cc7fb3960b4922fef3cd4c7f903789c
4
+ data.tar.gz: 16fec56eb20c4800fdff6198656dd154e16e6a46fbc98bc21f2a04ecbea98d10
5
5
  SHA512:
6
- metadata.gz: 97d0c4b1ac3453620a82c88853ce5a164e39c81abafbcfc24667758090da6038dc82a17915581fce21410ec6a37ceb322105e4cbb17386e9d11c1af834818d23
7
- data.tar.gz: e5c8f2980b90f7b909ec096d313eb299f1aa1b800132f6a4953dcacaea609f1c1b12a3e905944e9de48d7a02cecb6c22af1f03815045c968b8867fb620910eb8
6
+ metadata.gz: e61f91c6de80c5f3d9e3ce8186f0d67a4bc782500c26722557706d0c49107a1a003f38fa3c8d7e0ecb204fa3212511b3f4475994d616a5b24b6c53ab025feab1
7
+ data.tar.gz: dc1d1577bdfbe9179db7c0a6c6c0d0f13a2f8d1d829469c1e607cfa12a661782591a8c739b1f5ba7fe63dcdd73fa35305d126d7aa28e5f258b61c55f59cd7c23
@@ -1,29 +1,23 @@
1
+ require 'JSON'
1
2
  class ColonRocket
2
3
  class OverlappingKeysException < StandardError; end
3
4
 
4
- def self.blastoff(hash, overwrite_collisions=false, print_collisions=true)
5
- json = hash.map.with_object({}) do |val, obj|
6
- handle_collision(val, obj, overwrite_collisions, print_collisions) if collision_detected(val, obj)
7
- obj[val.first.to_s] = val.last.to_s
8
- end.to_s.gsub("\"=>\"", "\": \"")
9
- puts json
5
+ def self.blastoff(hash)
6
+ detect_collisions(hash.keys)
7
+ puts (hash.to_json)
10
8
  end
11
9
 
12
- # private - must be explicitly defined for class methods
13
-
14
- def self.collision_detected(val, obj)
15
- stringified_key = val.first.to_s
16
- obj.has_key?(stringified_key)
17
- end
18
-
19
- def self.handle_collision(val, obj, overwrite_collisions, print_collisions)
20
- if overwrite_collisions == false
21
- raise OverlappingKeysException, 'Collision detected, identical key found as String and Symbol, example { a: "B", "a" => "NOT B" } Pass in a second argument of true (boolean) to overwrite to the final positional value'
22
- elsif print_collisions == true
23
- stringified_key = val.first.to_s
24
- puts "Collision detected, reassigning \"#{stringified_key}\" from #{obj[stringified_key]} to #{val.last.to_s}"
10
+ def self.detect_collisions(hash_keys)
11
+ if hash_keys.length != hash_keys.map(&:to_s).uniq.length
12
+ duplicate_keys = hash_keys.map.
13
+ with_object(Hash.new(0)) { |key, obj| obj[key.to_s] += 1 }.
14
+ select { |key, val| val > 1 }
15
+ puts "~~~~~duplicate key(s) detected~~~~~: #{duplicate_keys}"
16
+ true
25
17
  end
26
18
  end
27
19
 
28
- private_class_method :collision_detected, :handle_collision
20
+ private_class_method :detect_collisions
29
21
  end
22
+
23
+ ColonRocket.blastoff({ a: 1, "a" => { b: 2, "b" => 3 } })
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colon_rocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pizza Steve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-30 00:00:00.000000000 Z
11
+ date: 2019-01-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: puts a Ruby Hash to your shell as a valid JSON object (without hash rockets)
14
14
  email: stevepentler@gmail.com