aw 0.1.11 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +65 -30
  4. data/lib/aw/fork.rb +22 -10
  5. data/lib/aw.rb +37 -8
  6. metadata +58 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cef162831a9fd5958fa3123864f9db07d313f4a851b5738ce0f8bf171b12a570
4
- data.tar.gz: 012f7e21a4a6282d4b998df713916509b7d34e4806d305c8878b1771833fc43f
3
+ metadata.gz: 72301167c4e8ea2ca3f8f2ab8bd7295822577c6539aeedf7bbc67b0feeb2eaf5
4
+ data.tar.gz: 0e4afe4ec73d3b19a3bdd2e644f2a45366ad9ac4ad73c7c57c7cd290face4639
5
5
  SHA512:
6
- metadata.gz: ca3c0e6bb8c8fbf4ca45dc57d966cf4b5ca738f3d17936ceb055719b4e58ede5591c4a7208878ff8f0380dfad9d587f9dfd4ceed0d7135c82a3d1d9f9ff3ab62
7
- data.tar.gz: 56152493f8c66f8b6316d0e12bafb8aaeda33c589a0fc2723cb2fa2767c705df5c4823436beab82a284a70adee6cdb90f938561ffed9244866705b3d7d014c88
6
+ metadata.gz: 72244a26c4a020ef1a5b185ca12d1d06dd87c3814d7acc55a3147f9df06eb414de9dbf4968d3e3d613d022d1475e48f7b60d52074e18047023e2fc6f02a0faa1
7
+ data.tar.gz: b6176b99c583a856bff7b98db9cbb9007b4ece7111b58739bfabab5c88324cde06581a46db4b9c8151ae7eb213abf05cc0210c57b7f04dcaf6beabcf299cec2c
data/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2020 Cyril Kato
3
+ Copyright (c) 2015-2022 Cyril Kato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,64 +1,105 @@
1
1
  # Aw
2
2
 
