hash_dial 1.0.1 → 1.0.2
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 +4 -4
- data/Gemfile.lock +35 -35
- data/README.md +1 -62
- data/hash_dial.gemspec +31 -29
- data/lib/hash_dial.rb +31 -29
- data/lib/hash_dial/hash_dialler.rb +76 -76
- data/lib/hash_dial/version.rb +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf7a8293144381456540a20c18cd5255e697f52d92d0dcbbde771ab7613d7d60
|
4
|
+
data.tar.gz: a34d06f35126cddb77126fbf0f30463440082c95337447595d24c07101dcce5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2152d053c75459522cb877896add0d324406e6b99a8092985d6e92f117958732cc2ca5906e90664827b38685ad67c50d630676cd95b1ad025ab496b5da7151e
|
7
|
+
data.tar.gz: 23c1c0c5c6ed8941d39656cf06352cdcb76ffcc03733a691637008e12e869ef95438bb7abb23f5761ac677d4f7572998fbf1ffb83fac73a1af1f2f42f3bbf967
|
data/Gemfile.lock
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
hash_dial (1.0.
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.3)
|
10
|
-
rake (10.5.0)
|
11
|
-
rspec (3.8.0)
|
12
|
-
rspec-core (~> 3.8.0)
|
13
|
-
rspec-expectations (~> 3.8.0)
|
14
|
-
rspec-mocks (~> 3.8.0)
|
15
|
-
rspec-core (3.8.0)
|
16
|
-
rspec-support (~> 3.8.0)
|
17
|
-
rspec-expectations (3.8.2)
|
18
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
-
rspec-support (~> 3.8.0)
|
20
|
-
rspec-mocks (3.8.0)
|
21
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
-
rspec-support (~> 3.8.0)
|
23
|
-
rspec-support (3.8.0)
|
24
|
-
|
25
|
-
PLATFORMS
|
26
|
-
ruby
|
27
|
-
|
28
|
-
DEPENDENCIES
|
29
|
-
bundler (~> 1.17)
|
30
|
-
hash_dial!
|
31
|
-
rake (~> 10.0)
|
32
|
-
rspec (~> 3.0)
|
33
|
-
|
34
|
-
BUNDLED WITH
|
35
|
-
1.17.2
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash_dial (1.0.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.17)
|
30
|
+
hash_dial!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.17.2
|
data/README.md
CHANGED
@@ -1,64 +1,3 @@
|
|
1
1
|
# HashDial (Ruby Gem)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
In particular, if you try to access a key on a value that isn't a hash, `dig()` will cause an error where HashDial will not.
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
hash = {a: {b: {c: true}, d: 5}}
|
9
|
-
hash.dig( :a, :d, :c) #=> TypeError: Integer does not have #dig method
|
10
|
-
hash.call(:a, :d, :c) #=> nil
|
11
|
-
hash.call(:a, :b, :c) #=> true
|
12
|
-
```
|
13
|
-
|
14
|
-
**Bonus: you don't even need to fiddle with existing code.** If you have already written something to access a deep hash key, just surround this with `dial` and `call` (rather than changing it to the form above as function parameters).
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
hash[:a][:d][:c] #=> TypeError: no implicit conversion of Symbol into Integer
|
18
|
-
|
19
|
-
#hash → [:a][:d][:c]
|
20
|
-
# ↓ ↓
|
21
|
-
hash.dial[:a][:d][:c].call #=> nil
|
22
|
-
```
|
23
|
-
|
24
|
-
## Explanation
|
25
|
-
|
26
|
-
We use the concept of placing a phone-call: you can 'dial' any set of keys regardless of whether they exist (like entering a phone number), then finally place the 'call'. If the key is invalid for any reason you get nil/default (like a wrong number); otherwise you get the value (you're connected).
|
27
|
-
|
28
|
-
This works by intermediating your request with a HashDialler object. Trying to access keys on this object simply builds up a list of keys to use when you later place the 'call'.
|
29
|
-
|
30
|
-
## Usage
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
require 'hash_dial'
|
34
|
-
```
|
35
|
-
|
36
|
-
### Use it like dig()
|
37
|
-
|
38
|
-
If you want to follow this pattern, it works in the same way. You can't change the default return value when using this pattern.
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
hash.call(:a, :b, :c) # Returns the value at hash[:a][:b][:c] or nil
|
42
|
-
```
|
43
|
-
|
44
|
-
### Use it like a Hash -- allows default return value
|
45
|
-
|
46
|
-
```ruby
|
47
|
-
hash.dial[:a][:b][:c].call # Returns the value at hash[:a][:b][:c] or nil
|
48
|
-
hash.dial[:a][:b][:c].call('Ooops') # Returns the value at hash[:a][:b][:c] or 'Ooops'
|
49
|
-
```
|
50
|
-
|
51
|
-
If you don't do this all in one line, you can access the HashDialler object should you want to manipulate it:
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
dialler = hash.dial # Returns a HashDialler object referencing hash
|
55
|
-
dialler[:a] # Adds :a to the list of keys to dial (returns self)
|
56
|
-
dialler.dial!(:b, :c) # Longhand way of adding more keys (returns self)
|
57
|
-
dialler.undial! # Removes the last-added key (returns self)
|
58
|
-
dialler[:c][:d] # Adds two more keys (returns self)
|
59
|
-
dialler += :e # Adds yet one more (returns self)
|
60
|
-
dialler -= :a # Removes all such keys from the list (returns self)
|
61
|
-
# So far we have dialled [:b][:c][:d][:e]
|
62
|
-
dialler.call # Returns the value at hash[:b][:c][:d][:e] or nil
|
63
|
-
dialler.hangup # Returns the original hash by reference
|
64
|
-
```
|
3
|
+
This gem is deprecated. It is replaced entirely by [KeyDial](https://github.com/ConvincibleMedia/ruby-gem-key_dial) which does the same thing, but works on Hashes, Arrays and Structs as well.
|
data/hash_dial.gemspec
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "hash_dial/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "hash_dial"
|
8
|
-
spec.version = HashDial::VERSION
|
9
|
-
spec.authors = ["Convincible"]
|
10
|
-
spec.email = ["development@convincible.media"]
|
11
|
-
|
12
|
-
spec.summary = "Access (deeply nested) hash keys. Get the value, or nil on any error. (Even safer than Hash::dig)."
|
13
|
-
spec.description = "Avoid all errors when accessing (deeply nested) Hash keys. Safer than dig(), as will quietly return nil (or your default) if the keys requested are invalid for any reason at all.
|
14
|
-
spec.homepage = "https://github.com/ConvincibleMedia/ruby-gem-hash_dial"
|
15
|
-
|
16
|
-
# Specify which files should be added to the gem when it is released.
|
17
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
-
`git ls-files -z`.split("\x0").reject { |f| 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
|
-
spec.required_ruby_version = '
|
25
|
-
|
26
|
-
spec.add_development_dependency "bundler", "~> 1.17"
|
27
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
-
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hash_dial/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_dial"
|
8
|
+
spec.version = HashDial::VERSION
|
9
|
+
spec.authors = ["Convincible"]
|
10
|
+
spec.email = ["development@convincible.media"]
|
11
|
+
|
12
|
+
spec.summary = "Access (deeply nested) hash keys. Get the value, or nil on any error. (Even safer than Hash::dig). Deprecated - use KeyDial."
|
13
|
+
spec.description = "Avoid all errors when accessing (deeply nested) Hash keys. Safer than dig(), as will quietly return nil (or your default) if the keys requested are invalid for any reason at all. This gem is deprecated - use KeyDial instead, which does the same thing but works on Hashes, Arrays and Structs as well."
|
14
|
+
spec.homepage = "https://github.com/ConvincibleMedia/ruby-gem-hash_dial"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| 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
|
+
spec.required_ruby_version = '>= 2.3'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
|
30
|
+
spec.post_install_message = "The 'hash_dial' gem has been deprecated and replaced by 'key_dial'."
|
31
|
+
end
|
data/lib/hash_dial.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
require "hash_dial/version"
|
2
|
-
require "hash_dial/hash_dialler"
|
3
|
-
|
4
|
-
module HashDial
|
5
|
-
|
6
|
-
# Called on a Hash, returns a HashDialler object for that Hash.
|
7
|
-
#
|
8
|
-
# @param lookup Parameters to this method form initial hash keys to dial. This is unnecessary but works anyway. For simplicity, dial hash keys by accessing them as if HashDialler were a Hash.
|
9
|
-
#
|
10
|
-
def to_dial(*lookup)
|
11
|
-
return HashDialler.new(self, *lookup)
|
12
|
-
end
|
13
|
-
|
14
|
-
alias_method :dial, :to_dial
|
15
|
-
|
16
|
-
# Called directly on a Hash, immediately dials and calls the hash keys specified as arguments. Returns the value found, or nil.
|
17
|
-
#
|
18
|
-
# @param lookup The hash keys to attempt to retrieve.
|
19
|
-
#
|
20
|
-
def call(*lookup)
|
21
|
-
return HashDialler.new(self, *lookup).call
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
# Extend core class so that hash.dial can be called
|
27
|
-
class Hash
|
28
|
-
include HashDial
|
29
|
-
end
|
1
|
+
require "hash_dial/version"
|
2
|
+
require "hash_dial/hash_dialler"
|
3
|
+
|
4
|
+
module HashDial
|
5
|
+
|
6
|
+
# Called on a Hash, returns a HashDialler object for that Hash.
|
7
|
+
#
|
8
|
+
# @param lookup Parameters to this method form initial hash keys to dial. This is unnecessary but works anyway. For simplicity, dial hash keys by accessing them as if HashDialler were a Hash.
|
9
|
+
#
|
10
|
+
def to_dial(*lookup)
|
11
|
+
return HashDialler.new(self, *lookup)
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :dial, :to_dial
|
15
|
+
|
16
|
+
# Called directly on a Hash, immediately dials and calls the hash keys specified as arguments. Returns the value found, or nil.
|
17
|
+
#
|
18
|
+
# @param lookup The hash keys to attempt to retrieve.
|
19
|
+
#
|
20
|
+
def call(*lookup)
|
21
|
+
return HashDialler.new(self, *lookup).call
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
# Extend core class so that hash.dial can be called
|
27
|
+
class Hash
|
28
|
+
include HashDial
|
29
|
+
end
|
30
|
+
|
31
|
+
warn "[DEPRECATION] HashDial has been deprecated and replaced by KeyDial. Please require 'key_dial' instead (this will not break any code)."
|
@@ -1,76 +1,76 @@
|
|
1
|
-
module HashDial
|
2
|
-
|
3
|
-
class HashDialler
|
4
|
-
|
5
|
-
@hash
|
6
|
-
@lookup
|
7
|
-
@default = nil
|
8
|
-
|
9
|
-
def initialize(hash, *lookup)
|
10
|
-
if hash.is_a?(Hash)
|
11
|
-
@hash = hash
|
12
|
-
else
|
13
|
-
@hash = {}
|
14
|
-
end
|
15
|
-
@lookup = []
|
16
|
-
if lookup.length > 0
|
17
|
-
dial!(*lookup)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Adds a hash key to the list of nested keys to try, one level deeper.
|
22
|
-
#
|
23
|
-
# @param keys The key to add. Multiple arguments would add multiple keys.
|
24
|
-
#
|
25
|
-
def dial!(*keys)
|
26
|
-
#unless key.is_a(Symbol) || key.is_a(String)
|
27
|
-
@lookup += keys
|
28
|
-
return self
|
29
|
-
end
|
30
|
-
|
31
|
-
# Digs into the hash to the list of keys specified by dialling. Returns nil or default if specified.
|
32
|
-
#
|
33
|
-
# @param default What to return if no key is found.
|
34
|
-
#
|
35
|
-
def call(default = nil)
|
36
|
-
begin
|
37
|
-
value = @hash.dig(*@lookup)
|
38
|
-
rescue
|
39
|
-
value = default
|
40
|
-
end
|
41
|
-
return value
|
42
|
-
end
|
43
|
-
|
44
|
-
# Return the original hash object.
|
45
|
-
def hangup
|
46
|
-
return @hash
|
47
|
-
end
|
48
|
-
|
49
|
-
# Remove keys from the dialling list.
|
50
|
-
#
|
51
|
-
# @param keys If specified, these keys would be removed from wherever they appear in the dialling list. Otherwise, the last added key is removed.
|
52
|
-
#
|
53
|
-
def undial!(*keys)
|
54
|
-
if keys.length > 0
|
55
|
-
@lookup -= keys
|
56
|
-
elsif @lookup.length > 0
|
57
|
-
@lookup.pop
|
58
|
-
end
|
59
|
-
return self
|
60
|
-
end
|
61
|
-
|
62
|
-
# The preferred way to build up your dialling list. Access HashDialler as if it were a Hash, e.g. hash[a][b][c]. This does not actually return any value, rather it dials those keys (awaiting a call).
|
63
|
-
#
|
64
|
-
def [](key)
|
65
|
-
return dial!(key)
|
66
|
-
end
|
67
|
-
def +(key)
|
68
|
-
return dial!(key)
|
69
|
-
end
|
70
|
-
def -(key)
|
71
|
-
return undial!(key)
|
72
|
-
end
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
1
|
+
module HashDial
|
2
|
+
|
3
|
+
class HashDialler
|
4
|
+
|
5
|
+
@hash
|
6
|
+
@lookup
|
7
|
+
@default = nil
|
8
|
+
|
9
|
+
def initialize(hash, *lookup)
|
10
|
+
if hash.is_a?(Hash)
|
11
|
+
@hash = hash
|
12
|
+
else
|
13
|
+
@hash = {}
|
14
|
+
end
|
15
|
+
@lookup = []
|
16
|
+
if lookup.length > 0
|
17
|
+
dial!(*lookup)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Adds a hash key to the list of nested keys to try, one level deeper.
|
22
|
+
#
|
23
|
+
# @param keys The key to add. Multiple arguments would add multiple keys.
|
24
|
+
#
|
25
|
+
def dial!(*keys)
|
26
|
+
#unless key.is_a(Symbol) || key.is_a(String)
|
27
|
+
@lookup += keys
|
28
|
+
return self
|
29
|
+
end
|
30
|
+
|
31
|
+
# Digs into the hash to the list of keys specified by dialling. Returns nil or default if specified.
|
32
|
+
#
|
33
|
+
# @param default What to return if no key is found.
|
34
|
+
#
|
35
|
+
def call(default = nil)
|
36
|
+
begin
|
37
|
+
value = @hash.dig(*@lookup)
|
38
|
+
rescue
|
39
|
+
value = default
|
40
|
+
end
|
41
|
+
return value
|
42
|
+
end
|
43
|
+
|
44
|
+
# Return the original hash object.
|
45
|
+
def hangup
|
46
|
+
return @hash
|
47
|
+
end
|
48
|
+
|
49
|
+
# Remove keys from the dialling list.
|
50
|
+
#
|
51
|
+
# @param keys If specified, these keys would be removed from wherever they appear in the dialling list. Otherwise, the last added key is removed.
|
52
|
+
#
|
53
|
+
def undial!(*keys)
|
54
|
+
if keys.length > 0
|
55
|
+
@lookup -= keys
|
56
|
+
elsif @lookup.length > 0
|
57
|
+
@lookup.pop
|
58
|
+
end
|
59
|
+
return self
|
60
|
+
end
|
61
|
+
|
62
|
+
# The preferred way to build up your dialling list. Access HashDialler as if it were a Hash, e.g. hash[a][b][c]. This does not actually return any value, rather it dials those keys (awaiting a call).
|
63
|
+
#
|
64
|
+
def [](key)
|
65
|
+
return dial!(key)
|
66
|
+
end
|
67
|
+
def +(key)
|
68
|
+
return dial!(key)
|
69
|
+
end
|
70
|
+
def -(key)
|
71
|
+
return undial!(key)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/hash_dial/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_dial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Convincible
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,11 +52,10 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
55
|
+
description: Avoid all errors when accessing (deeply nested) Hash keys. Safer than
|
56
56
|
dig(), as will quietly return nil (or your default) if the keys requested are invalid
|
57
|
-
for any reason at all.
|
58
|
-
|
59
|
-
just surround this with ''.dial'' and ''.call''.'
|
57
|
+
for any reason at all. This gem is deprecated - use KeyDial instead, which does
|
58
|
+
the same thing but works on Hashes, Arrays and Structs as well.
|
60
59
|
email:
|
61
60
|
- development@convincible.media
|
62
61
|
executables: []
|
@@ -79,13 +78,13 @@ files:
|
|
79
78
|
homepage: https://github.com/ConvincibleMedia/ruby-gem-hash_dial
|
80
79
|
licenses: []
|
81
80
|
metadata: {}
|
82
|
-
post_install_message:
|
81
|
+
post_install_message: The 'hash_dial' gem has been deprecated and replaced by 'key_dial'.
|
83
82
|
rdoc_options: []
|
84
83
|
require_paths:
|
85
84
|
- lib
|
86
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
86
|
requirements:
|
88
|
-
- - "
|
87
|
+
- - ">="
|
89
88
|
- !ruby/object:Gem::Version
|
90
89
|
version: '2.3'
|
91
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -98,5 +97,5 @@ rubygems_version: 3.0.0
|
|
98
97
|
signing_key:
|
99
98
|
specification_version: 4
|
100
99
|
summary: Access (deeply nested) hash keys. Get the value, or nil on any error. (Even
|
101
|
-
safer than Hash::dig).
|
100
|
+
safer than Hash::dig). Deprecated - use KeyDial.
|
102
101
|
test_files: []
|