snake_case_hash 1.0.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 +7 -0
- data/.document +5 -0
- data/.gemtest +0 -0
- data/.gitignore +25 -0
- data/.rspec +3 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +66 -0
- data/Rakefile +19 -0
- data/lib/hashie/sc_hash.rb +45 -0
- data/lib/sc_hash.rb +1 -0
- data/lib/snake_case_hash/version.rb +3 -0
- data/snake_case_hash.gemspec +26 -0
- data/spec/sc_hash_spec.rb +115 -0
- data/spec/spec_helper.rb +4 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd0dfb5e249faca5bd0391a455885b47753a1318
|
4
|
+
data.tar.gz: 76850110b55620075fbca2fd32f840292d58981b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bae620a51aa38668180b089af9cd088a27638baf84d0cb33ccb9c86f38d8d71e4efbf9e8492b3c56e56bde9254ee76ba2d18e6e5c64f658c982254d368d36dd9
|
7
|
+
data.tar.gz: 89db25b9562b990aa21aaf12f6d824c8814d817b91e660c6659035f9cd007586a17b7ebe5ea60ead176dbe275ab0825010d6ea3876df0f51883a0bcaf5da9e7f
|
data/.document
ADDED
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tom Cocca
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
= Snake Case Hash (was: Rash)
|
2
|
+
|
3
|
+
== This version of rash has been renamed snake_case_hash gem (with class Hashie::SCHash) to avoid namespace conflicts in gemspec.
|
4
|
+
|
5
|
+
|
6
|
+
This was needed because Hashie version 3 introduced a new class Hashie::Rash that interfered with the Hashie::Rash class that was in the Rash gem, and the maintainer of the Rash gem hasn't been updated. I guess this is a fork but just to avoid problems around loading gems in .gemspec files (which unlike bundler doesn't allow you to specify git sources.)
|
7
|
+
|
8
|
+
Snake Cash Hash is an extension to Hashie ( http://github.com/intridea/hashie ) that was originally named Rash.
|
9
|
+
|
10
|
+
SCHash subclasses Hashie::Mash to convert all keys in the hash to underscore
|
11
|
+
|
12
|
+
The purpose of this is when working w/ Java (or any other apis) that return hashes (including nested) that have camelCased keys
|
13
|
+
|
14
|
+
You will now be able to access those keys through underscored key names (camelCase still available)
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
|
18
|
+
@rash = Hashie::SCHash.new({
|
19
|
+
"varOne" => 1,
|
20
|
+
"two" => 2,
|
21
|
+
:three => 3,
|
22
|
+
:varFour => 4,
|
23
|
+
"fiveHumpHumps" => 5,
|
24
|
+
:nested => {
|
25
|
+
"NestedOne" => "One",
|
26
|
+
:two => "two",
|
27
|
+
"nested_three" => "three"
|
28
|
+
},
|
29
|
+
"nestedTwo" => {
|
30
|
+
"nested_two" => 22,
|
31
|
+
:nestedThree => 23
|
32
|
+
}
|
33
|
+
})
|
34
|
+
|
35
|
+
@rash.var_one # => 1
|
36
|
+
@rash.two # => 2
|
37
|
+
@rash.three # => 3
|
38
|
+
@rash.var_four # => 4
|
39
|
+
@rash.five_hump_humps # => 5
|
40
|
+
@rash.nested.nested_one # => "One"
|
41
|
+
@rash.nested.two # => "two"
|
42
|
+
@rash.nested.nested_three # => "three"
|
43
|
+
@rash.nested_two.nested_two # => 22
|
44
|
+
@rash.nested_two.nested_three # => 23
|
45
|
+
|
46
|
+
== Note on Patches/Pull Requests
|
47
|
+
|
48
|
+
* Fork the project.
|
49
|
+
* Make your feature addition or bug fix.
|
50
|
+
* Add tests for it. This is important so I don't break it in a
|
51
|
+
future version unintentionally.
|
52
|
+
* Commit, do not mess with rakefile, version, or history.
|
53
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
54
|
+
* Send me a pull request. Bonus points for topic branches.
|
55
|
+
|
56
|
+
== Copyright
|
57
|
+
|
58
|
+
Copyright (c) 2010 Tom Cocca. See LICENSE for details.
|
59
|
+
|
60
|
+
=== Acknowledgements
|
61
|
+
|
62
|
+
* Zac Colon (https://github.com/wzcolon) for initially moving Rash to a new namespace and updating dependencies.
|
63
|
+
* Trocca (https://github.com/tcocca) for Rash
|
64
|
+
* Intridea (https://github.com/intridea) for Hashie
|
65
|
+
* Mislav Marohnić (https://github.com/mislav) for contributions to Rash
|
66
|
+
* Steve Agalloco (https://github.com/spagalloco) for updating Rash to use bundler, rspec 2.5, hashie 1.0 and fixing some load dependencies
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :test => :spec
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
require 'rdoc/task'
|
11
|
+
require File.expand_path('../lib/snake_case_hash/version', __FILE__)
|
12
|
+
RDoc::Task.new do |rdoc|
|
13
|
+
version = SnakeCaseHash::VERSION
|
14
|
+
|
15
|
+
rdoc.rdoc_dir = 'rdoc'
|
16
|
+
rdoc.title = "snake_case_hash #{version}"
|
17
|
+
rdoc.rdoc_files.include('README*')
|
18
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
19
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class SCHash
|
2
|
+
end
|
3
|
+
|
4
|
+
require 'hashie/hash'
|
5
|
+
require 'hashie/mash'
|
6
|
+
|
7
|
+
module Hashie
|
8
|
+
class SCHash < Hashie::Mash
|
9
|
+
protected
|
10
|
+
|
11
|
+
def convert_key(key) #:nodoc:
|
12
|
+
underscore_string(key.to_s)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Unlike its parent Mash, a Rash will convert other Hashie::Hash values to a Rash when assigning
|
16
|
+
# instead of respecting the existing subclass
|
17
|
+
def convert_value(val, duping=false) #:nodoc:
|
18
|
+
case val
|
19
|
+
when self.class
|
20
|
+
val.dup
|
21
|
+
when ::Hash
|
22
|
+
val = val.dup if duping
|
23
|
+
self.class.new(val)
|
24
|
+
when Array
|
25
|
+
val.collect{ |e| convert_value(e) }
|
26
|
+
else
|
27
|
+
val
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# converts a camel_cased string to a underscore string
|
32
|
+
# subs spaces with underscores, strips whitespace
|
33
|
+
# Same way ActiveSupport does string.underscore
|
34
|
+
def underscore_string(str)
|
35
|
+
str.to_s.strip.
|
36
|
+
gsub(' ', '_').
|
37
|
+
gsub(/::/, '/').
|
38
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
39
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
40
|
+
tr("-", "_").
|
41
|
+
squeeze("_").
|
42
|
+
downcase
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/sc_hash.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'hashie/sc_hash'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "snake_case_hash/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{snake_case_hash}
|
7
|
+
s.authors = ["tcocca, wzcolon, corprew"]
|
8
|
+
s.description = %q{simple extension to Hashie::Mash for rubyified keys, all keys are converted to underscore to eliminate horrible camelCasing}
|
9
|
+
s.email = %q{}
|
10
|
+
s.homepage = %q{http://github.com/corprew/snake_case_hash}
|
11
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
12
|
+
s.summary = %q{simple extension to Hashie::Mash for rubyified keys}
|
13
|
+
|
14
|
+
s.version = SnakeCaseHash::VERSION
|
15
|
+
|
16
|
+
s.add_dependency 'hashie', '~> 3.0'
|
17
|
+
s.add_development_dependency 'rake', '~> 0.9'
|
18
|
+
s.add_development_dependency 'rdoc', '~> 3.9'
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.5'
|
20
|
+
s.add_development_dependency 'pry'
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hashie::SCHash do
|
4
|
+
subject {
|
5
|
+
Hashie::SCHash.new({
|
6
|
+
"varOne" => 1,
|
7
|
+
"two" => 2,
|
8
|
+
:three => 3,
|
9
|
+
:varFour => 4,
|
10
|
+
"fiveHumpHumps" => 5,
|
11
|
+
:nested => {
|
12
|
+
"NestedOne" => "One",
|
13
|
+
:two => "two",
|
14
|
+
"nested_three" => "three"
|
15
|
+
},
|
16
|
+
"nestedTwo" => {
|
17
|
+
"nested_two" => 22,
|
18
|
+
:nestedThree => 23
|
19
|
+
},
|
20
|
+
"spaced Key" => "When would this happen?",
|
21
|
+
"trailing spaces " => "better safe than sorry",
|
22
|
+
"extra spaces" => "hopefully this never happens"
|
23
|
+
})
|
24
|
+
}
|
25
|
+
|
26
|
+
it { should be_a(Hashie::Hash) }
|
27
|
+
|
28
|
+
it "should create a new SCHash where all the keys are underscored instead of camelcased" do
|
29
|
+
subject.var_one.should == 1
|
30
|
+
subject.two.should == 2
|
31
|
+
subject.three.should == 3
|
32
|
+
subject.var_four.should == 4
|
33
|
+
subject.five_hump_humps.should == 5
|
34
|
+
subject.nested.should be_a(Hashie::SCHash)
|
35
|
+
subject.nested.nested_one.should == "One"
|
36
|
+
subject.nested.two.should == "two"
|
37
|
+
subject.nested.nested_three.should == "three"
|
38
|
+
subject.nested_two.should be_a(Hashie::SCHash)
|
39
|
+
subject.nested_two.nested_two.should == 22
|
40
|
+
subject.nested_two.nested_three.should == 23
|
41
|
+
subject.spaced_key.should == "When would this happen?"
|
42
|
+
subject.trailing_spaces.should == "better safe than sorry"
|
43
|
+
subject.extra_spaces.should == "hopefully this never happens"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should allow camelCased accessors" do
|
47
|
+
subject.varOne.should == 1
|
48
|
+
subject.varOne = "once"
|
49
|
+
subject.varOne.should == "once"
|
50
|
+
subject.var_one.should == "once"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should allow camelCased accessors on nested hashes" do
|
54
|
+
subject.nested.nestedOne.should == "One"
|
55
|
+
subject.nested.nestedOne = "once"
|
56
|
+
subject.nested.nested_one.should == "once"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should merge well with a Mash" do
|
60
|
+
merged = subject.merge Hashie::Mash.new(
|
61
|
+
:nested => {:fourTimes => "a charm"},
|
62
|
+
:nested3 => {:helloWorld => "hi"}
|
63
|
+
)
|
64
|
+
|
65
|
+
merged.nested.four_times.should == "a charm"
|
66
|
+
merged.nested.fourTimes.should == "a charm"
|
67
|
+
merged.nested3.should be_a(Hashie::SCHash)
|
68
|
+
merged.nested3.hello_world.should == "hi"
|
69
|
+
merged.nested3.helloWorld.should == "hi"
|
70
|
+
merged[:nested3][:helloWorld].should == "hi"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should update well with a Mash" do
|
74
|
+
subject.update Hashie::Mash.new(
|
75
|
+
:nested => {:fourTimes => "a charm"},
|
76
|
+
:nested3 => {:helloWorld => "hi"}
|
77
|
+
)
|
78
|
+
|
79
|
+
subject.nested.four_times.should == "a charm"
|
80
|
+
subject.nested.fourTimes.should == "a charm"
|
81
|
+
subject.nested3.should be_a(Hashie::SCHash)
|
82
|
+
subject.nested3.hello_world.should == "hi"
|
83
|
+
subject.nested3.helloWorld.should == "hi"
|
84
|
+
subject[:nested3][:helloWorld].should == "hi"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should merge well with a Hash" do
|
88
|
+
merged = subject.merge({
|
89
|
+
:nested => {:fourTimes => "work like a charm"},
|
90
|
+
:nested3 => {:helloWorld => "hi"}
|
91
|
+
})
|
92
|
+
|
93
|
+
merged.nested.four_times.should == "work like a charm"
|
94
|
+
merged.nested.fourTimes.should == "work like a charm"
|
95
|
+
merged.nested3.should be_a(Hashie::SCHash)
|
96
|
+
merged.nested3.hello_world.should == "hi"
|
97
|
+
merged.nested3.helloWorld.should == "hi"
|
98
|
+
merged[:nested3][:helloWorld].should == "hi"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should handle assigning a new Hash and convert it to a snake_case_hash" do
|
102
|
+
subject.nested3 = {:helloWorld => "hi"}
|
103
|
+
|
104
|
+
subject.nested3.should be_a(Hashie::SCHash)
|
105
|
+
subject.nested3.hello_world.should == "hi"
|
106
|
+
subject.nested3.helloWorld.should == "hi"
|
107
|
+
subject[:nested3][:helloWorld].should == "hi"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should allow initializing reader" do
|
111
|
+
subject.nested3!.helloWorld = "hi"
|
112
|
+
subject.nested3.hello_world.should == "hi"
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: snake_case_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tcocca, wzcolon, corprew
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hashie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: simple extension to Hashie::Mash for rubyified keys, all keys are converted
|
84
|
+
to underscore to eliminate horrible camelCasing
|
85
|
+
email: ''
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".document"
|
91
|
+
- ".gemtest"
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.rdoc
|
97
|
+
- Rakefile
|
98
|
+
- lib/hashie/sc_hash.rb
|
99
|
+
- lib/sc_hash.rb
|
100
|
+
- lib/snake_case_hash/version.rb
|
101
|
+
- snake_case_hash.gemspec
|
102
|
+
- spec/sc_hash_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: http://github.com/corprew/snake_case_hash
|
105
|
+
licenses: []
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options:
|
109
|
+
- "--charset=UTF-8"
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: simple extension to Hashie::Mash for rubyified keys
|
128
|
+
test_files:
|
129
|
+
- spec/sc_hash_spec.rb
|
130
|
+
- spec/spec_helper.rb
|