futurist 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 2.2.2
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in futurist.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Aaron Kuehler
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.
@@ -0,0 +1,50 @@
1
+ # Futurist
2
+
3
+ [![Build Status](https://travis-ci.org/indiebrain/futurist.svg?branch=master)](https://travis-ci.org/indiebrain/futurist)
4
+ [![Code Climate](https://codeclimate.com/github/indiebrain/futurist/badges/gpa.svg)](https://codeclimate.com/github/indiebrain/futurist)
5
+ [![Test Coverage](https://codeclimate.com/github/indiebrain/futurist/badges/coverage.svg)](https://codeclimate.com/github/indiebrain/futurist/coverage)
6
+
7
+ An implementation of the [future](https://en.wikipedia.org/wiki/Futures_and_promises) construct which uses Process forking to resolve its Promise. Promise evaluation is eager, and value resolution blocks until the Promise is resolved.
8
+
9
+ **Note**
10
+ This implementation will only work on OSes which support process forking.
11
+
12
+ ## Installation
13
+
14
+ $ gem install futurist
15
+
16
+
17
+ or add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'futurist'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+
28
+ ## Usage
29
+
30
+ Futures also allow you to background the computation of any block.
31
+
32
+ ### Create a future
33
+
34
+ The call to `Futurist::Future#value` will block until the result of executing the block is available. If an exception occurs during the block's execution, the call to future.value will reraise the same exception.
35
+
36
+ ```ruby
37
+ future = Futurist::Future.new { 3 + 2 }
38
+ future.value # blocks until value is available
39
+ => 5
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/indiebrain/futurist.
@@ -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
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "futurist"
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
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application "rspec" is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ rubocop --config ./.rubocop.yml
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "futurist/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "futurist"
8
+ spec.version = Futurist::VERSION
9
+ spec.authors = ["Aaron Kuehler"]
10
+ spec.email = ["aaron.kuehler@gmail.com"]
11
+
12
+ spec.license = "MIT"
13
+ spec.homepage = "http://www.github.com/indiebrain/futurist.git"
14
+ spec.summary = "A Process based Future"
15
+ spec.description = <<-EOS.gsub(/^\s+/, "")
16
+ An implementation of the Future construct
17
+ (https://en.wikipedia.org/wiki/Futures_and_promises)
18
+ which uses system Processes for background value
19
+ evaluation
20
+ EOS
21
+
22
+ spec.files = `git ls-files -z`.
23
+ split("\x0").
24
+ reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
31
+ spec.add_development_dependency "pry", "~> 0.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.3"
34
+ spec.add_development_dependency "rubocop", "~> 0.33"
35
+ end
@@ -0,0 +1,9 @@
1
+ require "futurist/forking_resolution_strategy"
2
+ require "futurist/future"
3
+ require "futurist/pipe"
4
+ require "futurist/promise"
5
+ require "futurist/safe_promise"
6
+ require "futurist/version"
7
+
8
+ module Futurist
9
+ end
@@ -0,0 +1,57 @@
1
+ module Futurist
2
+ class ForkingResolutionStrategy
3
+ attr_reader :promise_process_id
4
+ def initialize(forking_method: Process.method(:fork),
5
+ process_monitor_constructor: Process.method(:detach),
6
+ channel: Futurist::Pipe.new,
7
+ process_exit: Process.method(:exit!),
8
+ promise:)
9
+ @promise = promise
10
+ @channel = channel
11
+ @forking_method = forking_method
12
+ @value = :not_retrieved
13
+ @process_exit = process_exit
14
+ @promise_process_id = start_promise_evaluation
15
+ @promise_monitor = process_monitor_constructor.call(@promise_process_id)
16
+ end
17
+
18
+ def resolve
19
+ if @value == :not_retrieved
20
+ @value = read_promise_value
21
+ end
22
+ @value
23
+ end
24
+
25
+ def resolved?
26
+ !promise_monitor.alive?
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :promise,
32
+ :channel,
33
+ :forking_method,
34
+ :promise_monitor,
35
+ :process_exit
36
+
37
+ def start_promise_evaluation
38
+ forking_method.call do
39
+ channel.close_reader
40
+ safe_promise = SafePromise.new(promise)
41
+ channel.write(safe_promise.value)
42
+ channel.close_writer
43
+ process_exit.call(0)
44
+ end
45
+ end
46
+
47
+ def read_promise_value
48
+ channel.close_writer
49
+ value = channel.read
50
+ channel.close_reader
51
+ if value.is_a?(Exception)
52
+ raise value
53
+ end
54
+ value
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ module Futurist
2
+ class Future
3
+ def initialize(resolution_strategy: ForkingResolutionStrategy,
4
+ &block)
5
+ promise = Futurist::Promise.new(callable: block)
6
+ @resolution_strategy = resolution_strategy.new(promise: promise)
7
+ end
8
+
9
+ def value
10
+ @value ||= resolution_strategy.resolve
11
+ end
12
+
13
+ def ready?
14
+ resolution_strategy.resolved?
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :resolution_strategy
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module Futurist
2
+ class Pipe
3
+ def initialize(pipe: IO.pipe)
4
+ @reader, @writer = pipe
5
+ end
6
+
7
+ def read
8
+ Marshal.load(reader.read)
9
+ end
10
+
11
+ def write(value)
12
+ Marshal.dump(value, writer)
13
+ end
14
+
15
+ def close_reader
16
+ reader.close
17
+ end
18
+
19
+ def close_writer
20
+ writer.close
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :reader,
26
+ :writer
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ module Futurist
2
+ class Promise
3
+ def initialize(callable:)
4
+ @callable = callable
5
+ end
6
+
7
+ def value
8
+ callable.call
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :callable
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Futurist
2
+ class SafePromise
3
+ def initialize(promise)
4
+ @promise = promise
5
+ end
6
+
7
+ def value
8
+ promise.value
9
+ rescue => error
10
+ error
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :promise
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Futurist
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: futurist
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Kuehler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-17 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: codeclimate-test-reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.33'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.33'
97
+ description: |
98
+ An implementation of the Future construct
99
+ (https://en.wikipedia.org/wiki/Futures_and_promises)
100
+ which uses system Processes for background value
101
+ evaluation
102
+ email:
103
+ - aaron.kuehler@gmail.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".gitignore"
109
+ - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".ruby-version"
112
+ - ".travis.yml"
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/rspec
119
+ - bin/rubocop
120
+ - bin/setup
121
+ - futurist.gemspec
122
+ - lib/futurist.rb
123
+ - lib/futurist/forking_resolution_strategy.rb
124
+ - lib/futurist/future.rb
125
+ - lib/futurist/pipe.rb
126
+ - lib/futurist/promise.rb
127
+ - lib/futurist/safe_promise.rb
128
+ - lib/futurist/version.rb
129
+ homepage: http://www.github.com/indiebrain/futurist.git
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.4.5
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: A Process based Future
153
+ test_files: []