ruby_peter_v 0.0.4 → 0.0.5
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.
- data/.rvmrc +1 -1
- data/.travis.yml +10 -0
- data/Guardfile +7 -0
- data/HISTORY.txt +10 -1
- data/README.md +17 -12
- data/lib/ruby_peter_v.rb +1 -0
- data/lib/ruby_peter_v/do_recursively.rb +17 -0
- data/lib/ruby_peter_v/version.rb +1 -1
- data/ruby_peter_v.gemspec +5 -2
- data/spec/lib/ruby_peter_v/do_recursively_spec.rb +70 -0
- data/spec/{max_with_nil_spec.rb → lib/ruby_peter_v/max_with_nil_spec.rb} +0 -0
- data/spec/{set_once_spec.rb → lib/ruby_peter_v/set_once_spec.rb} +0 -0
- data/spec/{single_spec.rb → lib/ruby_peter_v/single_spec.rb} +0 -0
- metadata +63 -10
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use --create
|
1
|
+
rvm use --create 2.0.0@ruby_peter_v
|
data/.travis.yml
ADDED
data/Guardfile
ADDED
data/HISTORY.txt
CHANGED
@@ -10,6 +10,15 @@
|
|
10
10
|
|
11
11
|
* Object#max_with_nil(a, b) added
|
12
12
|
|
13
|
-
0.0.4 (
|
13
|
+
0.0.4 (2013-05-22)
|
14
14
|
|
15
15
|
* Object#set_once(attribute, value) added
|
16
|
+
|
17
|
+
0.0.5 (2013-05-31)
|
18
|
+
|
19
|
+
* Object#do_recursively
|
20
|
+
|
21
|
+
TODO:
|
22
|
+
|
23
|
+
* Object#define_eql
|
24
|
+
* Object#assert_keys_in
|
data/README.md
CHANGED
@@ -2,23 +2,18 @@
|
|
2
2
|
|
3
3
|
Ruby helpers for Peter Vandenabeele
|
4
4
|
|
5
|
+
[](http://badge.fury.io/rb/ruby_peter_v)
|
6
|
+
[](http://travis-ci.org/petervandenabeele/ruby_peter_v)
|
7
|
+
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
8
11
|
|
9
12
|
gem 'ruby_peter_v'
|
10
13
|
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install ruby_peter_v
|
18
|
-
|
19
14
|
## Usage
|
20
15
|
|
21
|
-
|
16
|
+
### single on enumerable (instead of unstable .first)
|
22
17
|
|
23
18
|
The background of this is that in many cases,
|
24
19
|
the developer knows there _should_ only be 1
|
@@ -27,21 +22,31 @@ Or install it yourself as:
|
|
27
22
|
will happily choose a random entry (certainly with
|
28
23
|
ActiveRecord first) which is a silent bug.
|
29
24
|
|
30
|
-
|
25
|
+
### max_with_nil on Object
|
31
26
|
|
32
27
|
Finding the max of two arguments, but if one of them
|
33
28
|
is nil, take the other one, and both nil, return nil.
|
34
29
|
This is similar to the behavior ios nil would be considered
|
35
30
|
smaller than all other objects.
|
36
31
|
|
37
|
-
|
32
|
+
### set_once(attribute, value) on Object
|
38
33
|
|
39
34
|
Setting the instance variable with name attribute (a symbol),
|
40
35
|
once to a value value. This acts as a simple form of an
|
41
36
|
immutable attribute. After initialization of the object, it
|
42
37
|
can still be set once from nil to a specific value, but after
|
43
38
|
that, it can never be changed to a different value. Writing
|
44
|
-
it twice with the same value does not trow an exception.
|
39
|
+
it twice with the same value does not trow an exception.
|
40
|
+
|
41
|
+
### do_recursively(entry_or_collection, &block) on Object
|
42
|
+
|
43
|
+
Call the block on each entry that is given as entry, in a
|
44
|
+
collection, or in a colletion in a collection to unrestricted
|
45
|
+
depth. This implementation only uses each and loops recursively,
|
46
|
+
so it never instantiates an array or actual collection of all
|
47
|
+
objects (this allows streaming, lazy evaluation, e.g. for looping
|
48
|
+
over objects that are read from a file that is much larger than
|
49
|
+
the memory size, needed for Big Data processing).
|
45
50
|
|
46
51
|
## Contributing
|
47
52
|
|
data/lib/ruby_peter_v.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
def do_recursively(entry_or_collection, &block)
|
4
|
+
if entry_or_collection.respond_to?(:each)
|
5
|
+
loop_over_collection(entry_or_collection, &block)
|
6
|
+
else
|
7
|
+
yield(entry_or_collection)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def loop_over_collection(entry_or_collection, &block)
|
12
|
+
entry_or_collection.each do |inner_entry_or_collection|
|
13
|
+
do_recursively(inner_entry_or_collection, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/ruby_peter_v/version.rb
CHANGED
data/ruby_peter_v.gemspec
CHANGED
@@ -14,8 +14,11 @@ Gem::Specification.new do |gem|
|
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
-
gem.test_files = gem.files.grep(%r{^(
|
17
|
+
gem.test_files = gem.files.grep(%r{^(spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_development_dependency 'rspec', '
|
20
|
+
gem.add_development_dependency 'rspec', '>= 2'
|
21
|
+
gem.add_development_dependency 'guard-rspec'
|
22
|
+
gem.add_development_dependency 'rb-fsevent', '~> 0.9'
|
23
|
+
gem.add_development_dependency 'terminal-notifier-guard'
|
21
24
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "do_recursively" do
|
4
|
+
|
5
|
+
let(:a) {Object.new}
|
6
|
+
|
7
|
+
it "does it once for a non collection object" do
|
8
|
+
a.should_receive(:test_it).exactly(1).times
|
9
|
+
do_recursively(a) do |_e|
|
10
|
+
_e.test_it
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does it for each entry in a simple array" do
|
15
|
+
a.should_receive(:test_it).exactly(2).times
|
16
|
+
do_recursively([a,a]) do |_e|
|
17
|
+
_e.test_it
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does it for each entry in a complex combination" do
|
22
|
+
a.should_receive(:test_it).exactly(6).times
|
23
|
+
do_recursively([a,[a,a],[[[a],a],a]]) do |_e|
|
24
|
+
_e.test_it
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# This feature allows streaming
|
29
|
+
describe "works on an enumerable that is NOT an array" do
|
30
|
+
let(:b) do
|
31
|
+
_b = Object.new
|
32
|
+
_b.extend Enumerable
|
33
|
+
_b.instance_eval do
|
34
|
+
def each
|
35
|
+
yield "test"
|
36
|
+
yield "test"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
_b
|
40
|
+
end
|
41
|
+
|
42
|
+
it "call the each block 2 times" do
|
43
|
+
a.should_receive(:test_it).with("test").exactly(2).times
|
44
|
+
do_recursively(b) do |_e|
|
45
|
+
a.test_it(_e)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it "works with lazy in Ruby 2.0" do
|
50
|
+
pending("only Ruby >= 2.0") unless RUBY_VERSION.split('.').first.to_i >= 2
|
51
|
+
a.should_receive(:test_it).with("test").exactly(2).times
|
52
|
+
do_recursively(b.lazy) do |_e|
|
53
|
+
a.test_it(_e)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "does not call to_a on the collection" do
|
58
|
+
b.instance_eval do
|
59
|
+
def to_a
|
60
|
+
raise "Should not call to_a (no instantiation of the stream)"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
do_recursively(b) do |_e|
|
65
|
+
_e
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_peter_v
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,14 +9,14 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- - ! '
|
19
|
+
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '2'
|
22
22
|
type: :development
|
@@ -24,9 +24,57 @@ dependencies:
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- - ! '
|
27
|
+
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-fsevent
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.9'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: terminal-notifier-guard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
30
78
|
description: Ruby helpers for @peter_v
|
31
79
|
email:
|
32
80
|
- peter@vandenabeele.com
|
@@ -36,20 +84,24 @@ extra_rdoc_files: []
|
|
36
84
|
files:
|
37
85
|
- .gitignore
|
38
86
|
- .rvmrc
|
87
|
+
- .travis.yml
|
39
88
|
- Gemfile
|
89
|
+
- Guardfile
|
40
90
|
- HISTORY.txt
|
41
91
|
- LICENSE.txt
|
42
92
|
- README.md
|
43
93
|
- Rakefile
|
44
94
|
- lib/ruby_peter_v.rb
|
95
|
+
- lib/ruby_peter_v/do_recursively.rb
|
45
96
|
- lib/ruby_peter_v/max_with_nil.rb
|
46
97
|
- lib/ruby_peter_v/set_once.rb
|
47
98
|
- lib/ruby_peter_v/single.rb
|
48
99
|
- lib/ruby_peter_v/version.rb
|
49
100
|
- ruby_peter_v.gemspec
|
50
|
-
- spec/
|
51
|
-
- spec/
|
52
|
-
- spec/
|
101
|
+
- spec/lib/ruby_peter_v/do_recursively_spec.rb
|
102
|
+
- spec/lib/ruby_peter_v/max_with_nil_spec.rb
|
103
|
+
- spec/lib/ruby_peter_v/set_once_spec.rb
|
104
|
+
- spec/lib/ruby_peter_v/single_spec.rb
|
53
105
|
- spec/spec_helper.rb
|
54
106
|
homepage: https://github.com/petervandenabeele/ruby_peter_v
|
55
107
|
licenses: []
|
@@ -76,7 +128,8 @@ signing_key:
|
|
76
128
|
specification_version: 3
|
77
129
|
summary: Ruby helpers for @peter_v
|
78
130
|
test_files:
|
79
|
-
- spec/
|
80
|
-
- spec/
|
81
|
-
- spec/
|
131
|
+
- spec/lib/ruby_peter_v/do_recursively_spec.rb
|
132
|
+
- spec/lib/ruby_peter_v/max_with_nil_spec.rb
|
133
|
+
- spec/lib/ruby_peter_v/set_once_spec.rb
|
134
|
+
- spec/lib/ruby_peter_v/single_spec.rb
|
82
135
|
- spec/spec_helper.rb
|