eventkit-promise 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eventkit-promise.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake', '~> 10.4'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Oliver Martell
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,113 @@
1
+ # Eventkit::Promise
2
+
3
+ Promises/A+ for Ruby
4
+
5
+ [![Build Status](https://travis-ci.org/omartell/eventkit-promise.svg?branch=master)](https://travis-ci.org/omartell/eventkit-promise)
6
+
7
+ Supported Ruby versions: 1.9.3, 2.0, 2.1, 2.2
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'eventkit-promise'
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ require 'eventkit/promise'
21
+
22
+ # Resolving a promise
23
+
24
+ promise = Eventkit::Promise.new
25
+
26
+ promise.then(->(value) { value + 1 })
27
+
28
+ promise.resolve(1)
29
+
30
+ promise.value # => 1
31
+
32
+ # Rejecting a promise
33
+
34
+ promise = Eventkit::Promise.new
35
+
36
+ promise.then(
37
+ ->(value) {
38
+ value + 1
39
+ },
40
+ ->(error) {
41
+ log(error.message)
42
+ }
43
+ )
44
+
45
+ promise.reject(NoMethodError.new('Undefined method #call'))
46
+
47
+ promise.reason # => <NoMethodError: undefined method #call>
48
+
49
+ # Chaining promises
50
+
51
+ promise_a = Eventkit::Promise.new
52
+
53
+ promise_b = promise_a
54
+ .then(->(v) { v + 1 })
55
+ .then(->(v) { v + 1 })
56
+ .then(->(v) { v + 1 })
57
+
58
+ promise_b.catch { |error|
59
+ # Handle errors raised by any of the previous handlers
60
+ }
61
+
62
+ promise_a.resolve(1)
63
+
64
+ promise_a.value # => 1
65
+ promise_b.value # => 4
66
+
67
+ # Resolving and fullfiling with another promise
68
+
69
+ promise_a = Eventkit::Promise.new
70
+ promise_b = Eventkit::Promise.new
71
+
72
+ promise_a.resolve(promise_b)
73
+
74
+ promise_b.resolve('foobar')
75
+
76
+ promise_a.value # => foobar
77
+
78
+ # Resolving and rejecting with another promise
79
+
80
+ promise_a = Eventkit::Promise.new
81
+ promise_b = Eventkit::Promise.new
82
+
83
+ promise_a.resolve(promise_b)
84
+
85
+ promise_b.reject('Ooops can not continue')
86
+
87
+ promise_a.reason # => 'Ooops can not continue'
88
+
89
+ # Initializing with a block
90
+
91
+ promise = Promise.new do |p|
92
+ p.resolve('foobar')
93
+ end
94
+
95
+ promise.value # => 'foobar'
96
+
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ 1. Fork it ( https://github.com/[my-github-username]/eventkit-promise/fork )
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create a new Pull Request
112
+ 6. Happy hacking!
113
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
5
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eventkit/promise'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eventkit-promise"
8
+ spec.version = Eventkit::Promise::VERSION
9
+ spec.authors = ["Oliver Martell"]
10
+ spec.email = ["oliver.martell@gmail.com"]
11
+
12
+ spec.summary = "Promises/A+ for Ruby"
13
+ spec.description = "Ruby implementation of https://promisesaplus.com/"
14
+ spec.homepage = "http://github.com/omartell/eventkit-promise"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.2.0"
23
+ end
@@ -0,0 +1,126 @@
1
+ module Eventkit
2
+ class Promise
3
+ VERSION = "0.1.0"
4
+ attr_reader :value
5
+ alias_method :reason, :value
6
+
7
+ def initialize
8
+ @on_fullfiled = []
9
+ @on_rejected = []
10
+ @state = :pending
11
+ @resolved_with_promise = false
12
+ yield(self) if block_given?
13
+ end
14
+
15
+ def pending?
16
+ @state == :pending
17
+ end
18
+
19
+ def resolved?
20
+ @state == :resolved
21
+ end
22
+
23
+ def rejected?
24
+ @state == :rejected
25
+ end
26
+
27
+ def then(on_fullfiled_handler = nil, on_rejected_handler = nil)
28
+ promise = Promise.new
29
+
30
+ add_on_fullfiled { |value|
31
+ begin
32
+ if on_fullfiled_handler
33
+ promise.resolve(on_fullfiled_handler.to_proc.call(value))
34
+ else
35
+ promise.resolve(value)
36
+ end
37
+ rescue => error
38
+ promise.reject(error)
39
+ end
40
+ }
41
+
42
+ add_on_rejected { |value|
43
+ begin
44
+ if on_rejected_handler
45
+ promise.resolve(on_rejected_handler.to_proc.call(value))
46
+ else
47
+ promise.reject(value)
48
+ end
49
+ rescue => error
50
+ promise.reject(error)
51
+ end
52
+ }
53
+
54
+ promise
55
+ end
56
+
57
+ def resolve(value)
58
+ fail TypeError, 'Promised resolved with itself' if self == value
59
+
60
+ return unless pending? && !@resolved_with_promise
61
+
62
+ run_resolution(value)
63
+ self
64
+ end
65
+
66
+ def reject(value)
67
+ return unless pending? && !@resolved_with_promise
68
+
69
+ run_rejection(value)
70
+ self
71
+ end
72
+
73
+ def on_fullfiled(&handler)
74
+ self.then(handler)
75
+ end
76
+
77
+ def on_rejected(&handler)
78
+ self.then(nil, handler)
79
+ end
80
+
81
+ def catch(&handler)
82
+ self.then(nil, handler)
83
+ end
84
+
85
+ private
86
+
87
+ def add_on_fullfiled(&handler)
88
+ if resolved?
89
+ handler.call(value)
90
+ else
91
+ @on_fullfiled << handler
92
+ end
93
+ end
94
+
95
+ def add_on_rejected(&handler)
96
+ if rejected?
97
+ handler.call(value)
98
+ else
99
+ @on_rejected << handler
100
+ end
101
+ end
102
+
103
+ def run_resolution(value)
104
+ if value.respond_to?(:then)
105
+ begin
106
+ value.then(->(v) { run_resolution(v) },
107
+ ->(v) { run_rejection(v) })
108
+ @resolved_with_promise = true
109
+ rescue => e
110
+ reject(e)
111
+ end
112
+ else
113
+ @state = :resolved
114
+ @value = value
115
+ @on_fullfiled.each { |handler| handler.call(value) }
116
+ end
117
+ end
118
+
119
+ def run_rejection(value)
120
+ @value = value
121
+ @on_rejected.each { |handler| handler.call(value) }
122
+ @state = :rejected
123
+ end
124
+ end
125
+ end
126
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventkit-promise
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Oliver Martell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '10.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '10.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
46
+ description: Ruby implementation of https://promisesaplus.com/
47
+ email:
48
+ - oliver.martell@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - .travis.yml
56
+ - CODE_OF_CONDUCT.md
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - eventkit-promise.gemspec
62
+ - lib/eventkit/promise.rb
63
+ homepage: http://github.com/omartell/eventkit-promise
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Promises/A+ for Ruby
88
+ test_files: []