resonad 1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '049ebe880a81a65ead9e8fc9521acccc2fcf2b8d'
4
+ data.tar.gz: 502f3566a2a5e70424528d1ec79ba0cd234cf3f0
5
+ SHA512:
6
+ metadata.gz: 24fc92d3b8c14bf477ae416a96c228107e169c0a18f203a814007215d112641aad620919ef37e23b4e86724a7fee98dcbd40fd37d28fe3404f28ba65a2eef33f
7
+ data.tar.gz: fac2fdac51b782ba081ea3a7c2fcff0f71c44ee91f342d5dab098abd89c9585414543bbf6fabf8b6cd24bfc87a315a51dbdcddaf9677b9d41745bb34e2951914
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.7
4
+ - 2.3.4
5
+ - 2.4.1
6
+ cache: bundler
7
+ deploy:
8
+ provider: rubygems
9
+ on:
10
+ tags: true
11
+ rvm: 2.4.1
12
+ api_key:
13
+ secure: JPfhiLx7tDs9u4nXvVpQX041NHgZpTMTCAWqG52Us6pJIpDXl+nNzKaI0U/grucEE+OZQ8wfq/N3MV5uHHel+NzwVIVeJlouz3JiPXJUI60+tPvucLHe63LR/I3jzCWzHRLcwCYRo8mhh6jrq01KztgFRUxHzRTRodDcqFMi+xE=
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tom@tomdalling.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resonad.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Tom Dalling
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,78 @@
1
+ # Resonad
2
+
3
+ Usage example (assume each method returns a `Resonad`):
4
+
5
+ ```ruby
6
+ find_widget(widget_id)
7
+ .and_then { |widget| update_widget(widget) }
8
+ .on_success { |widget| logger.info("Updated #{widget}" }
9
+ .on_failure { |error| logger.warn("Widget update failed because #{error}") }
10
+ ```
11
+
12
+ Success type:
13
+
14
+ ```ruby
15
+ result = Resonad.Success(5)
16
+ result.success? #=> true
17
+ result.failure? #=> false
18
+ result.value #=> 5
19
+ result.error #=> raises an exception
20
+ ```
21
+
22
+ Failure type:
23
+
24
+ ```ruby
25
+ result = Resonad.Failure(:buzz)
26
+ result.success? #=> false
27
+ result.failure? #=> true
28
+ result.value #=> raises an exception
29
+ result.error #=> :buzz
30
+ ```
31
+
32
+ Mapping monads:
33
+
34
+ ```ruby
35
+ result = Resonad.Success(5)
36
+ .map { |i| i + 1 }
37
+ .map { |i| i + 1 }
38
+ .map { |i| i + 1 }
39
+ result.success? #=> true
40
+ result.value #=> 8
41
+
42
+ result = Resonad.Failure(:buzz)
43
+ .map { |i| i + 1 }
44
+ .map { |i| i + 1 }
45
+ .map { |i| i + 1 }
46
+ result.success? #=> false
47
+ result.error #=> :buzz
48
+ ```
49
+
50
+ Flat mapping monads (a.k.a. `and_then`):
51
+
52
+ ```ruby
53
+ result = Resonad.Success(5)
54
+ .and_then { |i| Resonad.Success(i + 1) }
55
+ .and_then { |i| Resonad.Failure("buzz #{i}") }
56
+ .and_then { |i| Resonad.Success(i + 1) }
57
+ .error #=> "buzz 6"
58
+
59
+ # can also use the less-nice `flat_map` method
60
+ result
61
+ .flat_map { |i| Resonad.Success(i + 1) }
62
+ ```
63
+
64
+ Automatic exception rescuing:
65
+
66
+ ```ruby
67
+ def try_divide(top, bottom)
68
+ Resonad.rescuing_from(ZeroDivisionError) { top / bottom }
69
+ end
70
+
71
+ yep = try_divide(6, 2)
72
+ yep.success? #=> true
73
+ yep.value #=> 3
74
+
75
+ nope = try_divide(6, 0)
76
+ nope.success? #=> false
77
+ node.error #=> #<ZeroDivisionError: ZeroDivisionError>
78
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "resonad"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/resonad.rb ADDED
@@ -0,0 +1,131 @@
1
+ module Resonad
2
+ class NonExistantError < StandardError; end
3
+ class NonExistantValue < StandardError; end
4
+
5
+ def self.Success(*args)
6
+ Success.new(*args)
7
+ end
8
+
9
+ def self.Failure(*args)
10
+ Failure.new(*args)
11
+ end
12
+
13
+ def self.rescuing_from(*exception_classes)
14
+ Success(yield)
15
+ rescue Exception => e
16
+ if exception_classes.empty?
17
+ Failure(e) # rescue from all exceptions
18
+ elsif exception_classes.any? { |klass| e.is_a?(klass) }
19
+ Failure(e) # rescue from specified exception type
20
+ else
21
+ raise # reraise unhandled exception
22
+ end
23
+ end
24
+
25
+ class Success
26
+ attr_accessor :value
27
+
28
+ def initialize(value)
29
+ @value = value
30
+ freeze
31
+ end
32
+
33
+ def success?
34
+ true
35
+ end
36
+
37
+ def failure?
38
+ false
39
+ end
40
+
41
+ def on_success
42
+ yield value
43
+ self
44
+ end
45
+
46
+ def on_failure
47
+ self
48
+ end
49
+
50
+ def error
51
+ raise NonExistantError, "Success resonads do not have errors"
52
+ end
53
+
54
+ def map
55
+ new_value = yield(value)
56
+ if new_value.__id__ == value.__id__
57
+ self
58
+ else
59
+ self.class.new(new_value)
60
+ end
61
+ end
62
+
63
+ def map_error
64
+ self
65
+ end
66
+
67
+ def flat_map
68
+ yield value
69
+ end
70
+ alias_method :and_then, :flat_map
71
+
72
+ def flat_map_error
73
+ self
74
+ end
75
+ alias_method :or_else, :flat_map_error
76
+ end
77
+
78
+ class Failure
79
+ attr_accessor :error
80
+
81
+ def initialize(error)
82
+ @error = error
83
+ freeze
84
+ end
85
+
86
+ def success?
87
+ false
88
+ end
89
+
90
+ def failure?
91
+ true
92
+ end
93
+
94
+ def on_success
95
+ self
96
+ end
97
+
98
+ def on_failure
99
+ yield error
100
+ self
101
+ end
102
+
103
+ def value
104
+ raise NonExistantValue, "Failure resonads do no have values"
105
+ end
106
+
107
+ def map
108
+ self
109
+ end
110
+
111
+ def map_error
112
+ new_error = yield(error)
113
+ if new_error.__id__ == error.__id__
114
+ self
115
+ else
116
+ self.class.new(new_error)
117
+ end
118
+ end
119
+
120
+ def flat_map
121
+ self
122
+ end
123
+ alias_method :and_then, :flat_map
124
+
125
+ def flat_map_error
126
+ yield error
127
+ end
128
+ alias_method :or_else, :flat_map_error
129
+ end
130
+
131
+ end
@@ -0,0 +1,3 @@
1
+ module Resonad
2
+ VERSION = '1.0'
3
+ end
data/resonad.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+ require 'resonad/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'resonad'
7
+ spec.version = Resonad::VERSION
8
+ spec.authors = ["Tom Dalling"]
9
+ spec.email = ['tom' + '@' + 'tomdalling.com']
10
+
11
+ spec.summary = "Objects that represent success or failure"
12
+ spec.homepage = "https://github.com/tomdalling/resonad"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "gem-release", "~> 0.7"
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resonad
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Tom Dalling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-22 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gem-release
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ description:
70
+ email:
71
+ - tom@tomdalling.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/resonad.rb
87
+ - lib/resonad/version.rb
88
+ - resonad.gemspec
89
+ homepage: https://github.com/tomdalling/resonad
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Objects that represent success or failure
113
+ test_files: []