colon_rocket 0.0.2 → 0.0.3

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 +29 -0
  3. metadata +8 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48ea412dc16687033d43af6ce9609e3e8e88a74e8ad19827f5f2808f8e31a34f
4
- data.tar.gz: 3053e8136b723cd5df10730ef70ef0d24b4a4216a08dcdd21894e9eeb63bffb8
3
+ metadata.gz: d77a6bce9f3606360b7fb1442ffa888554a79f56f8ad111c4c468ec6ff0341f0
4
+ data.tar.gz: b94f011f4cf52af5bc0841dcfe3e2f4fee40f1f58cb5a106ab758512aee8059a
5
5
  SHA512:
6
- metadata.gz: b88391ed022f5ec217f48af8deaefc807a66b4d0448442cda1f88275a74bf8f071e0aff92965df87ae0a353d9393b678d1b519da2271c7d840926dffde3dd576
7
- data.tar.gz: a0f4ff169262f5b88f118a62255aa789b27f3a92f88741d25d2c9384f2ee229635c1db2cededfa48440e55b0b026568b22fb6f777663d6d888fc569d29c8fb68
6
+ metadata.gz: 568c65f54bda37b27b76870259bee729296cdb5d2760eabee209ed1afb39a4b54c25c43cddcf0b7cecebfe08b04d5d93b20b65028ff4b3b833f6b17dcd2afe6f
7
+ data.tar.gz: 6a9921a1484ab6e084fbba6701ed2a02ed092195121bada358bb7fa2c7ebe15281263579d7c3d586ca922017d6980bc6815015f4b55ad6b6550cddbd4e6923ab
@@ -0,0 +1,29 @@
1
+ class ColonRocket
2
+ class OverlappingKeysException < StandardError; end
3
+
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
10
+ end
11
+
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}"
25
+ end
26
+ end
27
+
28
+ private_class_method :collision_detected, :handle_collision
29
+ end
metadata CHANGED
@@ -1,26 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colon_rocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-11-09 00:00:00.000000000 Z
11
+ date: 2018-12-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: When you need to drop json, but you have a ruby hash that doesn't play
14
- nicely. ColonRocket.blastoff($HASH) will "puts" a json ready hash.
13
+ description: ColonRocket.blastoff(RUBY_HASH)
15
14
  email: stevepentler@gmail.com
16
15
  executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
- files: []
18
+ files:
19
+ - lib/colon_rocket.rb
20
20
  homepage: http://rubygems.org/gems/colon_rocket
21
21
  licenses:
22
22
  - MIT
23
- metadata: {}
23
+ metadata:
24
+ source_code_uri: https://github.com/stevepentler/colon_rocket
24
25
  post_install_message:
25
26
  rdoc_options: []
26
27
  require_paths:
@@ -40,5 +41,5 @@ rubyforge_project:
40
41
  rubygems_version: 2.7.6
41
42
  signing_key:
42
43
  specification_version: 4
43
- summary: Hash rockets to colons, for json.
44
+ summary: puts a Ruby hash to your shell as a valid JSON object (without hash rockets)
44
45
  test_files: []