3
- [![Build Status](https://api.travis-ci.org/fixrb/aw.svg?branch=master)][travis]
4
- [![Code Climate](https://codeclimate.com/github/fixrb/aw/badges/gpa.svg)][codeclimate]
5
- [![Gem Version](https://badge.fury.io/rb/aw.svg)][gem]
6
- [![Inline docs](https://inch-ci.org/github/fixrb/aw.svg?branch=master)][inchpages]
7
- [![Documentation](https://img.shields.io/:yard-docs-38c800.svg)][rubydoc]
3
+ [![Version](https://img.shields.io/github/v/tag/fixrb/aw?label=Version&logo=github)](https://github.com/fixrb/aw/releases)
4
+ [![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/fixrb/aw/main)
5
+ [![CI](https://github.com/fixrb/aw/workflows/CI/badge.svg?branch=main)](https://github.com/fixrb/aw/actions?query=workflow%3Aci+branch%3Amain)
6
+ [![RuboCop](https://github.com/fixrb/aw/workflows/RuboCop/badge.svg?branch=main)](https://github.com/fixrb/aw/actions?query=workflow%3Arubocop+branch%3Amain)
7
+ [![License](https://img.shields.io/github/license/fixrb/aw?label=License&logo=github)](https://github.com/fixrb/aw/raw/main/LICENSE.md)
8
8
 
9
- > Aw, fork! 😬
9
+ > Aw, fork 😬
10
10
 
11
- Creates a subprocess to execute a block inside.
11
+ ![Aw](https://github.com/fixrb/aw/raw/main/img/aw.jpg)
12
+
13
+ Creates a sub-process to execute a block inside, and returns what it returns (or a boolean).
12
14
 
13
15
  ## Installation
14
16
 
15
17
  Add this line to your application's Gemfile:
16
18
 
17
19
  ```ruby
18
- gem 'aw'
20
+ gem "aw"
19
21
  ```
20
22
 
21
23
  And then execute:
22
24
 
23
- $ bundle
25
+ ```sh
26
+ bundle install
27
+ ```
24
28
 
25
29
  Or install it yourself as:
26
30
 
27
- $ gem install aw
31
+ ```sh
32
+ gem install aw
33
+ ```
28
34
 
29
35
  ## Usage
30
36
 
31
- It executes the block in a subprocess, and returns the result in the current process:
37
+ To make __Aw__ available:
38
+
39
+ ```ruby
40
+ require "aw"
41
+ ```
42
+
43
+ There are two methods:
44
+
45
+ - `fork!`
46
+ - `fork?`
47
+
48
+ ### Method `.fork!`
49
+
50
+ Executes a block of code in a sub-process, and returns the result:
32
51
 
33
52
  ```ruby
34
53
  Aw.fork! { 6 * 7 } # => 42
35
54
  ```
36
55
 
37
- Of course, it prevents from side effects:
56
+ When the execution of a block of code causes side effects, these are limited to the sub-process:
38
57
 
39
58
  ```ruby
40
- arr = ['foo']
59
+ arr = ["foo"] # => ["foo"]
41
60
 
42
- Aw.fork! { arr << 'FUU' } # => ["foo", "FUU"]
61
+ Aw.fork! { arr << "FUU" } # => ["foo", "FUU"]
43
62
 
44
63
  arr # => ["foo"]
45
64
  ```
46
65
 
47
- Exceptions raised within the block are propagated:
66
+ Exceptions raised in a block of code are propagated:
48
67
 
49
68
  ```ruby
50
- Aw.fork! { nil + 1 } # => NoMethodError (undefined method `+' for nil:NilClass)
69
+ Aw.fork! { nil + 1 }
51
70
  ```
52
71
 
53
- ## Contact
72
+ results in the error:
73
+
74
+ > `NoMethodError` (undefined method `+' for nil:NilClass)
54
75
 
55
- * Home page: https://github.com/fixrb/aw
56
- * Bugs/issues: https://github.com/fixrb/aw/issues
76
+ ### Method `.fork?`
57
77
 
58
- ## Rubies
78
+ Executes a block of code in a sub-process, and returns `true` if no exception is thrown:
79
+
80
+ ```ruby
81
+ Aw.fork? { 6 * 7 } # => true
82
+ ```
59
83
 
60
- * [MRI](https://www.ruby-lang.org/)
61
- * [Rubinius](https://rubinius.com/)
84
+ When the execution of a block of code causes side effects, these are limited to the sub-process:
85
+
86
+ ```ruby
87
+ arr = ["foo"] # => ["foo"]
88
+
89
+ Aw.fork? { arr << "FUU" } # => true
90
+
91
+ arr # => ["foo"]
92
+ ```
93
+
94
+ When an exception is raised in a code block, `false` is returned:
95
+
96
+ ```ruby
97
+ Aw.fork? { nil + 1 } # => false
98
+ ```
99
+
100
+ ## Contact
101
+
102
+ * Source code: https://github.com/fixrb/aw
62
103
 
63
104
  ## Versioning
64
105
 
@@ -66,19 +107,13 @@ __Aw__ follows [Semantic Versioning 2.0](https://semver.org/).
66
107
 
67
108
  ## License
68
109
 
69
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
110
+ The [gem](https://rubygems.org/gems/aw) is available as open source under the terms of the [MIT License](https://github.com/fixrb/aw/raw/main/LICENSE.md).
70
111
 
71
112
  ***
72
113
 
73
114
  <p>
74
115
  This project is sponsored by:<br />
75
116
  <a href="https://sashite.com/"><img
76
- src="https://github.com/fixrb/aw/raw/master/img/sashite.png"
117
+ src="https://github.com/fixrb/aw/raw/main/img/sashite.png"
77
118
  alt="Sashite" /></a>
78
119
  </p>
79
-
80
- [gem]: https://rubygems.org/gems/aw
81
- [travis]: https://travis-ci.org/fixrb/aw
82
- [codeclimate]: https://codeclimate.com/github/fixrb/aw
83
- [inchpages]: https://inch-ci.org/github/fixrb/aw
84
- [rubydoc]: https://rubydoc.info/gems/aw
data/lib/aw/fork.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'English'
3
+ require "English"
4
4
 
5
5
  module Aw
6
6
  # The Fork class.
7
+ #
8
+ # @api private
7
9
  class Fork
8
10
  # Initialize the class.
9
11
  #
@@ -11,7 +13,7 @@ module Aw
11
13
  # @param write [IO] The write endpoint.
12
14
  def initialize(read, write)
13
15
  # Currently, not available on all platforms.
14
- raise ::NotImplementedError, 'fork()' unless ::Process.respond_to?(:fork)
16
+ raise ::NotImplementedError, "fork()" unless ::Process.respond_to?(:fork)
15
17
 
16
18
  @read = read
17
19
  @write = write
@@ -27,26 +29,34 @@ module Aw
27
29
  # @return [IO] The write endpoint.
28
30
  attr_reader :write
29
31
 
30
- # Run the block inside a subprocess, and return the value.
32
+ # Runs the block inside a sub-process, and returns the computed value.
31
33
  #
32
- # @return [#object_id] The result.
33
- def call(*, **, &block)
34
+ # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
35
+ # call { 6 * 7 } # => 42
36
+ #
37
+ # @raise [Exception] Exceptions raised in a block of code are propagated.
38
+ # @return [#object_id] Returns the value that has been returned in the block.
39
+ def call(&block)
34
40
  pid = fork_and_return_pid(&block)
35
41
  write.close
36
42
  result = read.read
37
43
  ::Process.wait(pid)
38
44
 
39
45
  # rubocop:disable Security/MarshalLoad
40
- ::Marshal.load(result).tap do |r|
41
- raise r if r.is_a?(::Exception)
42
- end
46
+ ::Marshal.load(result).tap { |r| raise r if r.is_a?(::Exception) }
43
47
  # rubocop:enable Security/MarshalLoad
44
48
  end
45
49
 
46
50
  private
47
51
 
52
+ # Creates a sub-process to execute a block inside, and returns the
53
+ # sub-process ID.
54
+ #
55
+ # @return [Integer] The ID of the created sub-process.
48
56
  def fork_and_return_pid
49
- fork do
57
+ ::Process.fork do
58
+ # :nocov:
59
+
50
60
  read.close
51
61
 
52
62
  # rubocop:disable Lint/RescueException
@@ -58,7 +68,9 @@ module Aw
58
68
  # rubocop:enable Lint/RescueException
59
69
 
60
70
  ::Marshal.dump(result, write)
61
- exit!(true)
71
+ ::Process.exit!(true)
72
+
73
+ # :nocov:
62
74
  end
63
75
  end
64
76
  end
data/lib/aw.rb CHANGED
@@ -2,19 +2,48 @@
2
2
 
3
3
  # Namespace for the Aw library.
4
4
  #
5
- # @api public
6
- #
7
- # @example Fork and return 42 from 6 * 7.
5
+ # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
8
6
  # Aw.fork! { 6 * 7 } # => 42
7
+ #
8
+ # @example Computes `6 * 7` in a sub-process and returns `true` to the current process if no exception is thrown.
9
+ # Aw.fork? { 6 * 7 } # => true
10
+ #
11
+ # @api public
9
12
  module Aw
10
- # Run the block inside a subprocess, and return the value.
13
+ # Runs the block inside a sub-process, and returns the computed value.
14
+ #
15
+ # @param block [Proc] The code to run in a sub-process.
16
+ #
17
+ # @example Computes `6 * 7` in a sub-process and returns `42` to the current process.
18
+ # Aw.fork! { 6 * 7 } # => 42
11
19
  #
12
- # @param block [Proc] The code to run in a subprocess.
20
+ # @example Computes `nil + 1` in a sub-process and raises `NoMethodError` to the current process.
21
+ # Aw.fork! { nil + 1 } # => raise NoMethodError (undefined method `+' for nil:NilClass)
13
22
  #
14
- # @return [#object_id] The result.
23
+ # @raise [Exception] Exceptions raised in a block of code are propagated.
24
+ # @return [#object_id] Returns the value that has been returned in the block.
15
25
  def self.fork!(&block)
16
- Fork.new(*::IO.pipe).call(&block)
26
+ read, write = ::IO.pipe
27
+ Fork.new(read, write).call(&block)
28
+ end
29
+
30
+ # Runs the block inside a sub-process, and returns `true` if no exception is
31
+ # thrown. Otherwise when an exception is raised, `false` is returned.
32
+ #
33
+ # @param block [Proc] The code to run in a sub-process.
34
+ #
35
+ # @example Computes `6 * 7` in a sub-process and returns `true` to the current process.
36
+ # Aw.fork? { 6 * 7 } # => true
37
+ #
38
+ # @example Computes `nil + 1` in a sub-process and returns `false` to the current process.
39
+ # Aw.fork? { nil + 1 } # => false
40
+ #
41
+ # @return [Boolean] Returns `true` if stat is successful, `false` if not.
42
+ def self.fork?(&block)
43
+ pid = ::Process.fork(&block)
44
+ _, status = ::Process.wait2(pid)
45
+ status.success?
17
46
  end
18
47
  end
19
48
 
20
- require_relative File.join('aw', 'fork')
49
+ require_relative File.join("aw", "fork")
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-09 00:00:00.000000000 Z
11
+ date: 2022-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.1'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.1'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '13.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '13.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rubocop
42
+ name: rubocop-md
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0.79'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0.79'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop-performance
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,35 +66,64 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-thread_safety
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: simplecov
71
99
  requirement: !ruby/object:Gem::Requirement
72
100
  requirements:
73
- - - "~>"
101
+ - - ">="
74
102
  - !ruby/object:Gem::Version
75
- version: '0.17'
103
+ version: '0'
76
104
  type: :development
77
105
  prerelease: false
78
106
  version_requirements: !ruby/object:Gem::Requirement
79
107
  requirements:
80
- - - "~>"
108
+ - - ">="
81
109
  - !ruby/object:Gem::Version
82
- version: '0.17'
110
+ version: '0'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: yard
85
113
  requirement: !ruby/object:Gem::Requirement
86
114
  requirements:
87
- - - "~>"
115
+ - - ">="
88
116
  - !ruby/object:Gem::Version
89
- version: '0.9'
117
+ version: '0'
90
118
  type: :development
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
- - - "~>"
122
+ - - ">="
95
123
  - !ruby/object:Gem::Version
96
- version: '0.9'
97
- description: Creates a subprocess to execute a block inside.
124
+ version: '0'
125
+ description: Creates a sub-process to execute a block inside, and returns what it
126
+ returns (or a boolean).
98
127
  email: contact@cyril.email
99
128
  executables: []
100
129
  extensions: []
@@ -107,7 +136,8 @@ files:
107
136
  homepage: https://github.com/fixrb/aw
108
137
  licenses:
109
138
  - MIT
110
- metadata: {}
139
+ metadata:
140
+ rubygems_mfa_required: 'true'
111
141
  post_install_message:
112
142
  rdoc_options: []
113
143
  require_paths:
@@ -116,15 +146,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
146
  requirements:
117
147
  - - ">="
118
148
  - !ruby/object:Gem::Version
119
- version: '0'
149
+ version: 2.7.0
120
150
  required_rubygems_version: !ruby/object:Gem::Requirement
121
151
  requirements:
122
152
  - - ">="
123
153
  - !ruby/object:Gem::Version
124
154
  version: '0'
125
155
  requirements: []
126
- rubygems_version: 3.1.2
156
+ rubygems_version: 3.1.6
127
157
  signing_key:
128
158
  specification_version: 4
129
- summary: "Aw, fork! \U0001F62C"
159
+ summary: "Aw, fork \U0001F62C"
130
160
  test_files: []