hash_dial 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a2e69ddcb5f84bef6fa164953fa3a127396568cd91ee1debbad926902e29df3
4
- data.tar.gz: efa91b61097992c4b3a57545d9cb64af8cd4a9fa924277e2c5f1af60f967e1bb
3
+ metadata.gz: 9534eaddcb86bd7312266b1e8f9e4f3b820907156ad51b8dc217e7a6bd0aa4bb
4
+ data.tar.gz: b1655d5175679674f0c0771da15a23cca7ad57531557dff5992d109d8549f238
5
5
  SHA512:
6
- metadata.gz: b776b72153e96ff264e3c859cb1a12b37a06d8baa3538735e34dcffb8331780597ed119cc032c11ccdb70379f45f888ec497b09c417aeab794cc5917c29660ae
7
- data.tar.gz: 4c38e006c1140bb59afc72e2fadb347e40887c49e5b7274d59e2b9cef1c33e54fde80ff475944ed9bb541ddc296ccad9a0ff61a6d86b413993e1ef03dded41a9
6
+ metadata.gz: 85de5edb9b862b2b7f316cd503153008d35d112df936a95edd8c11bc67f462084d421b9c32e73968cb6db706ae49f69937465885e7d7f67ed0ae6959fef695d6
7
+ data.tar.gz: 8668b5f99eea13ae09b8a5a702698a662cc92eb56934bf8e4c586485683c775f5ad4cfe9c852314c6a2d9c34343dcb402d10f054f268e7d3d4261460453179b0
data/.gitignore CHANGED
@@ -1,17 +1,17 @@
1
- .git
2
- .env
3
- *.lnk
4
- *.db
5
- *.ini
6
-
7
- /.bundle/
8
- /.yardoc
9
- /_yardoc/
10
- /coverage/
11
- /doc/
12
- /pkg/
13
- /spec/reports/
14
- /tmp/
15
-
16
- # rspec failure tracking
17
- .rspec_status
1
+ .git
2
+ .env
3
+ *.lnk
4
+ *.db
5
+ *.ini
6
+
7
+ /.bundle/
8
+ /.yardoc
9
+ /_yardoc/
10
+ /coverage/
11
+ /doc/
12
+ /pkg/
13
+ /spec/reports/
14
+ /tmp/
15
+
16
+ # rspec failure tracking
17
+ .rspec_status
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # HashDial
1
+ # HashDial (Ruby Gem)
2
2
 
3
- **Avoid all errors when accessing a deeply nested Hash key.** HashDial goes one step beyond Hash::dig() by returning nil (or your default) if the keys requested are invalid for any reason.
3
+ **Avoid all errors when accessing a deeply nested Hash key.** HashDial goes one step beyond `Hash::dig()` by returning `nil` (or your default) if the keys requested are invalid for any reason.
4
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.
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
6
 
7
7
  ```ruby
8
8
  hash = {a: {b: {c: true}, d: 5}}
@@ -14,7 +14,8 @@ hash.call(:a, :b, :c) #=> true
14
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
15
 
16
16
  ```ruby
17
- hash[:a][:d][:c] #=> TypeError: no implicit conversion of Symbol into Integer
17
+ hash[:a][:d][:c] #=> TypeError: no implicit conversion of Symbol into Integer
18
+
18
19
  #hash → [:a][:d][:c]
19
20
  # ↓ ↓
20
21
  hash.dial[:a][:d][:c].call #=> nil
@@ -26,22 +27,6 @@ We use the concept of placing a phone-call: you can 'dial' any set of keys regar
26
27
 
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'.
28
29
 
29
- ## Installation
30
-
31
- Add this line to your application's Gemfile:
32
-
33
- ```ruby
34
- gem 'hash_dial'
35
- ```
36
-
37
- And then execute:
38
-
39
- $ bundle
40
-
41
- Or install it yourself as:
42
-
43
- $ gem install hash_dial
44
-
45
30
  ## Usage
46
31
 
47
32
  ```ruby
@@ -53,14 +38,14 @@ require 'hash_dial'
53
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.
54
39
 
55
40
  ```ruby
56
- hash.call(:a, :b, :c) #=> Returns the value at hash[:a][:b][:c] or nil
41
+ hash.call(:a, :b, :c) # Returns the value at hash[:a][:b][:c] or nil
57
42
  ```
58
43
 
59
44
  ### Use it like a Hash -- allows default return value
60
45
 
61
46
  ```ruby
