trick_bag 0.50.0 → 0.51.0

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
  SHA1:
3
- metadata.gz: 18fca0df6bf55045308d1addceafea1793c14b87
4
- data.tar.gz: 37c89c74b67277d29d959d10a0d17636c6347269
3
+ metadata.gz: 137cb6576cfd3498b7762313918781e053f12550
4
+ data.tar.gz: bf7a7e5a71cae9c08a7f8bde90801e422fe63d0a
5
5
  SHA512:
6
- metadata.gz: fa4fa5ddde0bca6c34ef27af74122b1ae1b669597b135f674b0f8e3dfac812a0d69576cea742ca2fe27ef5473cc1895ff5f046f1da0c36ec7d1104cfb2e5365c
7
- data.tar.gz: 377ec62488029e12b522af9e8cc0ec7ee49bdd54028cc85c916b9d4da5c3f7b10ad99ef7e67714c3e17217401a8f6476d7213b056f6e9886f8ab74bd542ac88f
6
+ metadata.gz: 137370f1acfd51c9378894c6328852013c22c3cb522cd1d93033350cd3145197d4b75c7288f7712af42d21411d92300459eb7a01675f9d4b2d5bb678a8bf9684
7
+ data.tar.gz: e097d5e75037f2219d8f76c8172afd31ae81a734bbf84dc9860c42b1884a671508c7b73d4b7d4688f7eb02698c5530b4f9a68f12cbbc805a787c5eed51d7f190
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.51.0
2
+
3
+ * Add CollectionAccess access and accessor methods.
4
+
5
+
1
6
  ## v0.50.0
2
7
 
3
8
  * On MultiCounter, fixed percent_of_total_hash, added fraction_of_total_hash.
@@ -0,0 +1,59 @@
1
+ # Supports access to nested hashes with a string instead of multiple []'s.
2
+ # Inspired by Josh Szmajda's dot_notation gem at https://github.com/joshsz/dot_notation,
3
+
4
+ module TrickBag
5
+ module CollectionAccess
6
+
7
+ module_function
8
+
9
+ # Accesses a collection with a single string that represents n levels of depth.
10
+ # See the spec file for examples, but here's one:
11
+ #
12
+ # h = { 'h' => ['a', 'b'] }
13
+ # CollectionAccess.access(h, 'h.1') => 'b'
14
+ #
15
+ # If an error is raised while trying to access the value with a given key,
16
+ # an error will be raised showing that error plus the context of which
17
+ # key failed, as in:
18
+ #
19
+ # Error occurred processing key [x.1] in [x.1.2]: undefined method `[]' for nil:NilClass
20
+ #
21
+ # @param the collection to access
22
+ # @param key_string the string representing the keys to use
23
+ # @separator the string to use to separate the
24
+ def access(collection, key_string, separator = '.')
25
+
26
+ keys = key_string.split(separator)
27
+ return_object = collection
28
+
29
+ keys.each_with_index do |key, index|
30
+ key = key.to_i if return_object.kind_of?(Array)
31
+ begin
32
+ return_object = return_object[key]
33
+ rescue => e
34
+ this_key = keys[0..index].join(separator)
35
+ raise "Error occurred processing key [#{this_key}] in [#{key_string}]: #{e}"
36
+ end
37
+ end
38
+ return_object
39
+ end
40
+
41
+
42
+ # Like access, but returns a lambda that can be used to access a given collection.
43
+ # Since lambdas can be called with the subscript operator,
44
+ # using it can resemble regular hash or array access.
45
+ # If you don't like this you can use '.call' or '.()' instead.
46
+ #
47
+ # An example:
48
+ #
49
+ # h = { 'h' => ['a', 'b'] }
50
+ # accessor = CollectionAccess.accessor(h)
51
+ # accessor['h.1'] # => 'b'
52
+ # or
53
+ # accessor.('h.1') # => 'b'
54
+ def accessor(collection, separator = '.')
55
+ ->(key_string) { access(collection, key_string, separator) }
56
+ end
57
+
58
+ end
59
+ end
@@ -41,7 +41,7 @@ class BufferedEnumerable
41
41
  # @param chunk_size the maximum number of objects to be buffered
42
42
  # @param fetcher lambda to be called to fetch to fill the buffer
43
43
  # @param fetch_notifier lambda to be called to when a fetch is done
44
- def self.create_with_lambdas(chunk_size, fetcher, fetch_notifier)
44
+ def self.create_with_lambdas(chunk_size, fetcher, fetch_notifier = nil)
45
45
  instance = self.new(chunk_size)
46
46
  instance.fetcher = fetcher
47
47
  instance.fetch_notifier = fetch_notifier
@@ -1,3 +1,3 @@
1
1
  module TrickBag
2
- VERSION = "0.50.0"
2
+ VERSION = "0.51.0"
3
3
  end
