focus_actor 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d829a31144bf0acd271fe344c811c7c2a9ebbf87032c8ff161207c590a36bdf3
4
+ data.tar.gz: cefdfe56ecb66e63680c84c1c0020e655ca0f3c12b1dda9dff6370b054143198
5
+ SHA512:
6
+ metadata.gz: b21356bcd44f228fa626cb2257a6cbcf6e4fcd0c494f5d60b3f1d224cc8379b3eb2c2c9e45798e2d3badfc4de97bbae253bb847dd263f9c3af85b680dc9bddbc
7
+ data.tar.gz: a78c8beae8dbf4a1a4470305d0e758269a4e4467048352bdd768e62dde617d15a821131ab37ae83f0ecbb73fcba39a2d791f4db4866bc30c7f8a6ff1a1127e3d
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,254 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rails
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.5
7
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
8
+ # to ignore them, so only the ones explicitly set in this file are enabled.
9
+ DisabledByDefault: true
10
+ Exclude:
11
+ - './focus_actor.gemspec'
12
+
13
+ Performance:
14
+ Exclude:
15
+ - '**/test/**/*'
16
+
17
+ # Prefer assert_not over assert !
18
+ Rails/AssertNot:
19
+ Include:
20
+ - '**/test/**/*'
21
+
22
+ # Prefer assert_not_x over refute_x
23
+ Rails/RefuteMethods:
24
+ Include:
25
+ - '**/test/**/*'
26
+
27
+ Rails/IndexBy:
28
+ Enabled: true
29
+
30
+ Rails/IndexWith:
31
+ Enabled: true
32
+
33
+ # Prefer &&/|| over and/or.
34
+ Style/AndOr:
35
+ Enabled: true
36
+
37
+ # Align `when` with `case`.
38
+ Layout/CaseIndentation:
39
+ Enabled: true
40
+
41
+ Layout/ClosingHeredocIndentation:
42
+ Enabled: true
43
+
44
+ # Align comments with method definitions.
45
+ Layout/CommentIndentation:
46
+ Enabled: true
47
+
48
+ Layout/ElseAlignment:
49
+ Enabled: true
50
+
51
+ # Align `end` with the matching keyword or starting expression except for
52
+ # assignments, where it should be aligned with the LHS.
53
+ Layout/EndAlignment:
54
+ Enabled: true
55
+ EnforcedStyleAlignWith: variable
56
+ AutoCorrect: true
57
+
58
+ Layout/EmptyLineAfterMagicComment:
59
+ Enabled: true
60
+
61
+ Layout/EmptyLinesAroundAccessModifier:
62
+ Enabled: true
63
+ EnforcedStyle: only_before
64
+
65
+ Layout/EmptyLinesAroundBlockBody:
66
+ Enabled: true
67
+
68
+ # In a regular class definition, no empty lines around the body.
69
+ Layout/EmptyLinesAroundClassBody:
70
+ Enabled: true
71
+
72
+ # In a regular method definition, no empty lines around the body.
73
+ Layout/EmptyLinesAroundMethodBody:
74
+ Enabled: true
75
+
76
+ # In a regular module definition, no empty lines around the body.
77
+ Layout/EmptyLinesAroundModuleBody:
78
+ Enabled: true
79
+
80
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
81
+ Style/HashSyntax:
82
+ Enabled: true
83
+
84
+ Layout/FirstArgumentIndentation:
85
+ Enabled: true
86
+
87
+ # Method definitions after `private` or `protected` isolated calls need one
88
+ # extra level of indentation.
89
+ Layout/IndentationConsistency:
90
+ Enabled: true
91
+ EnforcedStyle: indented_internal_methods
92
+
93
+ # Two spaces, no tabs (for indentation).
94
+ Layout/IndentationWidth:
95
+ Enabled: true
96
+
97
+ Layout/LeadingCommentSpace:
98
+ Enabled: true
99
+
100
+ Layout/SpaceAfterColon:
101
+ Enabled: true
102
+
103
+ Layout/SpaceAfterComma:
104
+ Enabled: true
105
+
106
+ Layout/SpaceAfterSemicolon:
107
+ Enabled: true
108
+
109
+ Layout/SpaceAroundEqualsInParameterDefault:
110
+ Enabled: true
111
+
112
+ Layout/SpaceAroundKeyword:
113
+ Enabled: true
114
+
115
+ Layout/SpaceBeforeComma:
116
+ Enabled: true
117
+
118
+ Layout/SpaceBeforeComment:
119
+ Enabled: true
120
+
121
+ Layout/SpaceBeforeFirstArg:
122
+ Enabled: true
123
+
124
+ Style/DefWithParentheses:
125
+ Enabled: true
126
+
127
+ # Defining a method with parameters needs parentheses.
128
+ Style/MethodDefParentheses:
129
+ Enabled: true
130
+
131
+ Style/RedundantFreeze:
132
+ Enabled: true
133
+
134
+ # Use `foo {}` not `foo{}`.
135
+ Layout/SpaceBeforeBlockBraces:
136
+ Enabled: true
137
+
138
+ # Use `foo { bar }` not `foo {bar}`.
139
+ Layout/SpaceInsideBlockBraces:
140
+ Enabled: true
141
+ EnforcedStyleForEmptyBraces: space
142
+
143
+ # Use `{ a: 1 }` not `{a:1}`.
144
+ Layout/SpaceInsideHashLiteralBraces:
145
+ Enabled: true
146
+
147
+ Layout/SpaceInsideParens:
148
+ Enabled: true
149
+
150
+ ## Check quotes usage according to lint rule below.
151
+ #Style/StringLiterals:
152
+ # Enabled: true
153
+ # EnforcedStyle: double_quotes
154
+
155
+ # Detect hard tabs, no hard tabs.
156
+ Layout/IndentationStyle:
157
+ Enabled: true
158
+
159
+ # Empty lines should not have any spaces.
160
+ Layout/TrailingEmptyLines:
161
+ Enabled: true
162
+
163
+ # No trailing whitespace.
164
+ Layout/TrailingWhitespace:
165
+ Enabled: true
166
+
167
+ # Use quotes for string literals when they are enough.
168
+ Style/RedundantPercentQ:
169
+ Enabled: true
170
+
171
+ Lint/AmbiguousOperator:
172
+ Enabled: true
173
+
174
+ Lint/AmbiguousRegexpLiteral:
175
+ Enabled: true
176
+
177
+ Lint/ErbNewArguments:
178
+ Enabled: true
179
+
180
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
181
+ Lint/RequireParentheses:
182
+ Enabled: true
183
+
184
+ Lint/ShadowingOuterLocalVariable:
185
+ Enabled: true
186
+
187
+ Lint/RedundantStringCoercion:
188
+ Enabled: true
189
+
190
+ Lint/UriEscapeUnescape:
191
+ Enabled: true
192
+
193
+ Lint/UselessAssignment:
194
+ Enabled: true
195
+
196
+ Lint/DeprecatedClassMethods:
197
+ Enabled: true
198
+
199
+ Style/ParenthesesAroundCondition:
200
+ Enabled: true
201
+
202
+ Style/HashTransformKeys:
203
+ Enabled: true
204
+
205
+ Style/HashTransformValues:
206
+ Enabled: true
207
+
208
+ Style/RedundantBegin:
209
+ Enabled: true
210
+
211
+ Style/RedundantReturn:
212
+ Enabled: true
213
+ AllowMultipleReturnValues: true
214
+
215
+ Style/Semicolon:
216
+ Enabled: true
217
+ AllowAsExpressionSeparator: true
218
+
219
+ # Prefer Foo.method over Foo::method
220
+ Style/ColonMethodCall:
221
+ Enabled: true
222
+
223
+ Style/TrivialAccessors:
224
+ Enabled: true
225
+
226
+ Performance/FlatMap:
227
+ Enabled: true
228
+
229
+ Performance/RedundantMerge:
230
+ Enabled: true
231
+
232
+ Performance/StartWith:
233
+ Enabled: true
234
+
235
+ Performance/EndWith:
236
+ Enabled: true
237
+
238
+ Performance/RegexpMatch:
239
+ Enabled: true
240
+
241
+ Performance/ReverseEach:
242
+ Enabled: true
243
+
244
+ Performance/UnfreezeString:
245
+ Enabled: true
246
+
247
+ #Performance/DeletePrefix:
248
+ # Enabled: true
249
+ #
250
+ #Performance/DeleteSuffix:
251
+ # Enabled: true
252
+
253
+ Style/FrozenStringLiteralComment:
254
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in focus_actor.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug'
7
+ gem "rake", "~> 12.0"
8
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ focus_actor (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ diff-lcs (1.3)
11
+ rake (12.3.2)
12
+ rspec (3.8.0)
13
+ rspec-core (~> 3.8.0)
14
+ rspec-expectations (~> 3.8.0)
15
+ rspec-mocks (~> 3.8.0)
16
+ rspec-core (3.8.0)
17
+ rspec-support (~> 3.8.0)
18
+ rspec-expectations (3.8.2)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-mocks (3.8.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-support (3.8.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ byebug
31
+ focus_actor!
32
+ rake (~> 12.0)
33
+ rspec (~> 3.0)
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
@@ -0,0 +1,45 @@
1
+ # FocusActor
2
+
3
+ This is a toy tool to extend concurrency for objects.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'focus_actor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install focus_actor
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class User
25
+ include FocusActor::Async
26
+ include FocusActor::Future
27
+
28
+ attr_reader :name, :age
29
+
30
+ def initialize(name)
31
+ @name = name
32
+ @age = 0
33
+ end
34
+
35
+ def grow(cost = 0.1)
36
+ sleep cost
37
+ @age += 1
38
+ end
39
+ end
40
+
41
+ user = User.new('Bob')
42
+ user.async.grow(1) # return nil, without block
43
+ future = user.future.grow(1) # return FutureCell
44
+ future.value
45
+ ```
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "focus_actor"
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__)
@@ -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
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/focus_actor/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "focus_actor"
5
+ spec.version = FocusActor::VERSION
6
+ spec.authors = ["Baodong"]
7
+ spec.email = ["wwwicbd@gmail.com"]
8
+
9
+ spec.summary = %q{Actor Tools}
10
+ spec.description = %q{Actor Tools}
11
+ spec.homepage = 'https://github.com/icbd/focus_actor'
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "#{spec.homepage}.git"
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/tags"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,8 @@
1
+ require "focus_actor/version"
2
+ require "focus_actor/async"
3
+ require "focus_actor/future"
4
+
5
+ module FocusActor
6
+ class Error < StandardError
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'async_processor'
2
+
3
+ module FocusActor
4
+ module Async
5
+ module InitializeExtension
6
+ def initialize(*args, &block)
7
+ super
8
+ @async = AsyncProcessor.new(self)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.send :prepend, InitializeExtension
14
+ base.attr_reader :async
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'cell_context'
2
+
3
+ module FocusActor
4
+ # This is a Proxy for instance.
5
+ # It can response instance methods on instance.
6
+ #
7
+ # Start a new thread, and then loop to call methods in mailbox.
8
+ class AsyncProcessor
9
+ attr_reader :instance, :mailbox
10
+
11
+ # mailbox should be thread safe
12
+ def initialize(instance, mailbox: Queue.new)
13
+ @instance = instance
14
+ @mailbox = mailbox
15
+
16
+ run!
17
+ end
18
+
19
+ # To call a method, push it to mailbox.
20
+ def method_missing(method, *args, &block)
21
+ return super unless instance.respond_to?(method)
22
+
23
+ mailbox.push CellContext.new(method, args, block)
24
+ nil
25
+ end
26
+
27
+ def respond_to_missing?(method, include_all = false)
28
+ instance.respond_to?(method) || super
29
+ end
30
+
31
+ private def run!
32
+ Thread.new do
33
+ loop do
34
+ cell_context = mailbox.pop
35
+ instance.public_send(cell_context.method, *cell_context.args, &cell_context.block)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ module FocusActor
2
+ CellContext = Struct.new(:method, :args, :block) do
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'future_processor'
2
+
3
+ module FocusActor
4
+ module Future
5
+ module InitializeExtension
6
+ def initialize(*args, &block)
7
+ super
8
+ @future = FutureProcessor.new(self)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.send :prepend, InitializeExtension
14
+ base.attr_reader :future
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module FocusActor
2
+ class FutureCell
3
+ def initialize(queue)
4
+ @queue = queue
5
+ @value_lock = Mutex.new
6
+ end
7
+
8
+ def value
9
+ @value ||= @value_lock.synchronize do
10
+ @value ||= @queue.pop
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'cell_context'
2
+ require_relative 'future_cell'
3
+
4
+ module FocusActor
5
+ class FutureProcessor
6
+ attr_reader :instance
7
+
8
+ def initialize(instance)
9
+ @instance = instance
10
+ end
11
+
12
+ def method_missing(method, *args, &block)
13
+ return super unless instance.respond_to?(method)
14
+
15
+ queue = Queue.new
16
+ Thread.new do
17
+ queue.push instance.public_send(method, *args, &block)
18
+ end
19
+
20
+ FutureCell.new(queue)
21
+ end
22
+
23
+ def respond_to_missing?(method, include_all = false)
24
+ instance.respond_to?(method) || super
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module FocusActor
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: focus_actor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Baodong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Actor Tools
14
+ email:
15
+ - wwwicbd@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - focus_actor.gemspec
31
+ - lib/focus_actor.rb
32
+ - lib/focus_actor/async.rb
33
+ - lib/focus_actor/async_processor.rb
34
+ - lib/focus_actor/cell_context.rb
35
+ - lib/focus_actor/future.rb
36
+ - lib/focus_actor/future_cell.rb
37
+ - lib/focus_actor/future_processor.rb
38
+ - lib/focus_actor/version.rb
39
+ homepage: https://github.com/icbd/focus_actor
40
+ licenses: []
41
+ metadata:
42
+ homepage_uri: https://github.com/icbd/focus_actor
43
+ source_code_uri: https://github.com/icbd/focus_actor.git
44
+ changelog_uri: https://github.com/icbd/focus_actor/tags
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.6
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Actor Tools
64
+ test_files: []