yopt 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da7d8098ce8b1dcc66d5e8f6ba3baf208daf805d
4
- data.tar.gz: 6ae68982bb5ff8a3dc09cba07961c04e51b46fa3
3
+ metadata.gz: b2b1eaa4ca004e729d902f06b93fca46dd526175
4
+ data.tar.gz: 920c701a92b2f4a74698b376e81035d049d9bc74
5
5
  SHA512:
6
- metadata.gz: 57fa46f4d6f766ac6fcd87a4b261fd049e8e3757c5578efa2851235b096f1086eb5cb1c801d0ca2dfa0266d6f5d593564677f4a170be22636cbcf3708517ddd9
7
- data.tar.gz: 577ab29f9bdb5283d93941f24995c5d8b4cb02a7847faa2ff66e5018b8c7308f582adac0439ea1ff5c1dd7039fa68d32f8defed983b61dfd34122ddd0f043b0f
6
+ metadata.gz: 1fe9eed7ba9150bef20625b851109cfa872ed1b961ef3e9552126c200f841e45faf978b6d9f2539e516b236ef9a42ae224ccd260363430f75be15a63ae2f3d73
7
+ data.tar.gz: caff9b6961f3217f9d1894bda7bf30227026d86ff28fe043a20da52c05955c9837b93b9ce058a70d1e73583ba5b0294b9e18e1773190c898b73d49d7795f624d
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  /vendor
11
11
  /yopt-0.1.0.gem
12
+ /_site/
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 2.2.0
4
5
  before_install: gem install bundler -v 1.10.6
data/README.md CHANGED
@@ -48,8 +48,16 @@ none.each {|value| puts value} # does not print anything
48
48
  We can safely retrieve the optional value by passing a default value to `Option#get_or_else`
49
49
 
50
50
  ```ruby
51
- some.get_or_else 0 # returns 42
52
- none.get_or_else 0 # returns 0
51
+ some.get_or_else {0} # returns 42
52
+ none.get_or_else {0} # returns 0
53
+ ```
54
+
55
+ Notice how we are passing a block rather than an argument. This makes the evaluation of the default value lazy. In other words the block will only be evaluated if the caller in None.
56
+
57
+ This gives us the possibility to react in a special way to a None value without breaking the API fluency, e.g.
58
+
59
+ ```ruby
60
+ opt.each {|v| do_something_with(v)}.get_or_else {log_failure}
53
61
  ```
54
62
 
55
63
  We can also filter the optional value depending on how it evaluates against a block via `Option#select`
@@ -60,7 +68,7 @@ none.select {|value| value < 0} # returns None
60
68
  some.select {|value| value > 0} # returns Some(42)
61
69
  ```
62
70
 
63
- We can easily turn any object into an Option by means of `Option.call` - aliased to `Option.[]` for convenience.
71
+ We can easily turn any object into an Option by means of `Option.call` - aliased to `Option[]` for convenience.
64
72
  For instance, this is useful when dealing with functions that might return `nil` to express the absence of a result.
65
73
 
66
74
  ```ruby
@@ -87,7 +95,7 @@ returns `c` if `opt` is `None`, and `f.(c, opt)` otherwise.
87
95
 
88
96
  This is a shortcut to
89
97
  ```
90
- opt.map{|v| f.(c,v)}.get_or_else(c)`
98
+ opt.map{|v| f.(c,v)}.get_or_else {c}`
91
99
  ```
92
100
 
93
101
 
@@ -137,15 +145,15 @@ email_opt.zip(captcha_opt).each{|(email,_)| send_pass_recovery(email)}
137
145
 
138
146
  `Option#zip` returns `None` if any of the arguments is `None` or if the caller is `None`
139
147
  ```ruby
140
- Yopt::None.zip Option.[42] # None
141
- Option.[42].zip Yopt::None # None
142
- Option.[42].zip Option.[0], Yopt::None, Option.[-1] # None
148
+ Yopt::None.zip Option[42] # None
149
+ Option[42].zip Yopt::None # None
150
+ Option[42].zip Option[0], Yopt::None, Option[-1] # None
143
151
  ```
144
152
 