@@ -0,0 +1,71 @@
1
+ require_relative '../../spec_helper'
2
+ require 'trick_bag/collections/collection_access'
3
+
4
+
5
+ module TrickBag
6
+ module CollectionAccess
7
+
8
+
9
+ describe CollectionAccess do
10
+
11
+ context '#access' do
12
+
13
+ it 'works with a single key' do
14
+ h = { 'a' => 123 }
15
+ expect(CollectionAccess.access(h, 'a')).to eq(123)
16
+ end
17
+
18
+ it 'works with 2 keys' do
19
+ h = { 'a' => { 'b' => 234 }}
20
+ expect(CollectionAccess.access(h, 'a.b')).to eq(234)
21
+ end
22
+
23
+ it 'works with 3 keys' do
24
+ h = { 'a' => { 'bb' => { 'ccc' => 345 }}}
25
+ expect(CollectionAccess.access(h, 'a.bb.ccc')).to eq(345)
26
+ end
27
+
28
+ it 'works with spaces as separators' do
29
+ h = { 'a' => { 'bb' => { 'ccc' => 345 }}}
30
+ expect(CollectionAccess.access(h, 'a bb ccc', ' ')).to eq(345)
31
+
32
+ end
33
+
34
+ it 'works with spaces as separators with multiple spaces' do
35
+ h = { 'a' => { 'bb' => { 'ccc' => 345 }}}
36
+ expect(CollectionAccess.access(h, 'a bb ccc', ' ')).to eq(345)
37
+ end
38
+
39
+ it 'works with numeric array subscripts 1 deep' do
40
+ a = ['a', 'b']
41
+ expect(CollectionAccess.access(a, '1')).to eq('b')
42
+ end
43
+
44
+ it 'works with a hash containing an array' do
45
+ h = { 'h' => ['a', 'b'] }
46
+ expect(CollectionAccess.access(h, 'h.1')).to eq('b')
47
+ end
48
+
49
+ it 'raises an error when accessing an invalid key' do
50
+ h = { 'h' => ['a', 'b'] }
51
+ expect(-> { CollectionAccess.access(h, 'x.1.2') }).to raise_error
52
+ end
53
+ end
54
+
55
+
56
+ context '#accessor' do
57
+
58
+ it 'works with a hash containing an array' do
59
+ h = { 'h' => ['a', 'b'] }
60
+ accessor = CollectionAccess.accessor(h)
61
+ expect(accessor['h.1']).to eq('b')
62
+ end
63
+
64
+ end
65
+
66
+
67
+ end
68
+
69
+
70
+ end
71
+ end
@@ -28,12 +28,18 @@ module Enumerables
28
28
  end
29
29
 
30
30
  e = BufferedEnumerable.create_with_lambdas(4, fetcher, fetch_notifier).to_enum
31
- (1..10).each do |n|
32
- expect(e.next).to eq(n)
33
- end
31
+ (1..10).each { |n| expect(e.next).to eq(n) }
34
32
  expect(chunk_fetch_calls).to eq(3)
35
33
  expect(object_count).to eq(12)
36
34
  end
35
+
36
+ specify 'create_with_lambdas can be called without specifying a fetch_notifier' do
37
+ be = nil
38
+ f1 = -> { be = BufferedEnumerable.create_with_lambdas(4, fetcher).to_enum }
39
+ expect(f1).not_to raise_error
40
+ f2 = -> { (1..10).each { be.next } }
41
+ expect(f2).not_to raise_error
42
+ end
37
43
  end
38
44
 
39
45
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trick_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.50.0
4
+ version: 0.51.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Bennett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-25 00:00:00.000000000 Z
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: os
@@ -95,6 +95,7 @@ files:
95
95
  - RELEASE_NOTES.md
96
96
  - Rakefile
97
97
  - lib/trick_bag.rb
98
+ - lib/trick_bag/collections/collection_access.rb
98
99
  - lib/trick_bag/collections/linked_list.rb
99
100
  - lib/trick_bag/core_types.rb
100
101
  - lib/trick_bag/enumerables/buffered_enumerable.rb
@@ -120,6 +121,7 @@ files:
120
121
  - lib/trick_bag/validations/other_validations.rb
121
122
  - lib/trick_bag/version.rb
122
123
  - spec/spec_helper.rb
124
+ - spec/trick_bag/collections/collection_access_spec.rb
123
125
  - spec/trick_bag/collections/linked_list_spec.rb
124
126
  - spec/trick_bag/core_types_spec.rb
125
127
  - spec/trick_bag/enumerables/buffered_enumerable_spec.rb
@@ -169,6 +171,7 @@ specification_version: 4
169
171
  summary: Miscellaneous general useful tools.
170
172
  test_files:
171
173
  - spec/spec_helper.rb
174
+ - spec/trick_bag/collections/collection_access_spec.rb
172
175
  - spec/trick_bag/collections/linked_list_spec.rb
173
176
  - spec/trick_bag/core_types_spec.rb
174
177
  - spec/trick_bag/enumerables/buffered_enumerable_spec.rb