object-let 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +25 -2
- data/lib/object-let/object.rb +3 -0
- data/lib/object-let/version.rb +1 -1
- data/spec/object_spec.rb +44 -7
- metadata +9 -8
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
= object-let
|
2
2
|
|
3
|
-
|
3
|
+
{<img src="https://gemnasium.com/ronen/object-let.png" alt="Dependency Status" />}[https://gemnasium.com/ronen/object-let]
|
4
|
+
|
5
|
+
Defines <tt>Object#let</tt>, which simply yields the object and returns the result.
|
4
6
|
This idiom, familiar to Lisp programmers, can be handy to eliminate the need
|
5
7
|
for an intermediate variable when you need to use the result of a
|
6
8
|
computation multiple times.
|
@@ -21,6 +23,23 @@ Stylistically, as well in terms of lexical scoping (for ruby 1.9), this
|
|
21
23
|
idiom can make clear that the intermediate result is of no importance outside the
|
22
24
|
block.
|
23
25
|
|
26
|
+
You can also think of this as analogous to "map" in a method chain, but for a single value
|
27
|
+
rather than for an enumerable. Compare:
|
28
|
+
|
29
|
+
array_of_items = thingy.item_names.map{ |name| Item.new(:name => name) }
|
30
|
+
just_one_item = thingy.item_name.let { |name| Item.new(:name => name) }
|
31
|
+
|
32
|
+
|
33
|
+
<tt>Object#let_if</tt> behaves like <tt>Object#let</tt> except that it only yields if the
|
34
|
+
object is truthy; otherwise it returns nil. So, modifying the above
|
35
|
+
example:
|
36
|
+
|
37
|
+
bounds = my_things.find_biggest.let_if { |biggest|
|
38
|
+
Bound.new(:width => biggest.width, :height => biggest.height)
|
39
|
+
}
|
40
|
+
|
41
|
+
+bounds+ will be nil if +find_biggest+ returns nil.
|
42
|
+
|
24
43
|
= See also
|
25
44
|
|
26
45
|
Discussion at http://www.opensourcery.com/blog/zack-hobson/objectlet-ruby-0
|
@@ -31,7 +50,6 @@ The "let" gem (https://rubygems.org/gems/let) provides a module that can be
|
|
31
50
|
included in a class to define memoizing accessors. That gem and this one
|
32
51
|
are compatible.
|
33
52
|
|
34
|
-
|
35
53
|
== Installation
|
36
54
|
|
37
55
|
Install via:
|
@@ -42,6 +60,11 @@ or in your Gemfile:
|
|
42
60
|
|
43
61
|
gem "object-let"
|
44
62
|
|
63
|
+
== History
|
64
|
+
|
65
|
+
* 0.1.0 - Add <tt>Object#left_if</tt>
|
66
|
+
* 0.0.1 - Initial version
|
67
|
+
|
45
68
|
== Copyright
|
46
69
|
|
47
70
|
Released under the MIT License. See LICENSE for details.
|
data/lib/object-let/object.rb
CHANGED
data/lib/object-let/version.rb
CHANGED
data/spec/object_spec.rb
CHANGED
@@ -1,21 +1,58 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
|
3
|
+
shared_examples "let" do
|
4
4
|
|
5
|
-
it "should
|
5
|
+
it "should be supported" do
|
6
6
|
obj = Object.new
|
7
|
-
obj.should be_respond_to
|
7
|
+
obj.should be_respond_to method
|
8
8
|
end
|
9
9
|
|
10
|
-
it "
|
10
|
+
it "should yield the object" do
|
11
11
|
obj = Object.new
|
12
|
-
obj.
|
12
|
+
obj.send(method) { |arg|
|
13
13
|
arg.should equal obj
|
14
14
|
}
|
15
15
|
end
|
16
16
|
|
17
|
-
it "
|
17
|
+
it "should return the block result" do
|
18
18
|
obj = Object.new
|
19
|
-
obj.
|
19
|
+
obj.send(method) {|arg| 3}.should == 3
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
shared_examples "let_if" do
|
24
|
+
it "should not yield" do
|
25
|
+
expect { subject.let_if { raise "in block"} }.should_not raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return nil" do
|
29
|
+
subject.let_if { 12345 }.should be_nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Object" do
|
34
|
+
|
35
|
+
context "let" do
|
36
|
+
it_should_behave_like "let" do
|
37
|
+
let(:method) { :let }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "let_if" do
|
42
|
+
it_should_behave_like "let" do
|
43
|
+
let(:method) { :let_if }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when object is nil" do
|
47
|
+
subject { nil }
|
48
|
+
it_should_behave_like "let_if"
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when object is false" do
|
52
|
+
subject { false }
|
53
|
+
it_should_behave_like "let_if"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object-let
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70259253506940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70259253506940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: simplecov
|
27
|
-
requirement: &
|
27
|
+
requirement: &70259253506520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70259253506520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov-gem-adapter
|
38
|
-
requirement: &
|
38
|
+
requirement: &70259253506060 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70259253506060
|
47
47
|
description: Defines Object#let, which yields the object and returns the result
|
48
48
|
email:
|
49
49
|
- ronen@barzel.org
|
@@ -93,3 +93,4 @@ summary: Defines Object#let, which yields the object and returns the result. Th
|
|
93
93
|
test_files:
|
94
94
|
- spec/object_spec.rb
|
95
95
|
- spec/spec_helper.rb
|
96
|
+
has_rdoc:
|