62
- hash.dial[:a][:b][:c].call #=> Returns the value at hash[:a][:b][:c] or nil
63
- hash.dial[:a][:b][:c].call('Ooops') #=> Returns the value at hash[:a][:b][:c] or 'Ooops'
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'
64
49
  ```
65
50
 
66
51
  If you don't do this all in one line, you can access the HashDialler object should you want to manipulate it:
@@ -74,6 +59,6 @@ dialler[:c][:d] # Adds two more keys (returns self)
74
59
  dialler += :e # Adds yet one more (returns self)
75
60
  dialler -= :a # Removes all such keys from the list (returns self)
76
61
  # So far we have dialled [:b][:c][:d][:e]
77
- dialler.call #=> Returns the value at hash[:b][:c][:d][:e] or nil
78
- dialler.hangup #=> Returns the original hash by reference
62
+ dialler.call # Returns the value at hash[:b][:c][:d][:e] or nil
63
+ dialler.hangup # Returns the original hash by reference
79
64
  ```
@@ -4,25 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "hash_dial/version"
5
5
 
6
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"]
7
+ spec.name = "hash_dial"
8
+ spec.version = HashDial::VERSION
9
+ spec.authors = ["Convincible"]
10
+ spec.email = ["development@convincible.media"]
11
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 a (deeply nested) Hash key. HashDial goes one step beyond Hash::dig() by returning nil (or your default) if the keys requested are invalid for any reason at all. 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'."
14
- spec.homepage = "https://github.com/ConvincibleMedia/hash_dial"
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. Bonus: you don't even need to fiddle with existing code. If you have already written something to access a deep hash key (e.g. hash[:a][:b][:c]), just surround this with '.dial' and '.call'."
14
+ spec.homepage = "https://github.com/ConvincibleMedia/ruby-gem-hash_dial"
15
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"]
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'
24
25
 
25
- spec.add_development_dependency "bundler", "~> 1.17"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
28
29
  end
@@ -3,18 +3,27 @@ require "hash_dial/hash_dialler"
3
3
 
4
4
  module HashDial
5
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
+ #
6
10
  def to_dial(*lookup)
7
11
  return HashDialler.new(self, *lookup)
8
12
  end
9
13
 
10
14
  alias_method :dial, :to_dial
11
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
+ #
12
20
  def call(*lookup)
13
21
  return HashDialler.new(self, *lookup).call
14
22
  end
15
23
 
16
24
  end
17
25
 
26
+ # Extend core class so that hash.dial can be called
18
27
  class Hash
19
28
  include HashDial
20
29
  end
@@ -1,61 +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
- def dial!(*keys)
22
- #unless key.is_a(Symbol) || key.is_a(String)
23
- @lookup += keys
24
- return self
25
- end
26
-
27
- def call(default = nil)
28
- begin
29
- value = @hash.dig(*@lookup)
30
- rescue
31
- value = default
32
- end
33
- return value
34
- end
35
-
36
- def hangup
37
- return @hash
38
- end
39
-
40
- def undial!(*keys)
41
- if keys.length > 0
42
- @lookup -= keys
43
- elsif @lookup.length > 0
44
- @lookup.pop
45
- end
46
- return self
47
- end
48
-
49
- def [](key)
50
- return dial!(key)
51
- end
52
- def +(key)
53
- return dial!(key)
54
- end
55
- def -(key)
56
- return undial!(key)
57
- end
58
-
59
- end
60
-
61
- 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
@@ -1,3 +1,3 @@
1
1
  module HashDial
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
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.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Convincible
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-20 00:00:00.000000000 Z
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,11 +52,11 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: 'Avoid all errors when accessing a (deeply nested) Hash key. HashDial
56
- goes one step beyond Hash::dig() by returning nil (or your default) if the keys
57
- requested are invalid for any reason at all. Bonus: you don''t even need to fiddle
58
- with existing code. If you have already written something to access a deep hash
59
- key, just surround this with ''.dial'' and ''.call''.'
55
+ description: 'Avoid all errors when accessing (deeply nested) Hash keys. Safer than
56
+ dig(), as will quietly return nil (or your default) if the keys requested are invalid
57
+ for any reason at all. Bonus: you don''t even need to fiddle with existing code.
58
+ If you have already written something to access a deep hash key (e.g. hash[:a][:b][:c]),
59
+ just surround this with ''.dial'' and ''.call''.'
60
60
  email:
61
61
  - development@convincible.media
62
62
  executables: []
@@ -76,7 +76,7 @@ files:
76
76
  - lib/hash_dial.rb
77
77
  - lib/hash_dial/hash_dialler.rb
78
78
  - lib/hash_dial/version.rb
79
- homepage: https://github.com/ConvincibleMedia/hash_dial
79
+ homepage: https://github.com/ConvincibleMedia/ruby-gem-hash_dial
80
80
  licenses: []
81
81
  metadata: {}
82
82
  post_install_message:
@@ -85,9 +85,9 @@ require_paths:
85
85
  - lib
86
86
  required_ruby_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ">="
88
+ - - "~>"
89
89
  - !ruby/object:Gem::Version
90
- version: '0'
90
+ version: '2.3'
91
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - ">="