yopt 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/yopt.rb +3 -1
- data/lib/yopt/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbb2a7c8b5105d52caa71a2b3bd9225e0f1b685d
|
4
|
+
data.tar.gz: 8223c2848bbe37d5bcce860e2b041bd3d920f3e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72212175296a542de531f527a84212cb87476620be6e9f8beb01d12d32667329202db342f22bf7831d766702242628bb5f8bf3df0cf0be97b161baa40547a484
|
7
|
+
data.tar.gz: 92d97d04a184624b6c9f490db9a3175da963b5b2e9ce3d030d04a1110d53d6e0e1e336a1b47df05687af15fbcae87f68cded80de5b8de9808e5c17e7228a32b5
|
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/yopt.svg)](https://badge.fury.io/rb/yopt)
|
1
2
|
[![Build Status](https://travis-ci.org/lbarasti/yopt.svg?branch=master)](https://travis-ci.org/lbarasti/yopt) [![Coverage Status](https://coveralls.io/repos/github/lbarasti/yopt/badge.svg?branch=master)](https://coveralls.io/github/lbarasti/yopt?branch=master)
|
2
3
|
|
3
4
|
# Yopt
|
@@ -43,10 +44,12 @@ none.map {|value| value + 2} # returns None
|
|
43
44
|
When we are not interested in the result of a computation on the optional value, it is a good practice to use `Option#each` rather than `Option#map`. That will make our intention clearer.
|
44
45
|
|
45
46
|
```ruby
|
46
|
-
some.each {|value| puts value} # prints 42
|
47
|
-
none.each {|value| puts value} # does not print anything
|
47
|
+
some.each {|value| puts value} # prints 42 and returns `some`
|
48
|
+
none.each {|value| puts value} # does not print anything and returns `none`
|
48
49
|
```
|
49
50
|
|
51
|
+
Notice that `#each` returns the calling option thus supporting method chaining.
|
52
|
+
|
50
53
|
We can safely retrieve the optional value by passing a default value to `Option#get_or_else`
|
51
54
|
|
52
55
|
```ruby
|
@@ -59,7 +62,9 @@ Notice how we are passing a block rather than an argument. This makes the evalua
|
|
59
62
|
This gives us the possibility to react in a special way to a None value without breaking the API fluency, e.g.
|
60
63
|
|
61
64
|
```ruby
|
62
|
-
|
65
|
+
|
66
|
+
|
67
|
+
opt.each {|v| function_with_side_effects(v)}.get_or_else {log_failure}
|
63
68
|
```
|
64
69
|
|
65
70
|
We can also filter the optional value depending on how it evaluates against a block via `Option#select`
|
data/lib/yopt.rb
CHANGED
@@ -17,7 +17,9 @@ module Yopt
|
|
17
17
|
end
|
18
18
|
singleton_class.send(:alias_method, :[], :call)
|
19
19
|
def each &block
|
20
|
-
|
20
|
+
return enum_for(__method__) unless block_given?
|
21
|
+
yield self.get unless empty?
|
22
|
+
return self
|
21
23
|
end
|
22
24
|
%i(map flat_map select reject collect collect_concat).each do |method|
|
23
25
|
define_method method, ->(&block) {
|
data/lib/yopt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lorenzo.barasti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
100
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.5.1
|
102
102
|
signing_key:
|
103
103
|
specification_version: 4
|
104
104
|
summary: Scala-inspired Options for the idiomatic Rubyist
|