ruby_peter_v 0.0.2 → 0.0.3

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/HISTORY.txt CHANGED
@@ -5,3 +5,7 @@
5
5
  0.0.2 (2013-01-03)
6
6
 
7
7
  * #single raises exception for nil (similar to #first)
8
+
9
+ 0.0.3 (2013-05-22)
10
+
11
+ * Object#max_with_nil added
data/README.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- * .single on enumerable (instead of unstable .first)
21
+ * #single on enumerable (instead of unstable .first)
22
22
 
23
23
  The background of this is that in many cases,
24
24
  the developer knows there _should_ only be 1
@@ -27,6 +27,13 @@ Or install it yourself as:
27
27
  will happily choose a random entry (certainly with
28
28
  ActiveRecord first) which is a silent bug.
29
29
 
30
+ * #max_with_nil on Object
31
+
32
+ Finding the max of two arguments, but if one of them
33
+ is nil, take the other one, and both nil, return nil.
34
+ This is similar to the behavior ios nil would be considered
35
+ smaller than all other objects.
36
+
30
37
  ## Contributing
31
38
 
32
39
  1. Fork it
@@ -0,0 +1,15 @@
1
+ class Object
2
+
3
+ def max_with_nil(a,b)
4
+ if b.nil?
5
+ a
6
+ else
7
+ if a.nil?
8
+ b
9
+ else
10
+ (b > a) ? b : a
11
+ end
12
+ end
13
+ end
14
+
15
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyPeterV
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/ruby_peter_v.rb CHANGED
@@ -1,2 +1,3 @@
1
- require "ruby_peter_v/version"
2
- require "ruby_peter_v/single"
1
+ require 'ruby_peter_v/version'
2
+ require 'ruby_peter_v/single'
3
+ require 'ruby_peter_v/max_with_nil'
@@ -0,0 +1,29 @@
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
data/spec/single_spec.rb CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "single" do
4
4
  it "raises error on nil value (nil.first also raises error)" do
5
5
  a = nil
6
- lambda {a.single}.should raise_error
6
+ lambda { a.single } . should raise_error NoMethodError
7
7
  end
8
8
 
9
9
  it "nil for empty set" do
@@ -18,6 +18,6 @@ describe "single" do
18
18
 
19
19
  it "exception for > 1 element in set" do
20
20
  a = [:a, :b]
21
- lambda{a.single}.should raise_exception
21
+ lambda { a.single } . should raise_exception RuntimeError
22
22
  end
23
23
  end
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-03 00:00:00.000000000 Z
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -42,9 +42,11 @@ files:
42
42
  - README.md
43
43
  - Rakefile
44
44
  - lib/ruby_peter_v.rb
45
+ - lib/ruby_peter_v/max_with_nil.rb
45
46
  - lib/ruby_peter_v/single.rb
46
47
  - lib/ruby_peter_v/version.rb
47
48
  - ruby_peter_v.gemspec
49
+ - spec/max_with_nil_spec.rb
48
50
  - spec/single_spec.rb
49
51
  - spec/spec_helper.rb
50
52
  homepage: https://github.com/petervandenabeele/ruby_peter_v
@@ -67,10 +69,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
69
  version: '0'
68
70
  requirements: []
69
71
  rubyforge_project:
70
- rubygems_version: 1.8.24
72
+ rubygems_version: 1.8.25
71
73
  signing_key:
72
74
  specification_version: 3
73
75
  summary: Ruby helpers for @peter_v
74
76
  test_files:
77
+ - spec/max_with_nil_spec.rb
75
78
  - spec/single_spec.rb
76
79
  - spec/spec_helper.rb