145
153
  When both the caller and all the arguments are defined then `zip` collects all the values in an Array wrapped in a `Yopt::Some`
146
154
 
147
155
  ```ruby
148
- Option.[42].zip Option.[0], Option.["str"] # Some([42, 0, "str"])
156
+ Option[42].zip Option[0], Option["str"] # Some([42, 0, "str"])
149
157
  ```
150
158
 
151
159
 
@@ -177,11 +185,11 @@ opt.filter(&is_positive).map {|v| Math.log(v)}
177
185
 
178
186
  Some (None?) might enjoy a comparison with Haskell's [Maybe](https://hackage.haskell.org/package/base/docs/Data-Maybe.html). Here is how the Data.Maybe API translate to Yopt.
179
187
  ```ruby
180
- maybe default f opt -> opt.map(&f).get_or_else(default)
188
+ maybe default f opt -> opt.map(&f).get_or_else {default}
181
189
  isJust opt -> not opt.empty?
182
190
  isNothing opt -> opt.empty?
183
191
  fromJust opt -> opt.get
184
- fromMaybe default opt -> opt.get_or_else default
192
+ fromMaybe default opt -> opt.get_or_else {default}
185
193
  listToMaybe list -> Option.ary_to_type list
186
194
  maybeToList opt -> opt.to_a
187
195
  catMaybes listOfOptions -> listOfOptions.flatten
@@ -48,8 +48,16 @@ none.each {|value| puts value} # does not print anything
48
48
  We can safely retrieve the optional value by passing a default value to `Option#get_or_else`
49
49
 
50
50
  ```ruby
51
- some.get_or_else 0 # returns 42
52
- none.get_or_else 0 # returns 0
51
+ some.get_or_else {0} # returns 42
52
+ none.get_or_else {0} # returns 0
53
+ ```
54
+
55
+ Notice how we are passing a block rather than an argument. This makes the evaluation of the default value lazy. In other words the block will only be evaluated if the caller in None.
56
+
57
+ This gives us the possibility to react in a special way to a None value without breaking the API fluency, e.g.
58
+
59
+ ```ruby
60
+ opt.each {|v| do_something_with(v)}.get_or_else {log_failure}
53
61
  ```
54
62
 
55
63
  We can also filter the optional value depending on how it evaluates against a block via `Option#select`
@@ -60,7 +68,7 @@ none.select {|value| value < 0} # returns None
60
68
  some.select {|value| value > 0} # returns Some(42)
61
69
  ```
62
70
 
63
- We can easily turn any object into an Option by means of `Option.call` - aliased to `Option.[]` for convenience.
71
+ We can easily turn any object into an Option by means of `Option.call` - aliased to `Option[]` for convenience.
64
72
  For instance, this is useful when dealing with functions that might return `nil` to express the absence of a result.
65
73
 
66
74
  ```ruby
@@ -87,7 +95,7 @@ returns `c` if `opt` is `None`, and `f.(c, opt)` otherwise.
87
95
 
88
96
  This is a shortcut to
89
97
  ```
90
- opt.map{|v| f.(c,v)}.get_or_else(c)`
98
+ opt.map{|v| f.(c,v)}.get_or_else {c}`
91
99
  ```
92
100
 
93
101
 
@@ -137,15 +145,15 @@ email_opt.zip(captcha_opt).each{|(email,_)| send_pass_recovery(email)}
137
145
 
138
146
  `Option#zip` returns `None` if any of the arguments is `None` or if the caller is `None`
139
147
  ```ruby
140
- Yopt::None.zip Option.[42] # None
141
- Option.[42].zip Yopt::None # None
142
- Option.[42].zip Option.[0], Yopt::None, Option.[-1] # None
148
+ Yopt::None.zip Option[42] # None
149
+ Option[42].zip Yopt::None # None
150
+ Option[42].zip Option[0], Yopt::None, Option[-1] # None
143
151
  ```
144
152
 
145
153
  When both the caller and all the arguments are defined then `zip` collects all the values in an Array wrapped in a `Yopt::Some`
146
154
 
