hash_ext 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/.travis.yml +1 -0
- data/Gemfile +1 -3
- data/README.md +67 -10
- data/Rakefile +1 -0
- data/hash_ext.gemspec +12 -5
- data/lib/hash_ext.rb +1 -0
- data/lib/hash_ext/indifferent.rb +1 -59
- data/lib/hash_ext/normalized.rb +70 -0
- data/lib/hash_ext/version.rb +1 -1
- metadata +48 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f9ae9f704e9ea7ee1f52968bbbc23bc94e0d1e9
|
4
|
+
data.tar.gz: facfc3a675152c71e3edd6d32402b7694886678e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a31b105fc8bccb7f15314327cc0001b6443987f344177a296380a8ceacd7e2aeb8c35a258cfa1e7ef05c5b70996b1946c3f252fadd583eb750f513da9be17a0
|
7
|
+
data.tar.gz: 3cf7ad00240b033eb6f559b916e98f41b518fbd958e1245751566e143472e37656ba1c8be31918fafc119672cdebf18dd571e8dcc1d4e7f661645cbfe87c5774
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# HashExt
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/hash_ext.
|
4
|
-
[![Build Status](https://travis-ci.org/gabynaiman/hash_ext.
|
5
|
-
[![Coverage Status](https://coveralls.io/repos/gabynaiman/hash_ext/badge.
|
6
|
-
[![Code Climate](https://codeclimate.com/github/gabynaiman/hash_ext.
|
7
|
-
[![Dependency Status](https://gemnasium.com/gabynaiman/hash_ext.
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/hash_ext.svg)](https://rubygems.org/gems/hash_ext)
|
4
|
+
[![Build Status](https://travis-ci.org/gabynaiman/hash_ext.svg?branch=master)](https://travis-ci.org/gabynaiman/hash_ext)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/gabynaiman/hash_ext/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/hash_ext?branch=master)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/gabynaiman/hash_ext.svg)](https://codeclimate.com/github/gabynaiman/hash_ext)
|
7
|
+
[![Dependency Status](https://gemnasium.com/gabynaiman/hash_ext.svg)](https://gemnasium.com/gabynaiman/hash_ext)
|
8
8
|
|
9
9
|
Hash extensions without monkey patching
|
10
10
|
|
@@ -26,13 +26,70 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
###
|
29
|
+
### Accessible
|
30
30
|
|
31
31
|
```ruby
|
32
|
-
Hash::Accessible
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
hash = Hash::Accessible.new key_1: 1, key_2: { key_3: 3 }
|
33
|
+
|
34
|
+
hash[:key_1] # => 1
|
35
|
+
hash.key_1 # => 1
|
36
|
+
|
37
|
+
hash[:key_2] # => {key_3: 3}
|
38
|
+
hash.key_2 # => {key_3: 3}
|
39
|
+
|
40
|
+
hash[:key_2][:key_3] # => 3
|
41
|
+
hash.key_2.key_3 # => 3
|
42
|
+
```
|
43
|
+
|
44
|
+
### Builder
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
hash = Hash::Builder.build do |h|
|
48
|
+
h.key_1 1
|
49
|
+
h.key_2 do
|
50
|
+
h.key_3 3
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
hash # => {key_1: 1, key_2: {key_3: 3}}
|
55
|
+
```
|
56
|
+
|
57
|
+
### Normalized
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
hash = Hash::Normalized.new { |key| key.upcase }
|
61
|
+
|
62
|
+
hash['key_1'] = 1
|
63
|
+
|
64
|
+
hash['key_1'] # => 1
|
65
|
+
hash['KEY_1'] # => 1
|
66
|
+
```
|
67
|
+
|
68
|
+
### Indifferent
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
hash = Hash::Indifferent.new
|
72
|
+
|
73
|
+
hash['key_1'] = 1
|
74
|
+
hash[:key_2] = 2
|
75
|
+
|
76
|
+
hash['key_1'] # => 1
|
77
|
+
hash[:key_1] # => 1
|
78
|
+
|
79
|
+
hash['key_2'] # => 2
|
80
|
+
hash[:key_2] # => 2
|
81
|
+
```
|
82
|
+
|
83
|
+
### Sorted
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
hash = Hash::Sorted.asc { |k,v| k.to_s }
|
87
|
+
|
88
|
+
hash[:c] = 1
|
89
|
+
hash[:b] = 2
|
90
|
+
hash[:a] = 3
|
91
|
+
|
92
|
+
hash.keys # => [:a, :b, :c]
|
36
93
|
```
|
37
94
|
|
38
95
|
## Development
|
data/Rakefile
CHANGED
data/hash_ext.gemspec
CHANGED
@@ -20,9 +20,16 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
22
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
-
spec.add_development_dependency 'rake'
|
24
|
-
spec.add_development_dependency 'minitest', '~>
|
25
|
-
spec.add_development_dependency '
|
26
|
-
spec.add_development_dependency 'simplecov'
|
27
|
-
spec.add_development_dependency '
|
23
|
+
spec.add_development_dependency 'rake', '~> 11.0'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
spec.add_development_dependency 'minitest-colorin', '~> 0.1'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.12'
|
27
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
28
|
+
spec.add_development_dependency 'pry-nav', '~> 0.2'
|
29
|
+
|
30
|
+
if RUBY_VERSION < '2'
|
31
|
+
spec.add_development_dependency 'term-ansicolor', '~> 1.3.0'
|
32
|
+
spec.add_development_dependency 'tins', '~> 1.6.0'
|
33
|
+
spec.add_development_dependency 'json', '~> 1.8'
|
34
|
+
end
|
28
35
|
end
|
data/lib/hash_ext.rb
CHANGED
data/lib/hash_ext/indifferent.rb
CHANGED
@@ -1,61 +1,3 @@
|
|
1
1
|
class Hash
|
2
|
-
|
3
|
-
|
4
|
-
def initialize(hash={})
|
5
|
-
update hash
|
6
|
-
end
|
7
|
-
|
8
|
-
def [](key)
|
9
|
-
super normalize_key(key)
|
10
|
-
end
|
11
|
-
|
12
|
-
def []=(key, value)
|
13
|
-
super normalize_key(key), normalize_value(value)
|
14
|
-
end
|
15
|
-
alias_method :store, :[]=
|
16
|
-
|
17
|
-
def delete(key)
|
18
|
-
super normalize_key(key)
|
19
|
-
end
|
20
|
-
|
21
|
-
def update(hash)
|
22
|
-
hash.each do |key, value|
|
23
|
-
store normalize_key(key), normalize_value(value)
|
24
|
-
end
|
25
|
-
self
|
26
|
-
end
|
27
|
-
alias_method :merge!, :update
|
28
|
-
|
29
|
-
def merge(hash)
|
30
|
-
dup.update hash
|
31
|
-
end
|
32
|
-
|
33
|
-
def key?(key)
|
34
|
-
super normalize_key(key)
|
35
|
-
end
|
36
|
-
alias_method :include?, :key?
|
37
|
-
alias_method :has_key?, :key?
|
38
|
-
alias_method :member?, :key?
|
39
|
-
|
40
|
-
def fetch(key, *args, &block)
|
41
|
-
super normalize_key(key), *args, &block
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def normalize_key(key)
|
47
|
-
key.kind_of?(String) ? key.to_sym : key
|
48
|
-
end
|
49
|
-
|
50
|
-
def normalize_value(value)
|
51
|
-
if value.kind_of? Hash
|
52
|
-
self.class.new value
|
53
|
-
elsif value.kind_of? Array
|
54
|
-
value.map { |v| normalize_value v }
|
55
|
-
else
|
56
|
-
value
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
2
|
+
Indifferent = Normalized.subclass { |key| key.kind_of?(String) ? key.to_sym : key }
|
61
3
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Hash
|
2
|
+
class Normalized < Hash
|
3
|
+
|
4
|
+
def self.subclass(&block)
|
5
|
+
Class.new(self) do
|
6
|
+
define_method :initialize do |hash={}|
|
7
|
+
super hash, &block
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(hash={}, &block)
|
13
|
+
@normalization_block = block
|
14
|
+
update hash
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
super normalize_key(key)
|
19
|
+
end
|
20
|
+
|
21
|
+
def []=(key, value)
|
22
|
+
super normalize_key(key), normalize_value(value)
|
23
|
+
end
|
24
|
+
alias_method :store, :[]=
|
25
|
+
|
26
|
+
def delete(key)
|
27
|
+
super normalize_key(key)
|
28
|
+
end
|
29
|
+
|
30
|
+
def update(hash)
|
31
|
+
hash.each do |key, value|
|
32
|
+
store normalize_key(key), normalize_value(value)
|
33
|
+
end
|
34
|
+
self
|
35
|
+
end
|
36
|
+
alias_method :merge!, :update
|
37
|
+
|
38
|
+
def merge(hash)
|
39
|
+
dup.update hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def key?(key)
|
43
|
+
super normalize_key(key)
|
44
|
+
end
|
45
|
+
alias_method :include?, :key?
|
46
|
+
alias_method :has_key?, :key?
|
47
|
+
alias_method :member?, :key?
|
48
|
+
|
49
|
+
def fetch(key, *args, &block)
|
50
|
+
super normalize_key(key), *args, &block
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def normalize_key(key)
|
56
|
+
@normalization_block.call key
|
57
|
+
end
|
58
|
+
|
59
|
+
def normalize_value(value)
|
60
|
+
if value.kind_of? Hash
|
61
|
+
self.class.new value
|
62
|
+
elsif value.kind_of? Array
|
63
|
+
value.map { |v| normalize_value v }
|
64
|
+
else
|
65
|
+
value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
data/lib/hash_ext/version.rb
CHANGED
metadata
CHANGED
@@ -1,99 +1,113 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '11.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: '0'
|
40
|
+
version: '11.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '5.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: '5.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: minitest-colorin
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.1'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
68
|
+
version: '0.1'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
75
|
+
version: '0.12'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
82
|
+
version: '0.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.8'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.8'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry-nav
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- -
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
103
|
+
version: '0.2'
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- -
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
110
|
+
version: '0.2'
|
97
111
|
description: Hash extensions without monkey patching
|
98
112
|
email:
|
99
113
|
- gabynaiman@gmail.com
|
@@ -101,11 +115,11 @@ executables: []
|
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
103
117
|
files:
|
104
|
-
- .coveralls.yml
|
105
|
-
- .gitignore
|
106
|
-
- .ruby-gemset
|
107
|
-
- .ruby-version
|
108
|
-
- .travis.yml
|
118
|
+
- ".coveralls.yml"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".ruby-gemset"
|
121
|
+
- ".ruby-version"
|
122
|
+
- ".travis.yml"
|
109
123
|
- Gemfile
|
110
124
|
- LICENSE.txt
|
111
125
|
- README.md
|
@@ -117,6 +131,7 @@ files:
|
|
117
131
|
- lib/hash_ext/accessible.rb
|
118
132
|
- lib/hash_ext/builder.rb
|
119
133
|
- lib/hash_ext/indifferent.rb
|
134
|
+
- lib/hash_ext/normalized.rb
|
120
135
|
- lib/hash_ext/sorted.rb
|
121
136
|
- lib/hash_ext/version.rb
|
122
137
|
homepage: https://github.com/gabynaiman/hash_ext
|
@@ -129,17 +144,17 @@ require_paths:
|
|
129
144
|
- lib
|
130
145
|
required_ruby_version: !ruby/object:Gem::Requirement
|
131
146
|
requirements:
|
132
|
-
- -
|
147
|
+
- - ">="
|
133
148
|
- !ruby/object:Gem::Version
|
134
149
|
version: '0'
|
135
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
151
|
requirements:
|
137
|
-
- -
|
152
|
+
- - ">="
|
138
153
|
- !ruby/object:Gem::Version
|
139
154
|
version: '0'
|
140
155
|
requirements: []
|
141
156
|
rubyforge_project:
|
142
|
-
rubygems_version: 2.
|
157
|
+
rubygems_version: 2.5.1
|
143
158
|
signing_key:
|
144
159
|
specification_version: 4
|
145
160
|
summary: Hash extensions without monkey patching
|