trying 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/.gitignore +2 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +40 -0
- data/LICENSE +21 -0
- data/README.md +57 -0
- data/Rakefile +8 -0
- data/lib/trying.rb +49 -0
- data/spec/trying_spec.rb +55 -0
- data/trying.gemspec +21 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb5cbafe009c28cdab3f87d8be1d561ff548970a
|
4
|
+
data.tar.gz: 150e9542d4d0afa2f184ee4c5c64ed765c8da47b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3df73abd97753fc37c7b8535116f5eadfe52bd0638b82b4b854b869dcedc8e90f5e0461abf953179d21400d67199c7cd89c452aedd84d6ba72a523778e34cc64
|
7
|
+
data.tar.gz: 0b89e6b88ebee5016c3d854cd3a9790af6ecad5ee23619af0053da0f4e2e6e90736748e52ade2e98000f1fbae445f538c728407bac29476cc8ba060f55f2c128
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
trying (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.1)
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
method_source (0.8.2)
|
12
|
+
pry (0.10.4)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.8.1)
|
15
|
+
slop (~> 3.4)
|
16
|
+
rspec (3.3.0)
|
17
|
+
rspec-core (~> 3.3.0)
|
18
|
+
rspec-expectations (~> 3.3.0)
|
19
|
+
rspec-mocks (~> 3.3.0)
|
20
|
+
rspec-core (3.3.2)
|
21
|
+
rspec-support (~> 3.3.0)
|
22
|
+
rspec-expectations (3.3.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.3.0)
|
25
|
+
rspec-mocks (3.3.2)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.3.0)
|
28
|
+
rspec-support (3.3.0)
|
29
|
+
slop (3.6.0)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
pry (~> 0.10)
|
36
|
+
rspec (~> 3.1)
|
37
|
+
trying!
|
38
|
+
|
39
|
+
BUNDLED WITH
|
40
|
+
1.14.6
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Dominic Bauer
|
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,57 @@
|
|
1
|
+
# trying
|
2
|
+
A poor man's pipes
|
3
|
+
|
4
|
+
```
|
5
|
+
% echo "gem 'trying'" | Gemfile
|
6
|
+
% gem install trying
|
7
|
+
```
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
String.include(Trying)
|
13
|
+
Range.include(Trying)
|
14
|
+
|
15
|
+
stream = (0..10).trying do
|
16
|
+
to_a
|
17
|
+
lazy
|
18
|
+
map { |x| x ** 2 }
|
19
|
+
first(10) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
20
|
+
end
|
21
|
+
|
22
|
+
truth = "Foobar".trying do # => 42
|
23
|
+
chars # "Foobar" -> ["F", "o", "o", "b", "a", "r"]
|
24
|
+
map(&:upcase) # %w(F o o b a r) -> %w(F O O B A R)
|
25
|
+
map(&:ord) # %w(F O O B A R) -> [70, 79, 79, 66, 65, 82]
|
26
|
+
reduce(0, :+) # [70, 79, 79, 66, 65, 82] -> 441
|
27
|
+
|
28
|
+
# with changes the value by block
|
29
|
+
with { |n| n * Math::PI } # 441 -> 1385.4423602330987
|
30
|
+
|
31
|
+
# tap leaves the callee untouched
|
32
|
+
tap { |n| puts n } # 1385.44... -> (Side effect), still 1385.44...
|
33
|
+
|
34
|
+
# with also swallows errors
|
35
|
+
with { |n| fail if n > 1380 } # 1385.44... -> RuntimeError
|
36
|
+
|
37
|
+
# failed? checks for any swallowed errors
|
38
|
+
failed? { :recover } # => RuntimeError -> :recover
|
39
|
+
|
40
|
+
# failed? accepts specific error classes
|
41
|
+
failed?(StandardError) { :nope } # :recover -> :recover
|
42
|
+
|
43
|
+
eql?(:recover) # :recover -> true
|
44
|
+
nil? # true -> false
|
45
|
+
to_s # false -> "false"
|
46
|
+
length # "false" -> 5
|
47
|
+
|
48
|
+
with { |n| (n..n + 20) } # 5 -> 5..25
|
49
|
+
to_a # 5..25 -> [5, 6, ..., 25]
|
50
|
+
lazy # [5, ..., 25] -> #<Enumerator::Lazy: ...>
|
51
|
+
collect { |x| x ** 2 } # #<Enumerator::Lazy: ...> -> #<Enumerator::Lazy: ...>
|
52
|
+
first(5) # #<Enumerator::Lazy: ...> -> [25, 36, 49, 64, 81]
|
53
|
+
|
54
|
+
reduce(1, &:*) # => [25, 36, 49, 64, 81] -> 228614400
|
55
|
+
public_send(:/, 5443200) # 228614400 => 42
|
56
|
+
end
|
57
|
+
```
|
data/Rakefile
ADDED
data/lib/trying.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Trying
|
2
|
+
class Proxy < BasicObject
|
3
|
+
attr_accessor :cell
|
4
|
+
|
5
|
+
def initialize(obj)
|
6
|
+
@cell = obj
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method, *argv, &block)
|
10
|
+
return nil unless @cell.respond_to?(method)
|
11
|
+
|
12
|
+
@cell = @cell.instance_eval do
|
13
|
+
public_send(method, *argv, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def with(&block)
|
18
|
+
@cell = tap(&block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def tap(&block)
|
22
|
+
block.call(@cell)
|
23
|
+
rescue => e
|
24
|
+
@cell = e
|
25
|
+
end
|
26
|
+
|
27
|
+
def pipe(method, *argv, &block)
|
28
|
+
method.call(@cell, *argv, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def failed?(error = ::StandardError, &block)
|
32
|
+
with(&block) if @cell.is_a? error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def trying(&block)
|
37
|
+
proxy = Proxy.new(self)
|
38
|
+
bound = binding()
|
39
|
+
result = nil
|
40
|
+
|
41
|
+
obj = proxy.instance_eval do
|
42
|
+
proxy = bound.local_variable_get(:proxy)
|
43
|
+
proxy.cell = proxy.instance_eval(&block)
|
44
|
+
bound.local_variable_set(:proxy, proxy)
|
45
|
+
end
|
46
|
+
|
47
|
+
obj.cell
|
48
|
+
end
|
49
|
+
end
|
data/spec/trying_spec.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "trying"
|
2
|
+
|
3
|
+
describe Trying do
|
4
|
+
it "works as advertised" do
|
5
|
+
String.include(Trying)
|
6
|
+
Range.include(Trying)
|
7
|
+
|
8
|
+
stream = (0..10).trying do
|
9
|
+
to_a
|
10
|
+
lazy
|
11
|
+
map { |x| x ** 2 }
|
12
|
+
first(10)
|
13
|
+
end
|
14
|
+
|
15
|
+
expect(stream).to eq [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
16
|
+
|
17
|
+
truth = "Foobar".trying do # => 42
|
18
|
+
chars # "Foobar" -> ["F", "o", "o", "b", "a", "r"]
|
19
|
+
map(&:upcase) # %w(F o o b a r) -> %w(F O O B A R)
|
20
|
+
map(&:ord) # %w(F O O B A R) -> [70, 79, 79, 66, 65, 82]
|
21
|
+
reduce(0, :+) # [70, 79, 79, 66, 65, 82] -> 441
|
22
|
+
|
23
|
+
# with changes the value by block
|
24
|
+
with { |n| n * Math::PI } # 441 -> 1385.4423602330987
|
25
|
+
|
26
|
+
# tap leaves the callee untouched
|
27
|
+
tap { |n| puts n } # 1385.44... -> (Side effect), still 1385.44...
|
28
|
+
|
29
|
+
# with also swallows errors
|
30
|
+
with { |n| fail if n > 1380 } # 1385.44... -> RuntimeError
|
31
|
+
|
32
|
+
# failed? checks for any swallowed errors
|
33
|
+
failed? { :recover } # => RuntimeError -> :recover
|
34
|
+
|
35
|
+
# failed? accepts specific error classes
|
36
|
+
failed?(StandardError) { :nope } # :recover -> :recover
|
37
|
+
|
38
|
+
eql?(:recover) # :recover -> true
|
39
|
+
nil? # true -> false
|
40
|
+
to_s # false -> "false"
|
41
|
+
length # "false" -> 5
|
42
|
+
|
43
|
+
with { |n| (n..n + 20) } # 5 -> 5..25
|
44
|
+
to_a # 5..25 -> [5, 6, ..., 25]
|
45
|
+
lazy # [5, ..., 25] -> #<Enumerator::Lazy: ...>
|
46
|
+
collect { |x| x ** 2 } # #<Enumerator::Lazy: ...> -> #<Enumerator::Lazy: ...>
|
47
|
+
first(5) # #<Enumerator::Lazy: ...> -> [25, 36, 49, 64, 81]
|
48
|
+
|
49
|
+
reduce(1, &:*) # => [25, 36, 49, 64, 81] -> 228614400
|
50
|
+
public_send(:/, 5443200) # 228614400 => 42
|
51
|
+
end
|
52
|
+
|
53
|
+
expect(truth).to be 42
|
54
|
+
end
|
55
|
+
end
|
data/trying.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "trying"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
|
5
|
+
s.summary = <<-summary
|
6
|
+
A poor man's pipes
|
7
|
+
summary
|
8
|
+
|
9
|
+
s.authors = ["Dominic Bauer"]
|
10
|
+
s.email = "bauerdominic@gmail.com"
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
14
|
+
|
15
|
+
s.homepage = "https://github.com/bauerd/trying"
|
16
|
+
s.license = "MIT"
|
17
|
+
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec", "~> 3.1"
|
20
|
+
s.add_development_dependency "pry", "~> 0.10"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trying
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dominic Bauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
41
|
+
description:
|
42
|
+
email: bauerdominic@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".gitignore"
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/trying.rb
|
54
|
+
- spec/trying_spec.rb
|
55
|
+
- trying.gemspec
|
56
|
+
homepage: https://github.com/bauerd/trying
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: A poor man's pipes
|
80
|
+
test_files:
|
81
|
+
- spec/trying_spec.rb
|