camel_snake_keys 0.0.6 → 0.0.7
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/.rubocop.yml +96 -1173
- data/.ruby-version +1 -1
- data/.travis.yml +9 -15
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -2
- data/README.md +0 -3
- data/Rakefile +0 -1
- data/camel_snake.gemspec +13 -13
- data/lib/camel_snake_keys.rb +27 -25
- data/lib/version.rb +1 -1
- data/spec/lib/camel_snake_keys_spec.rb +33 -6
- metadata +14 -14
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.0.1
|
data/.travis.yml
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
before_install:
|
3
|
-
- gem install bundler
|
3
|
+
- gem install bundler -v 1.17.3
|
4
|
+
- gem install rspec
|
4
5
|
install: bundle install --jobs=1 --retry=1
|
5
6
|
script:
|
6
7
|
- bundle install
|
7
8
|
- bundle exec rspec
|
8
9
|
|
9
10
|
rvm:
|
10
|
-
- 2.3.
|
11
|
-
- 2.
|
12
|
-
- 2.
|
13
|
-
-
|
14
|
-
# - jruby-
|
15
|
-
- ruby-head
|
16
|
-
# - jruby-head
|
11
|
+
- 2.3.1
|
12
|
+
# - 2.4.0
|
13
|
+
# - 2.5.0
|
14
|
+
# - ruby-head
|
15
|
+
# - jruby-head
|
17
16
|
|
18
17
|
matrix:
|
19
18
|
allow_failures:
|
@@ -21,16 +20,11 @@ matrix:
|
|
21
20
|
- rvm: jruby-head
|
22
21
|
|
23
22
|
env:
|
24
|
-
matrix:
|
25
|
-
- RAILS=3.2.22
|
26
|
-
- RAILS=4.1.13
|
27
|
-
- RAILS=4.2.7.1
|
28
|
-
- RAILS=master
|
29
23
|
global:
|
30
|
-
|
24
|
+
- JRUBY_OPTS="-J-Xmx1024m --debug"
|
31
25
|
|
32
26
|
notifications:
|
33
|
-
email:
|
27
|
+
email:
|
34
28
|
recipients:
|
35
29
|
- buermann@gmail.com
|
36
30
|
on_success: change
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,18 +2,15 @@
|
|
2
2
|
|
3
3
|
[![Gem Version][GV img]][Gem Version]
|
4
4
|
[![Build Status][BS img]][Build Status]
|
5
|
-
[![Dependency Status][DS img]][Dependency Status]
|
6
5
|
[![Coverage Status][CS img]][Coverage Status]
|
7
6
|
|
8
7
|
[Gem Version]: https://rubygems.org/gems/camel_snake_keys
|
9
8
|
[Build Status]: https://travis-ci.org/buermann/camel_snake_keys
|
10
9
|
[travis pull requests]: https://travis-ci.org/buermann/camel_snake_keys/pull_requests
|
11
|
-
[Dependency Status]: https://gemnasium.com/buermann/camel_snake_keys
|
12
10
|
[Coverage Status]: https://coveralls.io/r/buermann/camel_snake_keys
|
13
11
|
|
14
12
|
[GV img]: https://badge.fury.io/rb/camel_snake_keys.png
|
15
13
|
[BS img]: https://travis-ci.org/buermann/camel_snake_keys.png
|
16
|
-
[DS img]: https://gemnasium.com/buermann/camel_snake_keys.png
|
17
14
|
[CS img]: https://coveralls.io/repos/buermann/camel_snake_keys/badge.png?branch=master
|
18
15
|
|
19
16
|
|
data/Rakefile
CHANGED
data/camel_snake.gemspec
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
2
2
|
|
3
3
|
# Maintain your gem's version:
|
4
|
-
require
|
4
|
+
require 'version'
|
5
5
|
|
6
6
|
# Describe your gem and declare its dependencies:
|
7
7
|
Gem::Specification.new do |s|
|
8
|
-
s.name =
|
8
|
+
s.name = 'camel_snake_keys'
|
9
9
|
s.version = CamelSnakeKeys::VERSION
|
10
|
-
s.authors = [
|
11
|
-
s.email = [
|
12
|
-
s.homepage =
|
13
|
-
s.summary =
|
14
|
-
s.description =
|
15
|
-
s.license =
|
10
|
+
s.authors = ['Josh Buermann']
|
11
|
+
s.email = ['buermann@gmail.com']
|
12
|
+
s.homepage = 'https://github.com/buermann/camel_snake_keys'
|
13
|
+
s.summary = 'Convert nested data structure hash keys between camel and snake case.'
|
14
|
+
s.description = ''
|
15
|
+
s.license = 'MIT'
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n").sort
|
18
18
|
s.test_files = `git ls-files -- spec/*`.split("\n")
|
19
19
|
|
20
|
-
s.required_ruby_version = '
|
21
|
-
s.add_dependency
|
20
|
+
s.required_ruby_version = '> 2.0'
|
21
|
+
s.add_dependency 'activesupport'
|
22
22
|
|
23
|
-
s.add_development_dependency
|
24
|
-
s.add_development_dependency
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'activesupport'
|
25
25
|
end
|
data/lib/camel_snake_keys.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'active_support
|
2
|
-
require 'active_support/core_ext
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
3
|
|
4
4
|
module CamelSnakeKeys
|
5
|
-
[Hash,Array].each do |klass|
|
5
|
+
[Hash, Array].each do |klass|
|
6
6
|
refine klass do
|
7
7
|
def with_camel_keys(indifference=false)
|
8
8
|
CamelSnakeKeys.camel_keys(self, indifference)
|
@@ -15,50 +15,52 @@ module CamelSnakeKeys
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class << self
|
18
|
-
def if_underscore(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def if_underscore(obj)
|
19
|
+
case obj
|
20
|
+
when Symbol
|
21
|
+
obj.to_s.underscore.to_sym
|
22
|
+
when String
|
23
|
+
obj.underscore
|
23
24
|
else
|
24
|
-
|
25
|
+
obj
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
def if_camelize(
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
def if_camelize(obj)
|
30
|
+
case obj
|
31
|
+
when Symbol
|
32
|
+
obj.to_s.camelize(:lower).to_sym
|
33
|
+
when String
|
34
|
+
obj.camelize(:lower)
|
33
35
|
else
|
34
|
-
|
36
|
+
obj
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
38
40
|
def snake_keys(data, indifference=false)
|
39
|
-
|
41
|
+
case data
|
42
|
+
when Array
|
40
43
|
data.map { |v| snake_keys(v, indifference) }
|
41
|
-
|
42
|
-
hash =
|
44
|
+
when Hash
|
45
|
+
hash = data.sort_by {|k, _v| k =~ /_/ ? 0 : 1 }.map {|k, v| [if_underscore(k), snake_keys(v, indifference)] }.to_h
|
43
46
|
hash = hash.with_indifferent_access if indifference
|
44
|
-
data.
|
47
|
+
data.instance_of?(Hash) ? hash : data.class.new(hash)
|
45
48
|
else
|
46
49
|
data
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
53
|
def camel_keys(data, indifference=false)
|
51
|
-
|
54
|
+
case data
|
55
|
+
when Array
|
52
56
|
data.map { |v| camel_keys(v, indifference) }
|
53
|
-
|
54
|
-
hash =
|
57
|
+
when Hash
|
58
|
+
hash = data.sort_by {|k, _v| k =~ /_/ ? 1 : 0 }.map {|k, v| [if_camelize(k), camel_keys(v, indifference)] }.to_h
|
55
59
|
hash = hash.with_indifferent_access if indifference
|
56
|
-
data.
|
60
|
+
data.instance_of?(Hash) ? hash : data.class.new(hash)
|
57
61
|
else
|
58
62
|
data
|
59
63
|
end
|
60
64
|
end
|
61
65
|
end
|
62
66
|
end
|
63
|
-
|
64
|
-
|
data/lib/version.rb
CHANGED
@@ -35,14 +35,14 @@ RSpec.describe Enumerable do
|
|
35
35
|
hash.should eq camelized
|
36
36
|
end
|
37
37
|
|
38
|
-
it "should preserve symbol keys" do
|
38
|
+
it "should preserve symbol keys" do
|
39
39
|
camelized.with_snake_keys[:foo_bar].should_not be_nil
|
40
40
|
camelized.with_snake_keys['foo_bar'].should be_nil
|
41
41
|
snaked.with_camel_keys[:fooBar].should be_present
|
42
42
|
snaked.with_camel_keys['fooBar'].should be_nil
|
43
43
|
end
|
44
44
|
|
45
|
-
it "should preserve string keys" do
|
45
|
+
it "should preserve string keys" do
|
46
46
|
camelized.with_snake_keys['dark_matter'].should be_present
|
47
47
|
camelized.with_snake_keys[:dark_matter].should be_nil
|
48
48
|
snaked.with_camel_keys['darkMatter'].should be_present
|
@@ -65,7 +65,7 @@ RSpec.describe Enumerable do
|
|
65
65
|
|
66
66
|
end
|
67
67
|
|
68
|
-
context "hashes with indifferent access" do
|
68
|
+
context "hashes with indifferent access" do
|
69
69
|
let(:snaked) { {1.2=>1, 1=>1.2, nil=>2, :foo_bar=>1, "dark_matter"=>[{:dark_energy=>"aBc", "baz_qux"=>"Frob."}]}.with_indifferent_access }
|
70
70
|
let(:camelized) { { 1.2=>1, 1=>1.2, nil=>2, :fooBar=>1, "darkMatter"=>[{:darkEnergy=>"aBc", "bazQux"=>"Frob."}]}.with_indifferent_access }
|
71
71
|
|
@@ -95,7 +95,7 @@ RSpec.describe Enumerable do
|
|
95
95
|
|
96
96
|
end
|
97
97
|
|
98
|
-
context "mashes" do
|
98
|
+
context "mashes" do
|
99
99
|
let(:snaked) { Hashie::Mash.new({1.2=>1, 1=>1.2, nil=>2, :foo_bar=>1, "dark_matter"=>[{:dark_energy=>"aBc", "baz_qux"=>"Frob."}]}) }
|
100
100
|
let(:camelized) { Hashie::Mash.new({ 1.2=>1, 1=>1.2, nil=>2, :fooBar=>1, "darkMatter"=>[{:darkEnergy=>"aBc", "bazQux"=>"Frob."}]}) }
|
101
101
|
|
@@ -112,7 +112,7 @@ RSpec.describe Enumerable do
|
|
112
112
|
hash.should eq camelized
|
113
113
|
hash["foo_bar"].should eq hash[:foo_bar]
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
it "should snake case keys of hashes with redundant indifference" do
|
117
117
|
hash = camelized.with_snake_keys(true)
|
118
118
|
hash.class.should eq Hashie::Mash
|
@@ -126,7 +126,7 @@ RSpec.describe Enumerable do
|
|
126
126
|
hash.should eq camelized
|
127
127
|
hash["foo_bar"].should eq hash[:foo_bar]
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
end
|
131
131
|
|
132
132
|
context "hash merge conflicts should be resolved predictably" do
|
@@ -148,4 +148,31 @@ RSpec.describe Enumerable do
|
|
148
148
|
hash.with_camel_keys.should eq result
|
149
149
|
end
|
150
150
|
end
|
151
|
+
|
152
|
+
context "it should pass indifference down deeply nested structures" do
|
153
|
+
it "camelizing an array of hashes" do
|
154
|
+
camelized = [ a: { b: [{c: :d}] } ].with_camel_keys(true)
|
155
|
+
camelized.first[:a].is_a?(HashWithIndifferentAccess).should be_truthy
|
156
|
+
camelized.first[:a][:b].first.is_a?(HashWithIndifferentAccess).should be_truthy
|
157
|
+
end
|
158
|
+
|
159
|
+
it "cazemlizing a hashes of arrays" do
|
160
|
+
camelized = { a: [{b: {c: :d}}]}.with_camel_keys(true)
|
161
|
+
camelized.is_a?(HashWithIndifferentAccess).should be_truthy
|
162
|
+
camelized[:a].first[:b].is_a?(HashWithIndifferentAccess).should be_truthy
|
163
|
+
end
|
164
|
+
|
165
|
+
it "snaking an array of hashes" do
|
166
|
+
snaked = [ a: { b: [{c: :d}] } ].with_snake_keys(true)
|
167
|
+
snaked.first[:a].is_a?(HashWithIndifferentAccess).should be_truthy
|
168
|
+
snaked.first[:a][:b].first.is_a?(HashWithIndifferentAccess).should be_truthy
|
169
|
+
end
|
170
|
+
|
171
|
+
it "snaking a hashes of arrays" do
|
172
|
+
snaked = { a: [{b: {c: :d}}]}.with_snake_keys(true)
|
173
|
+
snaked.is_a?(HashWithIndifferentAccess).should be_truthy
|
174
|
+
snaked[:a].first[:b].is_a?(HashWithIndifferentAccess).should be_truthy
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
151
178
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: camel_snake_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Buermann
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -25,33 +25,33 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: rspec
|
28
|
+
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: activesupport
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
description: ''
|
56
56
|
email:
|
57
57
|
- buermann@gmail.com
|
@@ -77,13 +77,13 @@ homepage: https://github.com/buermann/camel_snake_keys
|
|
77
77
|
licenses:
|
78
78
|
- MIT
|
79
79
|
metadata: {}
|
80
|
-
post_install_message:
|
80
|
+
post_install_message:
|
81
81
|
rdoc_options: []
|
82
82
|
require_paths:
|
83
83
|
- lib
|
84
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">"
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '2.0'
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -92,8 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
rubygems_version: 3.
|
96
|
-
signing_key:
|
95
|
+
rubygems_version: 3.2.15
|
96
|
+
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Convert nested data structure hash keys between camel and snake case.
|
99
99
|
test_files:
|