plissken 1.1.0 → 1.2.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 +4 -4
- data/README.md +13 -3
- data/VERSION +1 -1
- data/lib/plissken.rb +3 -2
- data/lib/plissken/ext/array/to_snake_keys.rb +5 -0
- data/lib/plissken/ext/hash/to_snake_keys.rb +1 -43
- data/lib/plissken/methods.rb +50 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fde01f6be61447c2fc968aeafceea47285f0d22a
|
4
|
+
data.tar.gz: 2ed836cb7a1d7d6becf05b9be74fc77b21138b44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d623c90c8468a670de0cc2911e728c072b88069f47510f07688747d460507476c7c38e071cf660803772b017d748c0e3180f638a38a6468a88fb6477c2ed7fd
|
7
|
+
data.tar.gz: 7c6aa0a92878a03792cec6f503859e43902e5c2d2676e6b2680688a431c5fed6797fed043deee9c15d43e18dab4f26ce2687345e3613f04bcb46b22aecd218bd
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Have you ever needed to automatically convert JSON-style `camelBack` or `CamelCa
|
|
4
4
|
|
5
5
|
Plissken to the rescue.
|
6
6
|
|
7
|
-
This gem recursively converts all camelBack or CamelCase keys in a
|
7
|
+
This gem recursively converts all camelBack or CamelCase keys in either a Hash structure, or an Array of Hashes, to snake_case.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -22,10 +22,20 @@ gem install plissken
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
On hashes:
|
26
|
+
|
25
27
|
```ruby
|
26
|
-
my_hash = {"firstKey" => 1, "fooBars" => [{"bazBaz" => "value"}, {"blahBlah" => "value"}]}
|
28
|
+
my_hash = { "firstKey" => 1, "fooBars" => [{ "bazBaz" => "value" }, { "blahBlah" => "value" }] }
|
27
29
|
snaked_hash = my_hash.to_snake_keys
|
28
|
-
# => {"first_key" => 1, "foo_bars" => [{"baz_baz" => "value"}, {"blah_blah" => "value"}]}
|
30
|
+
# => { "first_key" => 1, "foo_bars" => [{ "baz_baz" => "value" }, { "blah_blah" => "value" }] }
|
31
|
+
```
|
32
|
+
|
33
|
+
On arrays:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
my_array_of_hashes = [{ "firstKey" => 1, "fooBars" => [{ "bazBaz" => "value" }, { "blahBlah" => "value" }] }]
|
37
|
+
snaked_hash = my_array_of_hashes.to_snake_keys
|
38
|
+
# => [{"first_key" => 1, "foo_bars" => [{ "baz_baz" => "value" }, { "blah_blah" => "value" }] }]
|
29
39
|
```
|
30
40
|
|
31
41
|
Plissken works on either string keys or symbolized keys. It has no dependencies, as it has its own `underscore` method lifted out of ActiveSupport.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/plissken.rb
CHANGED
@@ -1,47 +1,5 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Hash.to_snake_keys
|
4
1
|
class Hash
|
5
|
-
# Recursively converts CamelCase and camelBack JSON-style hash keys to
|
6
|
-
# Rubyish snake_case, suitable for use during instantiation of Ruby
|
7
|
-
# model attributes.
|
8
|
-
#
|
9
|
-
def to_snake_keys(value = self)
|
10
|
-
case value
|
11
|
-
when Array
|
12
|
-
value.map { |v| to_snake_keys(v) }
|
13
|
-
when Hash
|
14
|
-
snake_hash(value)
|
15
|
-
else
|
16
|
-
value
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def snake_hash(value)
|
23
|
-
Hash[value.map { |k, v| [underscore_key(k), to_snake_keys(v)] }]
|
24
|
-
end
|
25
2
|
|
26
|
-
|
27
|
-
if k.is_a? Symbol
|
28
|
-
underscore(k.to_s).to_sym
|
29
|
-
elsif k.is_a? String
|
30
|
-
underscore(k)
|
31
|
-
else
|
32
|
-
k # Plissken can't snakify anything except strings and symbols
|
33
|
-
end
|
34
|
-
end
|
3
|
+
include Plissken::Methods
|
35
4
|
|
36
|
-
def underscore(string)
|
37
|
-
@__memoize_underscore ||= {}
|
38
|
-
return @__memoize_underscore[string] if @__memoize_underscore[string]
|
39
|
-
@__memoize_underscore[string] =
|
40
|
-
string.tr('::', '/')
|
41
|
-
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
42
|
-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
43
|
-
.tr('-', '_')
|
44
|
-
.downcase
|
45
|
-
@__memoize_underscore[string]
|
46
|
-
end
|
47
5
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plissken
|
4
|
+
module Methods
|
5
|
+
|
6
|
+
# Recursively converts CamelCase and camelBack JSON-style hash keys to
|
7
|
+
# Rubyish snake_case, suitable for use during instantiation of Ruby
|
8
|
+
# model attributes.
|
9
|
+
#
|
10
|
+
def to_snake_keys(value = self)
|
11
|
+
case value
|
12
|
+
when Array
|
13
|
+
value.map { |v| to_snake_keys(v) }
|
14
|
+
when Hash
|
15
|
+
snake_hash(value)
|
16
|
+
else
|
17
|
+
value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def snake_hash(value)
|
24
|
+
Hash[value.map { |k, v| [underscore_key(k), to_snake_keys(v)] }]
|
25
|
+
end
|
26
|
+
|
27
|
+
def underscore_key(k)
|
28
|
+
if k.is_a? Symbol
|
29
|
+
underscore(k.to_s).to_sym
|
30
|
+
elsif k.is_a? String
|
31
|
+
underscore(k)
|
32
|
+
else
|
33
|
+
k # Plissken can't snakify anything except strings and symbols
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def underscore(string)
|
38
|
+
@__memoize_underscore ||= {}
|
39
|
+
return @__memoize_underscore[string] if @__memoize_underscore[string]
|
40
|
+
@__memoize_underscore[string] =
|
41
|
+
string.tr('::', '/')
|
42
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
43
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
44
|
+
.tr('-', '_')
|
45
|
+
.downcase
|
46
|
+
@__memoize_underscore[string]
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plissken
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Hrycyszyn
|
@@ -70,7 +70,9 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- VERSION
|
72
72
|
- lib/plissken.rb
|
73
|
+
- lib/plissken/ext/array/to_snake_keys.rb
|
73
74
|
- lib/plissken/ext/hash/to_snake_keys.rb
|
75
|
+
- lib/plissken/methods.rb
|
74
76
|
- lib/plissken/version.rb
|
75
77
|
homepage: https://github.com/futurechimp/plissken
|
76
78
|
licenses:
|