hash-pipe 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +46 -0
- data/README.md +2 -0
- data/hash-pipe.gemspec +20 -0
- data/lib/hash-pipe.rb +4 -0
- data/lib/hash_pipe/key_conversion.rb +20 -0
- data/lib/hash_pipe/value_checks.rb +17 -0
- data/spec/hash-pipe/key_conversion_spec.rb +30 -0
- data/spec/hash-pipe/value_checks_spec.rb +24 -0
- data/spec/spec_helper.rb +4 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2726ae03f2f526feb6a99d7e9d58dcd6da86568b
|
4
|
+
data.tar.gz: b2983bb6aa9033805a3468cb8f858a837980a839
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8354bf2a610ee5edb9a2b05a08480efdf534566a02b77f2400935fd3d3969de5f8a02dddb1315c4e41f0b1135eb6455b80fde1b7ae54987c6f178350d2350dbe
|
7
|
+
data.tar.gz: 26ce6d96912a5139be19398c27540a3b2042710f691e4a8c9b3f467a946b5e5d77f1d946a4fecaf66b6cb31e0aab18ccc40471a41b89bf4998a433f3fb3fa4e9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash-pipe (0.0.1)
|
5
|
+
activesupport (~> 4.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (4.0.3)
|
11
|
+
i18n (~> 0.6, >= 0.6.4)
|
12
|
+
minitest (~> 4.2)
|
13
|
+
multi_json (~> 1.3)
|
14
|
+
thread_safe (~> 0.1)
|
15
|
+
tzinfo (~> 0.3.37)
|
16
|
+
atomic (1.1.15)
|
17
|
+
coderay (1.1.0)
|
18
|
+
diff-lcs (1.2.5)
|
19
|
+
i18n (0.6.9)
|
20
|
+
method_source (0.8.2)
|
21
|
+
minitest (4.7.5)
|
22
|
+
multi_json (1.8.4)
|
23
|
+
pry (0.9.12.6)
|
24
|
+
coderay (~> 1.0)
|
25
|
+
method_source (~> 0.8)
|
26
|
+
slop (~> 3.4)
|
27
|
+
rspec (2.14.1)
|
28
|
+
rspec-core (~> 2.14.0)
|
29
|
+
rspec-expectations (~> 2.14.0)
|
30
|
+
rspec-mocks (~> 2.14.0)
|
31
|
+
rspec-core (2.14.7)
|
32
|
+
rspec-expectations (2.14.4)
|
33
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
34
|
+
rspec-mocks (2.14.4)
|
35
|
+
slop (3.4.7)
|
36
|
+
thread_safe (0.2.0)
|
37
|
+
atomic (>= 1.1.7, < 2)
|
38
|
+
tzinfo (0.3.38)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
hash-pipe!
|
45
|
+
pry (~> 0.9)
|
46
|
+
rspec (~> 2.14)
|
data/README.md
ADDED
data/hash-pipe.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "hash-pipe"
|
5
|
+
s.version = "0.0.1"
|
6
|
+
s.authors = ["Damien Timewell"]
|
7
|
+
s.email = ["mail@damientimewell.com"]
|
8
|
+
s.licenses = ['MIT']
|
9
|
+
s.homepage = "https://github.com/idlefingers/hash-pipe"
|
10
|
+
s.summary = "A small collection of useful hash extensions"
|
11
|
+
s.description = ""
|
12
|
+
|
13
|
+
s.add_dependency 'activesupport', '~> 4.0'
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec", '~> 2.14'
|
16
|
+
s.add_development_dependency "pry", '~> 0.9'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
20
|
+
end
|
data/lib/hash-pipe.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Hash.class_eval do
|
2
|
+
|
3
|
+
def self.convert_keys(hash, method = :underscore)
|
4
|
+
if hash.is_a?(Array)
|
5
|
+
hash.collect {|h| convert_keys(h, method) }
|
6
|
+
else
|
7
|
+
hash_array = hash.collect do |key,value|
|
8
|
+
value = value.is_a?(Hash) ? convert_keys(value, method) : value
|
9
|
+
[ key.to_s.send(method), value ]
|
10
|
+
end
|
11
|
+
|
12
|
+
Hash[hash_array]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def convert_keys(method = :underscore)
|
17
|
+
Hash.convert_keys self, method
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Hash.class_eval do
|
2
|
+
|
3
|
+
def no_blank_values
|
4
|
+
self.reject { |k,v| v.blank? }
|
5
|
+
end
|
6
|
+
|
7
|
+
def except(*keys)
|
8
|
+
keys.collect! &:to_s
|
9
|
+
self.reject {|k,v| keys.include? k.to_s }
|
10
|
+
end
|
11
|
+
|
12
|
+
def only(*keys)
|
13
|
+
keys.collect! &:to_s
|
14
|
+
self.select {|k,v| keys.include? k.to_s }
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'key_conversion' do
|
4
|
+
|
5
|
+
it 'should convert keys to lowercase by default' do
|
6
|
+
{ 'HELLO' => 'world' }.convert_keys.should eq 'hello' => 'world'
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should convert the key to a string if it is a symbol' do
|
10
|
+
{ hello: "world" }.convert_keys.should eq 'hello' => 'world'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should accept any method you can call on a string to convert the key' do
|
14
|
+
{ hello: "world" }.convert_keys(:upcase).should eq 'HELLO' => 'world'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should convert nested hashes with the same logic' do
|
18
|
+
{ 'HELLO' => { 'MR' => 'world' } }.convert_keys.should eq 'hello' => {'mr' => 'world'}
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should be both an instance and class method' do
|
22
|
+
Hash.new.should respond_to(:convert_keys)
|
23
|
+
Hash.should respond_to(:convert_keys)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should convert an array of hashes with the same logic' do
|
27
|
+
Hash.convert_keys([{ 'HELLO' => 'world' }, { 'FOO' => 'bar' }]).should eq [{ 'hello' => 'world' }, { 'foo' => 'bar' }]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'value checks' do
|
4
|
+
let(:hash) { {foo: "bar", baz: "box", zap: nil} }
|
5
|
+
|
6
|
+
describe 'no_blank_values' do
|
7
|
+
it 'should return the hash without any nil values' do
|
8
|
+
hash.no_blank_values.should_not have_key(:zap)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "except" do
|
13
|
+
it 'should return the hash except keys listed' do
|
14
|
+
hash.except(:baz).should_not have_key(:baz)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "only" do
|
19
|
+
it 'should return a hash containing only the keys listed' do
|
20
|
+
hash.only(:baz).should have_key(:baz)
|
21
|
+
hash.only(:baz).should_not have_key(:foo)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-pipe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damien Timewell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- mail@damientimewell.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- hash-pipe.gemspec
|
69
|
+
- lib/hash-pipe.rb
|
70
|
+
- lib/hash_pipe/key_conversion.rb
|
71
|
+
- lib/hash_pipe/value_checks.rb
|
72
|
+
- spec/hash-pipe/key_conversion_spec.rb
|
73
|
+
- spec/hash-pipe/value_checks_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: https://github.com/idlefingers/hash-pipe
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A small collection of useful hash extensions
|
99
|
+
test_files:
|
100
|
+
- spec/hash-pipe/key_conversion_spec.rb
|
101
|
+
- spec/hash-pipe/value_checks_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
has_rdoc:
|