hash_key_transformer 0.1.2 → 0.1.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/hash_key_transformer.rb +22 -17
- data/lib/hash_key_transformer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 242ec9fefddf58d3434723163ce8fe1badc4eaf7
|
4
|
+
data.tar.gz: 399605e6c1f28e27180dde264ea13c418b3211f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09dbf6693a51215f5b1405cd89d3b9a8aaeb3b83dcaa74cc7df44e2ebd8eceac5ba8ad0f6c8f472350b58539f16ce23c3a69526f3f9c1529e445fe1208df0e6f'
|
7
|
+
data.tar.gz: 13b791280d9cb8ec1d0b2ed9525f50460676a391b69d3b1a66d8d6244d431f401a8bb0b8ed9bb71b11190157f8393bfe5f3e8b72c7e05a7db0ff4cbd02640b24
|
data/Gemfile.lock
CHANGED
data/lib/hash_key_transformer.rb
CHANGED
@@ -3,55 +3,60 @@ require 'hash_key_transformer/version'
|
|
3
3
|
class HashKeyTransformer
|
4
4
|
class << self
|
5
5
|
|
6
|
-
def transform_camel_to_underscore(subject)
|
7
|
-
deep_transform_hash_keys(subject, :camel_to_underscore)
|
6
|
+
def transform_camel_to_underscore(subject, options={})
|
7
|
+
deep_transform_hash_keys(subject, :camel_to_underscore, options)
|
8
8
|
end
|
9
9
|
|
10
|
-
def transform_underscore_to_camel(subject)
|
11
|
-
deep_transform_hash_keys(subject, :underscore_to_camel)
|
10
|
+
def transform_underscore_to_camel(subject, options={})
|
11
|
+
deep_transform_hash_keys(subject, :underscore_to_camel, options)
|
12
12
|
end
|
13
13
|
|
14
|
-
def transform_dash_to_underscore(subject)
|
15
|
-
deep_transform_hash_keys(subject, :dash_to_underscore)
|
14
|
+
def transform_dash_to_underscore(subject, options={})
|
15
|
+
deep_transform_hash_keys(subject, :dash_to_underscore, options)
|
16
16
|
end
|
17
17
|
|
18
|
-
def transform_underscore_to_dash(subject)
|
19
|
-
deep_transform_hash_keys(subject, :underscore_to_dash)
|
18
|
+
def transform_underscore_to_dash(subject, options={})
|
19
|
+
deep_transform_hash_keys(subject, :underscore_to_dash, options)
|
20
20
|
end
|
21
21
|
|
22
22
|
# 'keyName' -> 'key_name'
|
23
|
-
def camel_to_underscore(key)
|
23
|
+
def camel_to_underscore(key, options = {})
|
24
24
|
key.to_s.gsub(/([A-Z])/) { "_#{$1}" }.downcase
|
25
25
|
end
|
26
26
|
|
27
27
|
# 'key_name' -> 'keyName'
|
28
|
-
def underscore_to_camel(key)
|
29
|
-
|
28
|
+
def underscore_to_camel(key, options = {})
|
29
|
+
if !!options[:keep_lead_underscore]
|
30
|
+
key.to_s.gsub(/(?!^)_([a-z0-9])/) { $1.upcase }
|
31
|
+
else
|
32
|
+
first_word, *rest_words = key.to_s.split('_').reject(&:empty?).map(&:downcase)
|
33
|
+
([first_word] + rest_words.map(&:capitalize)).join
|
34
|
+
end
|
30
35
|
end
|
31
36
|
|
32
37
|
# 'key-name' -> 'key_name'
|
33
|
-
def dash_to_underscore(key)
|
38
|
+
def dash_to_underscore(key, options = {})
|
34
39
|
key.to_s.downcase.gsub(/-([a-z0-9])/) { "_#{$1}" }
|
35
40
|
end
|
36
41
|
|
37
42
|
# 'key_name' -> 'key-name'
|
38
|
-
def underscore_to_dash(key)
|
43
|
+
def underscore_to_dash(key, options = {})
|
39
44
|
key.to_s.downcase.gsub(/_([a-z0-9])/) { "-#{$1}" }
|
40
45
|
end
|
41
46
|
|
42
47
|
private
|
43
48
|
|
44
49
|
# Recursively walk the JSON-like data structure (hash, array) and transform the hash keys using the given strategy
|
45
|
-
def deep_transform_hash_keys(subject, output_key_strategy)
|
50
|
+
def deep_transform_hash_keys(subject, output_key_strategy, options = {})
|
46
51
|
if subject.is_a?(Hash)
|
47
52
|
subject.inject({}) do |memo, (key, value)|
|
48
|
-
new_key = send(output_key_strategy, key).to_sym
|
49
|
-
memo[new_key] = deep_transform_hash_keys(value, output_key_strategy)
|
53
|
+
new_key = send(output_key_strategy, key, options).to_sym
|
54
|
+
memo[new_key] = deep_transform_hash_keys(value, output_key_strategy, options)
|
50
55
|
memo
|
51
56
|
end
|
52
57
|
elsif subject.is_a?(Array)
|
53
58
|
subject.inject([]) do |memo, item|
|
54
|
-
memo << deep_transform_hash_keys(item, output_key_strategy)
|
59
|
+
memo << deep_transform_hash_keys(item, output_key_strategy, options)
|
55
60
|
memo
|
56
61
|
end
|
57
62
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_key_transformer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Rood
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|