HashEx 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/HashEx.gemspec +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/HashEx.rb +185 -0
- data/lib/HashEx/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da62151ef382c538c759bec9bac47c0b335a0ce12c3809b0793803e6ac61dfc4
|
4
|
+
data.tar.gz: 06b2ce34ac1600de78719fc14cb483e8b424b6560f80ef078cf796fc797b584e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f7d35010ef4f6d3cce3586484672b09cfcc2dee7d5e66449a94564ada85a5ab8d00aee02d75350199ddac423604a5fc34d6de692a863d012f9f1e3c5216264ed
|
7
|
+
data.tar.gz: 7148a5ebde986a122f1b35ce1e6184602c72ee8bfe2bb4e335a043aa379e893cdb5db92cfaa054c9deead247cace6a08f9df2ec76d2a9b2b0699de5fa81e41a8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
HashEx (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.1)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
HashEx!
|
30
|
+
bundler (~> 1.16)
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.1
|
data/HashEx.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "HashEx/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "HashEx"
|
8
|
+
spec.version = HashEx::VERSION
|
9
|
+
spec.authors = ["eGust"]
|
10
|
+
spec.email = ["egustc@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enhanced Hash, HashEx::JsObject works like JS Object}
|
13
|
+
spec.description = %q{HashEx::Base is an abstract class need to override #convert_key.
|
14
|
+
HashEx::JsObject is a JavaScript-Object-like Hash. }
|
15
|
+
spec.homepage = "https://github.com/eGust/HashEx"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 TODO: Write your name
|
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,41 @@
|
|
1
|
+
# HashEx
|
2
|
+
|
3
|
+
1. `HashEx::JsObject` - JavaScript-Object-like hash.
|
4
|
+
1. `HashEx::Base` - Abstract base class.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'HashEx'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install HashEx
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### `HashEx::Base`
|
25
|
+
|
26
|
+
Just override `HashEx::Base#convert_key` to create your own one
|
27
|
+
|
28
|
+
### `HashEx::JsObject`
|
29
|
+
|
30
|
+
It works like JS Object. For an instance `h`:
|
31
|
+
|
32
|
+
1. `h[:key]`, `h['key']` and `h.key` are equal.
|
33
|
+
1. `h.a = { foo: { bar: { baz: 123} } }` will convert `Hash` to `HashEx::JsObject` recursively. `h.a.foo` and `h.a.foo.bar` will be instances of `HashEx::JsObject`.
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/eGust/HashEx.
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "HashEx"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/HashEx.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
require "HashEx/version"
|
2
|
+
|
3
|
+
module HashEx
|
4
|
+
class Base < Hash
|
5
|
+
protected
|
6
|
+
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
7
|
+
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
8
|
+
|
9
|
+
public
|
10
|
+
def initialize(constructor = {})
|
11
|
+
if constructor.respond_to?(:to_hash)
|
12
|
+
super()
|
13
|
+
update(constructor)
|
14
|
+
|
15
|
+
hash = constructor.to_hash
|
16
|
+
self.default = hash.default if hash.default
|
17
|
+
self.default_proc = hash.default_proc if hash.default_proc
|
18
|
+
else
|
19
|
+
super(constructor)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.[](*args)
|
24
|
+
new.merge!(Hash[*args])
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(method, *args, &block)
|
28
|
+
key = method.to_s
|
29
|
+
if key[-1] == '='
|
30
|
+
self[key.chop] = args[0]
|
31
|
+
else
|
32
|
+
self[key]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def key?(key)
|
37
|
+
super(convert_key(key))
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :include?, :key?
|
41
|
+
alias_method :has_key?, :key?
|
42
|
+
alias_method :member?, :key?
|
43
|
+
|
44
|
+
def [](key)
|
45
|
+
super(convert_key(key))
|
46
|
+
end
|
47
|
+
|
48
|
+
def fetch(key, *extras)
|
49
|
+
super(convert_key(key), *extras)
|
50
|
+
end
|
51
|
+
|
52
|
+
def []=(key, value)
|
53
|
+
regular_writer(convert_key(key), convert_value(value))
|
54
|
+
end
|
55
|
+
|
56
|
+
alias_method :store, :[]=
|
57
|
+
|
58
|
+
def update(other_hash)
|
59
|
+
if other_hash.is_a? self.class
|
60
|
+
super(other_hash)
|
61
|
+
else
|
62
|
+
other_hash.to_hash.each_pair do |key, value|
|
63
|
+
if block_given? && key?(key)
|
64
|
+
value = yield(convert_key(key), self[key], value)
|
65
|
+
end
|
66
|
+
regular_writer(convert_key(key), convert_value(value))
|
67
|
+
end
|
68
|
+
self
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
alias_method :merge!, :update
|
73
|
+
|
74
|
+
if Hash.method_defined?(:dig)
|
75
|
+
def dig(*args)
|
76
|
+
args[0] = convert_key(args[0]) if args.size > 0
|
77
|
+
super(*args)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def default(*args)
|
82
|
+
super(*args.map { |arg| convert_key(arg) })
|
83
|
+
end
|
84
|
+
|
85
|
+
def values_at(*indices)
|
86
|
+
indices.collect { |key| self[convert_key(key)] }
|
87
|
+
end
|
88
|
+
|
89
|
+
if Hash.method_defined?(:fetch_values)
|
90
|
+
def fetch_values(*indices, &block)
|
91
|
+
indices.collect { |key| fetch(key, &block) }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def merge(hash, &block)
|
96
|
+
dup.update(hash, &block)
|
97
|
+
end
|
98
|
+
|
99
|
+
def reverse_merge(other_hash)
|
100
|
+
super(self.class.new(other_hash))
|
101
|
+
end
|
102
|
+
|
103
|
+
def reverse_merge!(other_hash)
|
104
|
+
super(self.class.new(other_hash))
|
105
|
+
end
|
106
|
+
|
107
|
+
def replace(other_hash)
|
108
|
+
super(self.class.new(other_hash))
|
109
|
+
end
|
110
|
+
|
111
|
+
def delete(key)
|
112
|
+
super(convert_key(key))
|
113
|
+
end
|
114
|
+
|
115
|
+
def select(*args, &block)
|
116
|
+
return to_enum(:select) unless block_given?
|
117
|
+
dup.tap { |hash| hash.select!(*args, &block) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def reject(*args, &block)
|
121
|
+
return to_enum(:reject) unless block_given?
|
122
|
+
dup.tap { |hash| hash.reject!(*args, &block) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def transform_keys(*args, &block)
|
126
|
+
return to_enum(:transform_keys) unless block_given?
|
127
|
+
dup.tap { |hash| hash.transform_keys!(*args, &block) }
|
128
|
+
end
|
129
|
+
|
130
|
+
def transform_values(*args, &block)
|
131
|
+
return to_enum(:transform_values) unless block_given?
|
132
|
+
dup.tap { |hash| hash.transform_values!(*args, &block) }
|
133
|
+
end
|
134
|
+
|
135
|
+
def slice(*keys)
|
136
|
+
keys.map! { |key| convert_key(key) }
|
137
|
+
self.class.new(super)
|
138
|
+
end
|
139
|
+
|
140
|
+
def slice!(*keys)
|
141
|
+
keys.map! { |key| convert_key(key) }
|
142
|
+
super
|
143
|
+
end
|
144
|
+
|
145
|
+
def compact
|
146
|
+
dup.tap(&:compact!)
|
147
|
+
end
|
148
|
+
|
149
|
+
def to_h
|
150
|
+
h = Hash.new
|
151
|
+
set_defaults(h)
|
152
|
+
|
153
|
+
each do |key, value|
|
154
|
+
h[key] = convert_value(value, revert: true)
|
155
|
+
end
|
156
|
+
h
|
157
|
+
end
|
158
|
+
|
159
|
+
protected
|
160
|
+
def convert_key(key)
|
161
|
+
raise "Method #convert_key must be rewritten"
|
162
|
+
end
|
163
|
+
|
164
|
+
def convert_value(value, revert: false)
|
165
|
+
if value.is_a? Hash
|
166
|
+
if revert
|
167
|
+
value.to_h
|
168
|
+
else
|
169
|
+
value.is_a?(self.class) ? value : self.class.new(value)
|
170
|
+
end
|
171
|
+
elsif value.is_a? Array
|
172
|
+
value.map { |e| convert_value(e) }
|
173
|
+
else
|
174
|
+
value
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
class JsObject < Base
|
180
|
+
protected
|
181
|
+
def convert_key(key)
|
182
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: HashEx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- eGust
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-31 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: "HashEx::Base is an abstract class need to override #convert_key.\nHashEx::JsObject
|
56
|
+
is a JavaScript-Object-like Hash. "
|
57
|
+
email:
|
58
|
+
- egustc@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- HashEx.gemspec
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- lib/HashEx.rb
|
75
|
+
- lib/HashEx/version.rb
|
76
|
+
homepage: https://github.com/eGust/HashEx
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.7.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Enhanced Hash, HashEx::JsObject works like JS Object
|
100
|
+
test_files: []
|