ytry 0.0.1
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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/lib/ytry.rb +111 -0
- data/lib/ytry/version.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d75a68288e060261679cfd5e4e94f9185230b2b
|
4
|
+
data.tar.gz: 90087e023cf2c15966ccc9069af417f9e9399b9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2999648ea69177c1877b2ad818946be3216eb36d1ed2283b1d33044973ade655f3a2d06b761f2d0d60c2f23ae8e589404d1d8630e167b656626dbb8b236715a7
|
7
|
+
data.tar.gz: 763fedbdf00f4465687b105964bead8bc3efdb8a6eb1d1bdfdda17d3c632b0b60860ef6d04a708df1eae02a17bbbc067fc067985f229c40268f15233b2c38038
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 lorenzo.barasti
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
[](https://badge.fury.io/rb/ytry)
|
2
|
+
[](https://travis-ci.org/lbarasti/ytry) [](https://coveralls.io/github/lbarasti/ytry?branch=master)
|
3
|
+
|
4
|
+
# Ytry
|
5
|
+
|
6
|
+
A [Scala](http://www.scala-lang.org/api/current/index.html#scala.util.Try) inspired gem that introduces `Try`s to Ruby while aiming for an idiomatic API.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'ytry'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install ytry
|
23
|
+
|
24
|
+
## Basic usage
|
25
|
+
|
26
|
+
The Try type represents a computation that may either result in an exception, or return a successfully computed value ([scala-docs](http://www.scala-lang.org/api/2.11.8/index.html#scala.util.Try))
|
27
|
+
|
28
|
+
[TODO]
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lbarasti/ytry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
40
|
+
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/ytry.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'ytry/version'
|
2
|
+
|
3
|
+
module Ytry
|
4
|
+
def Try
|
5
|
+
raise ArgumentError, 'missing block' unless block_given?
|
6
|
+
begin
|
7
|
+
Success.new(yield)
|
8
|
+
rescue StandardError => e
|
9
|
+
Failure.new(e)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
module Try
|
13
|
+
include Enumerable
|
14
|
+
def self.ary_to_type value
|
15
|
+
raise Try.invalid_argument('Argument must be an array-like object', value) unless value.respond_to? :to_ary
|
16
|
+
return value if value.is_a? Try
|
17
|
+
value.to_ary.empty? ?
|
18
|
+
Failure.new(RuntimeError.new("Could not convert empty array-like object to Success")) :
|
19
|
+
Success.new(value.to_ary.first)
|
20
|
+
end
|
21
|
+
def each &block
|
22
|
+
to_ary.each &block
|
23
|
+
end
|
24
|
+
%i(map select reject collect collect_concat).each do |method|
|
25
|
+
define_method method, ->(&block) {
|
26
|
+
block or return enum_for(method)
|
27
|
+
self.empty? ? self : Try.ary_to_type(Try{super(&block)}.flatten)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
def flat_map &block
|
31
|
+
block or return enum_for(method)
|
32
|
+
return self if self.empty?
|
33
|
+
wrapped_result = Try{block.call(self.get)}
|
34
|
+
return wrapped_result if (!wrapped_result.empty? && !wrapped_result.get.respond_to?(:to_ary))
|
35
|
+
Try.ary_to_type(wrapped_result.flatten)
|
36
|
+
end
|
37
|
+
def grep(pattern, &block)
|
38
|
+
Try.ary_to_type super
|
39
|
+
end
|
40
|
+
def flatten
|
41
|
+
return self if empty?
|
42
|
+
Try.ary_to_type self.get
|
43
|
+
end
|
44
|
+
def zip *others
|
45
|
+
# TODO return first Failure among arguments - if any
|
46
|
+
return Failure.new(Exception.new) if self.empty? || others.any?(&:empty?)
|
47
|
+
collection = others.reduce(self.to_a, &:concat)
|
48
|
+
Success.new collection
|
49
|
+
end
|
50
|
+
def | lambda
|
51
|
+
self.flat_map &lambda # slow but easy to read + supports symbols out of the box
|
52
|
+
end
|
53
|
+
def or_else
|
54
|
+
return self unless empty?
|
55
|
+
other = yield
|
56
|
+
other.is_a?(Try) ? other : raise(Try.invalid_argument('Block should evaluate to an Try', other))
|
57
|
+
end
|
58
|
+
def get_or_else
|
59
|
+
raise ArgumentError, 'missing block' unless block_given?
|
60
|
+
return self.get unless empty?
|
61
|
+
yield
|
62
|
+
end
|
63
|
+
def inspect() to_s end
|
64
|
+
private
|
65
|
+
def self.invalid_argument type_str, arg
|
66
|
+
TypeError.new "#{type_str}. Found #{arg.class}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
class Success
|
70
|
+
include Try
|
71
|
+
def initialize value
|
72
|
+
@value = value.freeze
|
73
|
+
end
|
74
|
+
def get() @value end
|
75
|
+
def empty?() false end
|
76
|
+
def to_s() "Success(#{get})" end
|
77
|
+
def to_ary() [get] end
|
78
|
+
def == other
|
79
|
+
other.is_a?(Success) && self.get == other.get
|
80
|
+
end
|
81
|
+
def === other
|
82
|
+
other.is_a?(Success) && self.get === other.get
|
83
|
+
end
|
84
|
+
def recover &block
|
85
|
+
raise ArgumentError, 'missing block' unless block_given?
|
86
|
+
self
|
87
|
+
end
|
88
|
+
end
|
89
|
+
class Failure
|
90
|
+
include Try
|
91
|
+
attr_reader :error
|
92
|
+
def initialize value
|
93
|
+
@error = value.freeze
|
94
|
+
end
|
95
|
+
def get() raise @error end
|
96
|
+
def empty?() true end
|
97
|
+
def to_s() "Failure(#{@error})" end
|
98
|
+
def to_ary() [] end
|
99
|
+
def == other
|
100
|
+
other.is_a?(Failure) && self.error == other.error
|
101
|
+
end
|
102
|
+
def === other
|
103
|
+
other.is_a?(Failure) && self.error === other.error
|
104
|
+
end
|
105
|
+
def recover &block
|
106
|
+
raise ArgumentError, 'missing block' unless block_given?
|
107
|
+
candidate = Success.new(@error).map &block
|
108
|
+
(!candidate.empty? && candidate.get.nil?) ? self : candidate
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/lib/ytry/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ytry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- lorenzo.barasti
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: '"The Try type represents a computation that may either result in an
|
70
|
+
exception, or return a successfully computed value." (From the scala-docs for scala.util.Try)'
|
71
|
+
email: ytry-user-group@googlegroups.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- lib/ytry.rb
|
79
|
+
- lib/ytry/version.rb
|
80
|
+
homepage: http://lbarasti.github.io/ytry
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '2.0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Scala-inspired Trys for the idiomatic Rubyist
|
104
|
+
test_files: []
|