hash_ext 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +18 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hash_ext.gemspec +28 -0
- data/lib/hash_ext/accessible.rb +26 -0
- data/lib/hash_ext/builder.rb +30 -0
- data/lib/hash_ext/indifferent.rb +61 -0
- data/lib/hash_ext/sorted.rb +74 -0
- data/lib/hash_ext/version.rb +3 -0
- data/lib/hash_ext.rb +6 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d728c7347c8b3209b150ae75da2721cda37e272e
|
4
|
+
data.tar.gz: 9f169be4e8f43e1d2a3ff5aa66a4ad5d2557ebd1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7ada52ce084a077b70dd3a935b5f28269ca91a119db70eb2cf8fe899c7649fdb7cafe30f07279f3dab21df2ca9043d943c03aae577a5a105fb644c603caf940
|
7
|
+
data.tar.gz: 8db3bfbe191bf2ab8b16ec9f4c7146b55e0ce7d007460c415c6ce719b5d21320bc65fc49735092639802c00741749549640c918b31242c779b63b4a4be103a6d
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
hash_ext
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Gabriel Naiman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# HashExt
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/hash_ext.png)](https://rubygems.org/gems/hash_ext)
|
4
|
+
[![Build Status](https://travis-ci.org/gabynaiman/hash_ext.png?branch=master)](https://travis-ci.org/gabynaiman/hash_ext)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/gabynaiman/hash_ext/badge.png?branch=master)](https://coveralls.io/r/gabynaiman/hash_ext?branch=master)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/gabynaiman/hash_ext.png)](https://codeclimate.com/github/gabynaiman/hash_ext)
|
7
|
+
[![Dependency Status](https://gemnasium.com/gabynaiman/hash_ext.png)](https://gemnasium.com/gabynaiman/hash_ext)
|
8
|
+
|
9
|
+
Hash extensions without monkey patching
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'hash_ext'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install hash_ext
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### Available classes
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Hash::Accessible
|
33
|
+
Hash::Builder
|
34
|
+
Hash::Indifferent
|
35
|
+
Hash::Sorted
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
|
42
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/hash_ext.
|
47
|
+
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
52
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:spec) do |t|
|
5
|
+
t.libs << 'spec'
|
6
|
+
t.pattern = 'spec/**/*_spec.rb'
|
7
|
+
t.verbose = false
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :spec
|
11
|
+
|
12
|
+
desc 'Pry console'
|
13
|
+
task :console do
|
14
|
+
require 'hash_ext'
|
15
|
+
require 'pry'
|
16
|
+
ARGV.clear
|
17
|
+
Pry.start
|
18
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hash_ext"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/hash_ext.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_ext/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hash_ext'
|
8
|
+
spec.version = HashExt::VERSION
|
9
|
+
spec.authors = ['Gabriel Naiman']
|
10
|
+
spec.email = ['gabynaiman@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Hash extensions without monkey patching'
|
13
|
+
spec.description = 'Hash extensions without monkey patching'
|
14
|
+
spec.homepage = 'https://github.com/gabynaiman/hash_ext'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 4.7'
|
25
|
+
spec.add_development_dependency 'turn', '~> 0.9'
|
26
|
+
spec.add_development_dependency 'simplecov'
|
27
|
+
spec.add_development_dependency 'pry-nav'
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Hash
|
2
|
+
class Accessible < Indifferent
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def method_missing(method, *args, &block)
|
7
|
+
if method.to_s.end_with? '='
|
8
|
+
key = method[0..-2]
|
9
|
+
self[key] = make_accessible args[0]
|
10
|
+
else
|
11
|
+
make_accessible self[method]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_accessible(value)
|
16
|
+
if value.kind_of? Hash
|
17
|
+
self.class.new value
|
18
|
+
elsif value.kind_of? Array
|
19
|
+
value.map { |v| make_accessible v }
|
20
|
+
else
|
21
|
+
value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Hash
|
2
|
+
class Builder
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@hash = {}
|
6
|
+
@scopes = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
yield self
|
11
|
+
@hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
if block
|
16
|
+
@scopes.push method
|
17
|
+
block.call
|
18
|
+
@scopes.pop
|
19
|
+
else
|
20
|
+
current_scope = @scopes.inject(@hash) { |h,s| h[s] ||= {} }
|
21
|
+
current_scope[method] = args.first
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.build(&block)
|
26
|
+
new.build(&block)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class Hash
|
2
|
+
class Indifferent < Hash
|
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
|
61
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
class Hash
|
2
|
+
class Sorted
|
3
|
+
|
4
|
+
extend Forwardable
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def_delegators :@hash, :[], :[]=, :value?, :fetch, :values_at,
|
8
|
+
:key?, :include?, :empty?,
|
9
|
+
:delete, :delete_if, :clear,
|
10
|
+
:merge, :merge!, :update, :store, :replace,
|
11
|
+
:count, :size, :length,
|
12
|
+
:==, :eql, :hash
|
13
|
+
|
14
|
+
def_delegators :to_h, :to_a, :to_s, :inspect,
|
15
|
+
:select, :reject, :flatten,
|
16
|
+
:as_json, :to_json
|
17
|
+
|
18
|
+
def initialize(hash={}, direction=:asc, &sort_criteria)
|
19
|
+
@hash = hash
|
20
|
+
@direction = direction
|
21
|
+
@sort_criteria = sort_criteria
|
22
|
+
end
|
23
|
+
|
24
|
+
def keys
|
25
|
+
sorted_keys = @hash.sort_by(&sort_criteria).map { |k,v| k }
|
26
|
+
direction == :asc ? sorted_keys : sorted_keys.reverse
|
27
|
+
end
|
28
|
+
|
29
|
+
def values
|
30
|
+
keys.map { |k| self[k] }
|
31
|
+
end
|
32
|
+
|
33
|
+
def each
|
34
|
+
return enum_for(:each) unless block_given?
|
35
|
+
keys.each { |k| yield k, self[k]}
|
36
|
+
end
|
37
|
+
alias_method :each_pair, :each
|
38
|
+
|
39
|
+
def each_key
|
40
|
+
return enum_for(:each_key) unless block_given?
|
41
|
+
keys.each { |k| yield k }
|
42
|
+
end
|
43
|
+
|
44
|
+
def each_value
|
45
|
+
return enum_for(:each_value) unless block_given?
|
46
|
+
keys.each { |k| yield self[k] }
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_h
|
50
|
+
each_with_object({}) { |(k,v),h| h[k] = v }
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
attr_reader :direction, :sort_criteria
|
56
|
+
|
57
|
+
class << self
|
58
|
+
|
59
|
+
def ascending(hash={}, &sort_criteria)
|
60
|
+
new hash, :asc, &sort_criteria
|
61
|
+
end
|
62
|
+
alias_method :asc, :ascending
|
63
|
+
|
64
|
+
def descending(hash={}, &sort_criteria)
|
65
|
+
new hash, :desc, &sort_criteria
|
66
|
+
end
|
67
|
+
alias_method :desc, :descending
|
68
|
+
|
69
|
+
private :new
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/hash_ext.rb
ADDED
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Naiman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: turn
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
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
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-nav
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Hash extensions without monkey patching
|
98
|
+
email:
|
99
|
+
- gabynaiman@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .coveralls.yml
|
105
|
+
- .gitignore
|
106
|
+
- .ruby-gemset
|
107
|
+
- .ruby-version
|
108
|
+
- .travis.yml
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- hash_ext.gemspec
|
116
|
+
- lib/hash_ext.rb
|
117
|
+
- lib/hash_ext/accessible.rb
|
118
|
+
- lib/hash_ext/builder.rb
|
119
|
+
- lib/hash_ext/indifferent.rb
|
120
|
+
- lib/hash_ext/sorted.rb
|
121
|
+
- lib/hash_ext/version.rb
|
122
|
+
homepage: https://github.com/gabynaiman/hash_ext
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.8
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Hash extensions without monkey patching
|
146
|
+
test_files: []
|