multi_type 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23b0785b71180fea2fd0096e7626dff20d0f7ee0
4
+ data.tar.gz: e26a7ddc88aca4c26610775440ff07efb730134b
5
+ SHA512:
6
+ metadata.gz: e63f8d7c494549132f6af17bee1f346172529ad5f69c78ad9d45158188b2af9a88fcfc8c2cf3c8f768faeb59f553f03c49fc6b7fcf022ba04fe1919a1246f583
7
+ data.tar.gz: 60afa3c9b04e62ff23f523e2e8ad34b9c25bf428a92edfb2ca92b6b7727cbebbd14811bc196bfd31d1b598db875c8f4ea4fdf62ccae633313ffa11226d5e3147
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pavel Pravosud
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,125 @@
1
+ # MultiType
2
+
3
+ MultiType lets you create weird module-like objects that match object
4
+ instances of certain types. If this sentense doesn't seem to make sense,
5
+ chances are, you don't really need it anyway.
6
+
7
+ Here're some potential usecases:
8
+
9
+ ### Type-checks
10
+
11
+ ```ruby
12
+ class WhateverMediator < Mediators::Base
13
+ Actor = MultiType[User, Admin]
14
+
15
+ def initialize(actor:, action:)
16
+ @actor, @action = actor, action
17
+ unless Actor === actor
18
+ raise ArgumentError, "actor must be of Actor type"
19
+ end
20
+ end
21
+
22
+ def call
23
+ # actor performing an action maybe?
24
+ end
25
+ end
26
+ ```
27
+
28
+ ### Error Rescuing
29
+
30
+ ```ruby
31
+
32
+ class MyClient < Clients::Base
33
+ RetryableErrors = MultiType[
34
+ EOFError,
35
+ Excon::Errors::ResponseParseError,
36
+ Excon::Errors::SocketError,
37
+ Excon::Errors::Timeout,
38
+ OpenSSL::SSL::SSLError,
39
+ SocketError,
40
+ SystemCallError
41
+ ]
42
+
43
+ CriticalErrors = MultiType[
44
+ Excon::Errors::Conflict,
45
+ Excon::Errors::Forbidden,
46
+ Excon::Errors::NotAcceptable,
47
+ Excon::Errors::NotFound,
48
+ Excon::Errors::Unauthorized
49
+ ]
50
+
51
+
52
+ def perform(**params)
53
+ log "performing with #{params}"
54
+
55
+ connection.post(
56
+ path: "/call/action",
57
+ body: JSON.dump(params)
58
+ )
59
+ rescue RetryableErrors => error
60
+ log "failed: #{error}"
61
+ log "retrying in 10"
62
+ sleep 10
63
+ retry
64
+ rescue CriticalErrors => error
65
+ log "failed critically: #{error}"
66
+ perform_cleanup
67
+ rescue => error # ¯\_(ツ)_/¯
68
+ log "unexpected error happened: #{error}"
69
+ raise
70
+ end
71
+ end
72
+ ```
73
+
74
+ ### Combining
75
+
76
+ Combining MultiType with other types or MultiTypes is as easy as creating
77
+ a new MultiType including all those things:
78
+
79
+ ```ruby
80
+ SocketErrors = MultiType[
81
+ EOFError,
82
+ OpenSSL::SSL::SSLError,
83
+ SocketError,
84
+ SystemCallError,
85
+ Timeout::Error
86
+ ]
87
+
88
+ ConnectionErrors = MultiType[
89
+ Excon::Errors::ResponseParseError,
90
+ Excon::Errors::SocketError,
91
+ Excon::Errors::Timeout,
92
+ Net::HTTPBadResponse,
93
+ SocketErrors # note that this is a multi type
94
+ ]
95
+
96
+ # Now ConnectionErrors include all SocketErrors and a bunch of new types
97
+ ```
98
+
99
+ ### Installation
100
+
101
+ Add this line to your application's Gemfile:
102
+
103
+ ```ruby
104
+ gem "multi_type"
105
+ ```
106
+
107
+ And then execute:
108
+
109
+ $ bundle
110
+
111
+ Or install it yourself as:
112
+
113
+ $ gem install multi_type
114
+
115
+
116
+ ### Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at
119
+ https://github.com/rwz/multi_type.
120
+
121
+
122
+ ### License
123
+
124
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
125
+
data/lib/multi_type.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "multi_type/version"
2
+
3
+ module MultiType
4
+ extend self
5
+
6
+ autoload :Group, "multi_type/group"
7
+
8
+ def [](*args)
9
+ Group.new(args).to_module
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module MultiType
2
+ class Group
3
+ def initialize(klasses)
4
+ @klasses = klasses
5
+ end
6
+
7
+ def ===(other)
8
+ @klasses.any? { |k| k === other }
9
+ end
10
+
11
+ def inspect
12
+ ?< + @klasses.map(&:inspect).join(", ") + ?>
13
+ end
14
+
15
+ def to_module
16
+ group = self
17
+
18
+ Module.new do
19
+ extend self
20
+
21
+ %i[inspect ===].each do |m|
22
+ define_method m do |*args|
23
+ group.public_send m, *args
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module MultiType
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Pravosud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - pavel@pravosud.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/multi_type.rb
23
+ - lib/multi_type/group.rb
24
+ - lib/multi_type/version.rb
25
+ homepage: https://github.com/rwz/multi_type
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.5.1
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Multi-type support for Ruby
49
+ test_files: []