camel_snake_keys 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +36 -0
- data/README.md +22 -3
- data/lib/camel_snake_keys.rb +2 -3
- data/lib/version.rb +1 -1
- data/spec/lib/camel_snake_keys_spec.rb +51 -16
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60878589862b05c4c1b707c7840614c473b9ea52
|
4
|
+
data.tar.gz: fbf67796982570c635fad519603702847e0163ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3aca7eee031b8ad0592bbeb8e3dae912d797bef57a20605c31d5ee40b5231429194329f65ae91688d68ee0b62b692e72eddfad958a759abb8ff1a66bdd90c08d
|
7
|
+
data.tar.gz: d43e5649843a5a9a14b98185d9ebc8b6a0ff5b255236f3f5cc87728aace3e220df48214a03f9914739c670c4e6063d4f3272bdddd08447e35700da159f148cc9
|
data/.travis.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
language: ruby
|
2
|
+
before_install:
|
3
|
+
- gem install bundler
|
4
|
+
install: bundle install --jobs=1 --retry=1
|
5
|
+
script:
|
6
|
+
- bundle install
|
7
|
+
- bundle exec rspec
|
8
|
+
|
9
|
+
rvm:
|
10
|
+
- 2.2
|
11
|
+
- 2.1
|
12
|
+
- 2.0.0
|
13
|
+
- jruby-9.0.4.0
|
14
|
+
- ruby-head
|
15
|
+
- jruby-head
|
16
|
+
|
17
|
+
matrix:
|
18
|
+
allow_failures:
|
19
|
+
- rvm: ruby-head
|
20
|
+
- rvm: jruby-head
|
21
|
+
|
22
|
+
env:
|
23
|
+
matrix:
|
24
|
+
- RAILS=3.2.22
|
25
|
+
- RAILS=4.1.13
|
26
|
+
- RAILS=4.2.5.1
|
27
|
+
- RAILS=master
|
28
|
+
global:
|
29
|
+
- JRUBY_OPTS="-J-Xmx1024m --debug"
|
30
|
+
|
31
|
+
notifications:
|
32
|
+
email:
|
33
|
+
recipients:
|
34
|
+
- buermann@gmail.com
|
35
|
+
on_success: change
|
36
|
+
on_failure: always
|
data/README.md
CHANGED
@@ -1,20 +1,39 @@
|
|
1
1
|
# camel_snake_keys
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version][GV img]][Gem Version]
|
4
|
+
[![Build Status][BS img]][Build Status]
|
5
|
+
[![Dependency Status][DS img]][Dependency Status]
|
6
|
+
[![Coverage Status][CS img]][Coverage Status]
|
7
|
+
|
8
|
+
[Gem Version]: https://rubygems.org/gems/camel_snake_keys
|
9
|
+
[Build Status]: https://travis-ci.org/buermann/camel_snake_keys
|
10
|
+
[travis pull requests]: https://travis-ci.org/buermann/camel_snake_keys/pull_requests
|
11
|
+
[Dependency Status]: https://gemnasium.com/buermann/camel_snake_keys
|
12
|
+
[Coverage Status]: https://coveralls.io/r/buermann/camel_snake_keys
|
13
|
+
|
14
|
+
[GV img]: https://badge.fury.io/rb/camel_snake_keys.png
|
15
|
+
[BS img]: https://travis-ci.org/buermann/camel_snake_keys.png
|
16
|
+
[DS img]: https://gemnasium.com/buermann/camel_snake_keys.png
|
17
|
+
[CS img]: https://coveralls.io/repos/buermann/camel_snake_keys/badge.png?branch=master
|
18
|
+
|
19
|
+
|
20
|
+
Add recursive with_snake_keys and with_camel_keys to Enumerable, preserve strings and symbols in hashes, and treat hash descendents like ActiveSupport::HashWithIndifferentAccess and Hashie::Mash agnostically.
|
4
21
|
|
5
22
|
## Documentation
|
6
23
|
|
7
|
-
Add gem 'camel_snake_keys' to your
|
24
|
+
Add `gem 'camel_snake_keys'` to your Gemfile or gem install camel_snake_keys.
|
8
25
|
|
9
26
|
Enumerables will be monkey patched the following methods:
|
10
27
|
|
28
|
+
```
|
11
29
|
with_snake_keys(with_indifferent=false)
|
12
30
|
with_camel_keys(with_indifferent=false)
|
31
|
+
```
|
13
32
|
|
14
33
|
If with_indifference is set to a true value hashes will be returned as ActiveSupport's HashWithIndifferentAccess.
|
15
34
|
|
16
35
|
```
|
17
|
-
require '
|
36
|
+
require 'camel_snake_keys'
|
18
37
|
|
19
38
|
{fooBar: "Frob"}.with_snake_keys
|
20
39
|
=> {:foo_bar=>"Frob"}
|
data/lib/camel_snake_keys.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'active_support/core_ext/hash'
|
2
2
|
require 'active_support/core_ext/string/inflections'
|
3
|
-
require 'hashie/mash'
|
4
3
|
|
5
4
|
module CamelSnakeKeys
|
6
5
|
class << self
|
@@ -28,7 +27,7 @@ module CamelSnakeKeys
|
|
28
27
|
if data.kind_of? Array
|
29
28
|
data.map { |v| snake_keys(v) }
|
30
29
|
elsif data.kind_of? Hash
|
31
|
-
hash = Hash[data.map {|k, v| [if_underscore(k), snake_keys(v)] }]
|
30
|
+
hash = Hash[data.sort_by {|k,v| k =~ /_/ ? 0 : 1 }.map {|k, v| [if_underscore(k), snake_keys(v)] }]
|
32
31
|
hash = hash.with_indifferent_access if indifference
|
33
32
|
data.class == Hash ? hash : data.class.new(hash)
|
34
33
|
else
|
@@ -40,7 +39,7 @@ module CamelSnakeKeys
|
|
40
39
|
if data.kind_of? Array
|
41
40
|
data.map { |v| camel_keys(v) }
|
42
41
|
elsif data.kind_of? Hash
|
43
|
-
hash = Hash[data.map {|k, v| [if_camelize(k), camel_keys(v)] }]
|
42
|
+
hash = Hash[data.sort_by {|k,v| k =~ /_/ ? 1 : 0 }.map {|k, v| [if_camelize(k), camel_keys(v)] }]
|
44
43
|
hash = hash.with_indifferent_access if indifference
|
45
44
|
data.class == Hash ? hash : data.class.new(hash)
|
46
45
|
else
|
data/lib/version.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'hashie/mash'
|
2
3
|
|
3
4
|
RSpec.describe Enumerable do
|
4
5
|
|
@@ -13,6 +14,7 @@ RSpec.describe Enumerable do
|
|
13
14
|
it "should camel case keys of hashes" do
|
14
15
|
snaked.with_camel_keys.should eq camelized
|
15
16
|
end
|
17
|
+
|
16
18
|
end
|
17
19
|
|
18
20
|
context "hashes" do
|
@@ -21,26 +23,40 @@ RSpec.describe Enumerable do
|
|
21
23
|
|
22
24
|
it "should snake case keys of hashes" do
|
23
25
|
hash = camelized.with_snake_keys
|
24
|
-
hash.class.should
|
26
|
+
hash.class.should eq Hash
|
25
27
|
hash.should eq snaked
|
26
28
|
end
|
27
29
|
|
28
30
|
it "should camel case keys of hashes" do
|
29
31
|
hash = snaked.with_camel_keys
|
30
|
-
hash.class.should
|
32
|
+
hash.class.should eq Hash
|
31
33
|
hash.should eq camelized
|
32
34
|
end
|
33
35
|
|
36
|
+
it "should preserve symbol keys" do
|
37
|
+
camelized.with_snake_keys[:foo_bar].should_not be_nil
|
38
|
+
camelized.with_snake_keys['foo_bar'].should be_nil
|
39
|
+
snaked.with_camel_keys[:fooBar].should be_present
|
40
|
+
snaked.with_camel_keys['fooBar'].should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should preserve string keys" do
|
44
|
+
camelized.with_snake_keys['dark_matter'].should be_present
|
45
|
+
camelized.with_snake_keys[:dark_matter].should be_nil
|
46
|
+
snaked.with_camel_keys['darkMatter'].should be_present
|
47
|
+
snaked.with_camel_keys[:darkMatter].should be_nil
|
48
|
+
end
|
49
|
+
|
34
50
|
it "should snake case keys of hashes with indifference" do
|
35
51
|
hash = camelized.with_snake_keys(true)
|
36
|
-
hash.class.should
|
52
|
+
hash.class.should eq HashWithIndifferentAccess
|
37
53
|
hash.should eq snaked.with_indifferent_access
|
38
54
|
hash[:foo_bar].should eq hash["foo_bar"]
|
39
55
|
end
|
40
56
|
|
41
57
|
it "should camel case keys of hashes with indifference" do
|
42
58
|
hash = snaked.with_camel_keys(true)
|
43
|
-
hash.class.should
|
59
|
+
hash.class.should eq HashWithIndifferentAccess
|
44
60
|
hash.should eq camelized.with_indifferent_access
|
45
61
|
hash["fooBar"].should eq hash[:fooBar]
|
46
62
|
end
|
@@ -53,25 +69,25 @@ RSpec.describe Enumerable do
|
|
53
69
|
|
54
70
|
it "should snake case keys of hashes" do
|
55
71
|
hash = camelized.with_snake_keys
|
56
|
-
hash.class.should
|
72
|
+
hash.class.should eq HashWithIndifferentAccess
|
57
73
|
hash.should eq snaked
|
58
74
|
end
|
59
75
|
|
60
76
|
it "should camel case keys of hashes" do
|
61
77
|
hash = snaked.with_camel_keys
|
62
|
-
hash.class.should
|
78
|
+
hash.class.should eq HashWithIndifferentAccess
|
63
79
|
hash.should eq camelized
|
64
80
|
end
|
65
81
|
|
66
82
|
it "should snake case keys of hashes with indifference" do
|
67
83
|
hash = camelized.with_snake_keys(true)
|
68
|
-
hash.class.should
|
84
|
+
hash.class.should eq HashWithIndifferentAccess
|
69
85
|
hash.should eq snaked
|
70
86
|
end
|
71
87
|
|
72
88
|
it "should camel case keys of hashes with indifference" do
|
73
89
|
hash = snaked.with_camel_keys(true)
|
74
|
-
hash.class.should
|
90
|
+
hash.class.should eq HashWithIndifferentAccess
|
75
91
|
hash.should eq camelized
|
76
92
|
end
|
77
93
|
|
@@ -83,32 +99,51 @@ RSpec.describe Enumerable do
|
|
83
99
|
|
84
100
|
it "should snake case keys of hashes" do
|
85
101
|
hash = camelized.with_snake_keys
|
86
|
-
hash.class.should
|
102
|
+
hash.class.should eq Hashie::Mash
|
87
103
|
hash.should eq snaked
|
88
|
-
hash["fooBar"].should
|
104
|
+
hash["fooBar"].should eq hash[:fooBar]
|
89
105
|
end
|
90
106
|
|
91
107
|
it "should camel case keys of hashes" do
|
92
108
|
hash = snaked.with_camel_keys
|
93
|
-
hash.class.should
|
109
|
+
hash.class.should eq Hashie::Mash
|
94
110
|
hash.should eq camelized
|
95
|
-
hash["foo_bar"].should
|
111
|
+
hash["foo_bar"].should eq hash[:foo_bar]
|
96
112
|
end
|
97
113
|
|
98
114
|
it "should snake case keys of hashes with redundant indifference" do
|
99
115
|
hash = camelized.with_snake_keys(true)
|
100
|
-
hash.class.should
|
116
|
+
hash.class.should eq Hashie::Mash
|
101
117
|
hash.should eq snaked
|
102
|
-
hash["foo_bar"].should
|
118
|
+
hash["foo_bar"].should eq hash[:foo_bar]
|
103
119
|
end
|
104
120
|
|
105
121
|
it "should camel case keys of hashes with redundant indifference" do
|
106
122
|
hash = snaked.with_camel_keys(true)
|
107
|
-
hash.class.should
|
123
|
+
hash.class.should eq Hashie::Mash
|
108
124
|
hash.should eq camelized
|
109
|
-
hash["foo_bar"].should
|
125
|
+
hash["foo_bar"].should eq hash[:foo_bar]
|
110
126
|
end
|
111
127
|
|
112
128
|
end
|
113
129
|
|
130
|
+
context "hash merge conflicts should be resolved predictably" do
|
131
|
+
it "should give camel case key values priority when snake casing" do
|
132
|
+
hash = { foo_bar: 1, fooBar: 2 }
|
133
|
+
result = { foo_bar: 2 }
|
134
|
+
hash.with_snake_keys.should eq result
|
135
|
+
|
136
|
+
hash = { fooBar: 2, foo_bar: 1 }
|
137
|
+
hash.with_snake_keys.should eq result
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should give snake case key values priority when camel casing" do
|
141
|
+
hash = { foo_bar: 1, fooBar: 2 }
|
142
|
+
result = { fooBar: 1 }
|
143
|
+
hash.with_camel_keys.should eq result
|
144
|
+
|
145
|
+
hash = { fooBar: 2, foo_bar: 1 }
|
146
|
+
hash.with_camel_keys.should eq result
|
147
|
+
end
|
148
|
+
end
|
114
149
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Buermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- ".coveralls.yml"
|
49
49
|
- ".gitignore"
|
50
50
|
- ".rubocop.yml"
|
51
|
+
- ".travis.yml"
|
51
52
|
- CHANGELOG.md
|
52
53
|
- Gemfile
|
53
54
|
- README.md
|
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
78
|
version: '0'
|
78
79
|
requirements: []
|
79
80
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.
|
81
|
+
rubygems_version: 2.5.1
|
81
82
|
signing_key:
|
82
83
|
specification_version: 4
|
83
84
|
summary: Convert nested data structure hash keys between camel and snake case.
|