ruby_peter_v 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.txt +5 -0
- data/README.md +74 -10
- data/lib/ruby_peter_v/version.rb +1 -1
- data/lib/ruby_peter_v.rb +0 -1
- metadata +1 -4
- data/lib/ruby_peter_v/max_with_nil.rb +0 -15
- data/spec/lib/ruby_peter_v/max_with_nil_spec.rb +0 -29
data/HISTORY.txt
CHANGED
data/README.md
CHANGED
@@ -18,16 +18,25 @@ Add this line to your application's Gemfile:
|
|
18
18
|
The background of this is that in many cases,
|
19
19
|
the developer knows there _should_ only be 1
|
20
20
|
element in the set. Using first is fine, but if
|
21
|
-
inadvertently more elements are present,
|
21
|
+
inadvertently more elements are present, `.first`
|
22
22
|
will happily choose a random entry (certainly with
|
23
23
|
ActiveRecord first) which is a silent bug.
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
TODO: refactor : use a dedicated exception UniquenessError
|
26
|
+
TODO: refactor : take a block (a "detect" with uniqueness check)
|
27
|
+
|
28
|
+
```
|
29
|
+
$ irb
|
30
|
+
2.0.0-p195 :001 > require 'ruby_peter_v'
|
31
|
+
=> true
|
32
|
+
2.0.0-p195 :002 > [].single
|
33
|
+
=> nil
|
34
|
+
2.0.0-p195 :003 > [1].single
|
35
|
+
=> 1
|
36
|
+
2.0.0-p195 :004 > [1,2].single
|
37
|
+
RuntimeError: INTERNAL ERROR: size of set was 2
|
38
|
+
...
|
39
|
+
```
|
31
40
|
|
32
41
|
### set_once(attribute, value) on Object
|
33
42
|
|
@@ -36,24 +45,79 @@ Add this line to your application's Gemfile:
|
|
36
45
|
immutable attribute. After initialization of the object, it
|
37
46
|
can still be set once from nil to a specific value, but after
|
38
47
|
that, it can never be changed to a different value. Writing
|
39
|
-
it twice with the same value does not
|
48
|
+
it twice with the same value does not throw an exception.
|
49
|
+
|
50
|
+
TODO: refactor to a Module method that also defines the attr_accessor
|
51
|
+
|
52
|
+
```
|
53
|
+
2.0.0-p195 :009 > class A
|
54
|
+
2.0.0-p195 :010?> attr_reader :foo
|
55
|
+
2.0.0-p195 :011?> def foo=(_foo)
|
56
|
+
2.0.0-p195 :012?> set_once(:foo, _foo)
|
57
|
+
2.0.0-p195 :013?> end
|
58
|
+
2.0.0-p195 :014?> end
|
59
|
+
=> nil
|
60
|
+
2.0.0-p195 :015 > a = A.new
|
61
|
+
=> #<A:0x007fa82905f720>
|
62
|
+
2.0.0-p195 :016 > a.foo
|
63
|
+
=> nil
|
64
|
+
2.0.0-p195 :017 > a.foo = 1
|
65
|
+
=> 1
|
66
|
+
2.0.0-p195 :018 > a.foo
|
67
|
+
=> 1
|
68
|
+
2.0.0-p195 :019 > a.foo = 1
|
69
|
+
=> 1
|
70
|
+
2.0.0-p195 :020 > a.foo = 2
|
71
|
+
SetOnceError: Value of foo was 1, trying to set it to 2
|
72
|
+
```
|
40
73
|
|
41
74
|
### do_recursively(entry_or_collection, &block) on Object
|
42
75
|
|
43
76
|
Call the block on each entry that is given as entry, in a
|
44
|
-
collection, or in a
|
77
|
+
collection, or in a collection in a collection to unrestricted
|
45
78
|
depth. This implementation only uses each and loops recursively,
|
46
79
|
so it never instantiates an array or actual collection of all
|
47
80
|
objects (this allows streaming, lazy evaluation, e.g. for looping
|
48
81
|
over objects that are read from a file that is much larger than
|
49
82
|
the memory size, needed for Big Data processing).
|
50
83
|
|
84
|
+
TODO: refactor to "entry_or_collection.each_recursively"
|
85
|
+
|
86
|
+
```
|
87
|
+
2.0.0-p195 :021 > do_recursively([:a, [:b, [:c]]]) { |e| puts e.succ }
|
88
|
+
b
|
89
|
+
c
|
90
|
+
d
|
91
|
+
```
|
92
|
+
|
51
93
|
### DefineEquality(*accessors) on Module
|
52
94
|
|
53
|
-
Defining the equality (
|
95
|
+
Defining the equality (eql?, ==, hash) between instances.
|
54
96
|
The class is included by default in the accessor list in
|
55
97
|
the usage `include DefineEquality(:foo, :bar)`.
|
56
98
|
|
57
99
|
Defining equality for instances of different classes is
|
58
100
|
possible by manually defining the `equality_accessors`
|
59
101
|
function (see the spec with the b5 instance).
|
102
|
+
|
103
|
+
```
|
104
|
+
2.0.0-p195 :022 > class B
|
105
|
+
2.0.0-p195 :023?> include DefineEquality(:foo)
|
106
|
+
2.0.0-p195 :024?> attr_accessor :foo
|
107
|
+
2.0.0-p195 :025?> end
|
108
|
+
=> nil
|
109
|
+
2.0.0-p195 :026 > b1 = B.new
|
110
|
+
=> #<B:0x007fc1488f4ce0>
|
111
|
+
2.0.0-p195 :027 > b1.foo = '1'
|
112
|
+
=> "1"
|
113
|
+
2.0.0-p195 :028 > b2 = B.new
|
114
|
+
=> #<B:0x007fc1488a0028>
|
115
|
+
2.0.0-p195 :029 > b2.foo = '1'
|
116
|
+
=> "1"
|
117
|
+
2.0.0-p195 :030 > b1 == b2
|
118
|
+
=> true
|
119
|
+
2.0.0-p195 :031 > b2.foo = '2'
|
120
|
+
=> "2"
|
121
|
+
2.0.0-p195 :032 > b1 == b2
|
122
|
+
=> false
|
123
|
+
```
|
data/lib/ruby_peter_v/version.rb
CHANGED
data/lib/ruby_peter_v.rb
CHANGED
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.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -94,14 +94,12 @@ files:
|
|
94
94
|
- lib/ruby_peter_v.rb
|
95
95
|
- lib/ruby_peter_v/define_equality.rb
|
96
96
|
- lib/ruby_peter_v/do_recursively.rb
|
97
|
-
- lib/ruby_peter_v/max_with_nil.rb
|
98
97
|
- lib/ruby_peter_v/set_once.rb
|
99
98
|
- lib/ruby_peter_v/single.rb
|
100
99
|
- lib/ruby_peter_v/version.rb
|
101
100
|
- ruby_peter_v.gemspec
|
102
101
|
- spec/lib/ruby_peter_v/define_equality_spec.rb
|
103
102
|
- spec/lib/ruby_peter_v/do_recursively_spec.rb
|
104
|
-
- spec/lib/ruby_peter_v/max_with_nil_spec.rb
|
105
103
|
- spec/lib/ruby_peter_v/set_once_spec.rb
|
106
104
|
- spec/lib/ruby_peter_v/single_spec.rb
|
107
105
|
- spec/spec_helper.rb
|
@@ -132,7 +130,6 @@ summary: Ruby helpers for @peter_v
|
|
132
130
|
test_files:
|
133
131
|
- spec/lib/ruby_peter_v/define_equality_spec.rb
|
134
132
|
- spec/lib/ruby_peter_v/do_recursively_spec.rb
|
135
|
-
- spec/lib/ruby_peter_v/max_with_nil_spec.rb
|
136
133
|
- spec/lib/ruby_peter_v/set_once_spec.rb
|
137
134
|
- spec/lib/ruby_peter_v/single_spec.rb
|
138
135
|
- spec/spec_helper.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "max_with_nil" do
|
4
|
-
it "nil if a and b are nil" do
|
5
|
-
max_with_nil(nil, nil).should == nil
|
6
|
-
end
|
7
|
-
|
8
|
-
it "b if a is nil" do
|
9
|
-
max_with_nil(nil, 10).should == 10
|
10
|
-
end
|
11
|
-
|
12
|
-
it "a if b is nil" do
|
13
|
-
max_with_nil(10, nil).should == 10
|
14
|
-
end
|
15
|
-
|
16
|
-
it "a if a > b" do
|
17
|
-
max_with_nil(10, 5).should == 10
|
18
|
-
end
|
19
|
-
|
20
|
-
it "b if b > a" do
|
21
|
-
max_with_nil(5, 10).should == 10
|
22
|
-
end
|
23
|
-
|
24
|
-
it "a if a == b" do
|
25
|
-
a = "test"
|
26
|
-
b = "test"
|
27
|
-
max_with_nil(a, b).object_id.should == a.object_id
|
28
|
-
end
|
29
|
-
end
|