hash_pick 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 930eee6958a5e12f3706b0c47b2d8b36e8209a94
4
- data.tar.gz: f3bbcca070e3d83dfba08d4286da1747bde8b6ed
3
+ metadata.gz: 0ab66b9a5a34241d4501d248ac0493ca8254e81c
4
+ data.tar.gz: 3234a0b99d870f14bb7e24c978132d9aef498fce
5
5
  SHA512:
6
- metadata.gz: d9473e4453f82608a60450281e1c4fce27b3e78bed49bef720a30102a9d691a267c69802c3af1cce88eabc0413e3e9ef74721710dae133bb3634e3fe882ae642
7
- data.tar.gz: 5d1e2ad8ef12b33f3ca3aba8d6e33e7fe4802f94e30731899880ba93047ee7da28498ba10f86502251ed303abfcb837941525b7b9e5e44b2d6cac3815f96008a
6
+ metadata.gz: '059c479e8ddb54cda612938a2016df22b831b09b987305eb6a8c01afd8c0b9df251d88736b1021b3b5ef3c004052f9dc2f5c65f679690afdbdd79b2f2596bbd1'
7
+ data.tar.gz: 3b8eaddc6159f30cc3cf3653b62ca2a9d34e7caf3f99613a41b3d612c27d45cb945cf7ea2d8fdd8a5aefc495f42498eca96cdb77b2822024717fb0aa53a9dfbf
data/README.md CHANGED
@@ -6,7 +6,7 @@ A module of utility methods for hash path queries.
6
6
 
7
7
  ## Documentation
8
8
 
9
- For documentation of the released gem, see [rubydoc.info](http://www.rubydoc.info/gems/hash_pick).
9
+ For documentation, follow the _Documentation_ link of the released gem on [rubygems.org](https://rubygems.org/gems/hash_pick).
10
10
 
11
11
  ## Installation
12
12
 
@@ -1,3 +1,3 @@
1
1
  module HashPick
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/hash_pick.rb CHANGED
@@ -3,8 +3,8 @@
3
3
  ##
4
4
  # Fetch a value from a nested dictionary
5
5
  #
6
- # Provides methods for fetching a value from a nested dictionary (an object that implements +include?+ and +fetch+),
7
- # using a key path expressed as a list (an object that implements +inject+).
6
+ # Provides methods for fetching a value from a nested dictionary (an object structure whose root and branch nodes implement +[]+),
7
+ # using a key path expressed as a list (an +Enumerable+ object whose +each+ method yields a path key).
8
8
  #
9
9
  # The key path is iterated. In each iteration, the key is looked up in the dictionary, and the value found is used
10
10
  # as the dictionary for the next iteration. Lookup failure immediately returns nil.
@@ -53,13 +53,7 @@ module HashPick
53
53
  assert_non_nil_path_keys(path)
54
54
 
55
55
  pick(hash, path) do |acc, p|
56
- if acc.include?(p.to_sym)
57
- acc.fetch(p.to_sym)
58
- elsif acc.include?(p.to_s)
59
- acc[p.to_s]
60
- else
61
- throw :break
62
- end
56
+ acc[p.to_sym].nil? ? acc[p.to_s] : acc[p.to_sym]
63
57
  end
64
58
  end
65
59
 
@@ -87,17 +81,11 @@ module HashPick
87
81
  # }
88
82
  # }
89
83
  #
90
- # HashPick.pick(dict, [:sheldon, :first_name]) do |p, k|
91
- # throw :break unless p[:live] and p.include?(k)
92
- # p[k]
93
- # end
84
+ # HashPick.pick(dict, [:sheldon, :first_name]) { |p, k| p[k] if p[:live] }
94
85
  # # => "Sheldon"
95
86
  #
96
- # HashPick.pick(dict, [:charles, :first_name]) do |p, k|
97
- # throw :break unless p[:live] and p.include?(k)
98
- # p[k]
99
- # end
100
- # # => "Hearn"
87
+ # HashPick.pick(dict, [:charles, :first_name]) { |p, k| p[k] if p[:live] }
88
+ # # => nil
101
89
  #
102
90
  # @param [Hash] hash
103
91
  # the dictionary to apply the +path+ to.
@@ -129,7 +117,7 @@ module HashPick
129
117
  end
130
118
 
131
119
  def dictionary?(hash)
132
- hash.respond_to?(:include?) && hash.respond_to?(:fetch)
120
+ hash.respond_to?(:[])
133
121
  end
134
122
 
135
123
  def assert_non_nil_path_keys(path)
@@ -137,7 +125,7 @@ module HashPick
137
125
  end
138
126
 
139
127
  def assert_enumerable_path(path)
140
- raise ArgumentError.new("path is not enumerable") unless path.respond_to?(:inject)
128
+ raise ArgumentError.new("path is not enumerable") unless path.is_a?(Enumerable)
141
129
  end
142
130
 
143
131
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_pick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sheldon Hearn