147
155
  ```ruby
148
- Option.[42].zip Option.[0], Option.["str"] # Some([42, 0, "str"])
156
+ Option[42].zip Option[0], Option["str"] # Some([42, 0, "str"])
149
157
  ```
150
158
 
151
159
 
@@ -177,11 +185,11 @@ opt.filter(&is_positive).map {|v| Math.log(v)}
177
185
 
178
186
  Some (None?) might enjoy a comparison with Haskell's [Maybe](https://hackage.haskell.org/package/base/docs/Data-Maybe.html). Here is how the Data.Maybe API translate to Yopt.
179
187
  ```ruby
180
- maybe default f opt -> opt.map(&f).get_or_else(default)
188
+ maybe default f opt -> opt.map(&f).get_or_else {default}
181
189
  isJust opt -> not opt.empty?
182
190
  isNothing opt -> opt.empty?
183
191
  fromJust opt -> opt.get
184
- fromMaybe default opt -> opt.get_or_else default
192
+ fromMaybe default opt -> opt.get_or_else {default}
185
193
  listToMaybe list -> Option.ary_to_type list
186
194
  maybeToList opt -> opt.to_a
187
195
  catMaybes listOfOptions -> listOfOptions.flatten
@@ -47,11 +47,13 @@ module Yopt
47
47
  raise Option.invalid_argument('an Option', other) unless other.is_a? Option
48
48
  if empty? then other else self end
49
49
  end
50
- def get_or_else default
51
- if empty? then default else self.get end
50
+ def get_or_else
51
+ raise ArgumentError, 'missing block' unless block_given?
52
+ return self.get unless empty?
53
+ yield
52
54
  end
53
55
  def or_nil
54
- get_or_else nil
56
+ get_or_else {nil}
55
57
  end
56
58
  def inspect() to_s end
57
59
  private
@@ -1,3 +1,3 @@
1
1
  module Yopt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Yopt::VERSION
9
9
  spec.authors = ["lorenzo.barasti"]
10
10
 
11
- spec.summary = %q{Scala-inspired Options for the idiomatic Rubyist.}
12
- spec.description = %q{This gem makes it possible to adopt the Option pattern in Ruby. It's meant to make conditional flow in our software clearer and more linear.}
13
- spec.homepage = "https://github.com/lbarasti/yopt"
11
+ spec.summary = %q{Scala-inspired Options for the idiomatic Rubyist}
12
+ spec.description = %q{The Option type models the possible absence of a value. It lets us deal with the uncertainty related to such a value being there without having to resort to errors or conditional blocks.}
13
+ spec.homepage = "http://lbarasti.github.io/yopt"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -18,7 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '>= 2.0'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.10"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "minitest", "~> 5.8"
24
26
  end
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.1.0
4
+ version: 0.2.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-01-23 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,18 +42,19 @@ dependencies:
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '5.8'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description: This gem makes it possible to adopt the Option pattern in Ruby. It's
56
- meant to make conditional flow in our software clearer and more linear.
54
+ version: '5.8'
55
+ description: The Option type models the possible absence of a value. It lets us deal
56
+ with the uncertainty related to such a value being there without having to resort
57
+ to errors or conditional blocks.
57
58
  email:
58
59
  executables: []
59
60
  extensions: []
@@ -79,7 +80,7 @@ files:
79
80
  - lib/yopt.rb
80
81
  - lib/yopt/version.rb
81
82
  - yopt.gemspec
82
- homepage: https://github.com/lbarasti/yopt
83
+ homepage: http://lbarasti.github.io/yopt
83
84
  licenses:
84
85
  - MIT
85
86
  metadata: {}
@@ -91,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
92
  requirements:
92
93
  - - ">="
93
94
  - !ruby/object:Gem::Version
94
- version: '0'
95
+ version: '2.0'
95
96
  required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  requirements:
97
98
  - - ">="
@@ -102,6 +103,6 @@ rubyforge_project:
102
103
  rubygems_version: 2.4.5
103
104
  signing_key:
104
105
  specification_version: 4
105
- summary: Scala-inspired Options for the idiomatic Rubyist.
106
+ summary: Scala-inspired Options for the idiomatic Rubyist
106
107
  test_files: []
107
108
  has_rdoc: