syncem 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.0pdd.yml +9 -0
- data/.gitignore +6 -0
- data/.pdd +5 -0
- data/.rubocop.yml +26 -0
- data/.rultor.yml +26 -0
- data/.travis.yml +12 -0
- data/Gemfile +25 -0
- data/README.md +69 -0
- data/Rakefile +59 -0
- data/appveyor.yml +25 -0
- data/lib/syncem.rb +57 -0
- data/syncem.gemspec +56 -0
- data/test/test_syncem.rb +81 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b7d113c834d559321aeaa30fa6f9da68bb49f26b83e2309196fda47e70656a82
|
4
|
+
data.tar.gz: 277e6dd0a02a58113dc3aed41aeb250ebc0629c1d804f7a77050c2e2c3789908
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: efff7adc096e2036d36176221755cbcf353af7213f39eb03c211832fe7e0ed3ebbc7363585158cddaf320ac8d3f8901a42139cda770bddd50e3bfbb9f5f9ffa6
|
7
|
+
data.tar.gz: d7183f7a4731ef650f9909401f2b02712a001f6b06cc5685f8a783fdaacf692de0b0386fb26c57ba7c7583e61d8e67adbf84cc3eb3d848831e5125af1f8c5e22
|
data/.0pdd.yml
ADDED
data/.gitignore
ADDED
data/.pdd
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.3
|
4
|
+
|
5
|
+
Layout/EmptyLineAfterGuardClause:
|
6
|
+
Enabled: false
|
7
|
+
Layout/MultilineMethodCallIndentation:
|
8
|
+
Enabled: false
|
9
|
+
Layout/EndOfLine:
|
10
|
+
EnforcedStyle: lf
|
11
|
+
Metrics/AbcSize:
|
12
|
+
Max: 65
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Max: 30
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 50
|
17
|
+
Metrics/ClassLength:
|
18
|
+
Max: 180
|
19
|
+
Metrics/CyclomaticComplexity:
|
20
|
+
Max: 10
|
21
|
+
Metrics/PerceivedComplexity:
|
22
|
+
Max: 10
|
23
|
+
Metrics/ParameterLists:
|
24
|
+
Max: 10
|
25
|
+
Layout/AlignParameters:
|
26
|
+
Enabled: false
|
data/.rultor.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
assets:
|
2
|
+
rubygems.yml: zerocracy/home#assets/rubygems.yml
|
3
|
+
install: |-
|
4
|
+
export GEM_HOME=~/.ruby
|
5
|
+
export GEM_PATH=$GEM_HOME:$GEM_PATH
|
6
|
+
release:
|
7
|
+
script: |-
|
8
|
+
bundle install
|
9
|
+
rake
|
10
|
+
rm -rf *.gem
|
11
|
+
sed -i "s/0\.0\.0/${tag}/g" syncem.gemspec
|
12
|
+
git add syncem.gemspec
|
13
|
+
git commit -m "Version set to ${tag}"
|
14
|
+
gem build syncem.gemspec
|
15
|
+
chmod 0600 ../rubygems.yml
|
16
|
+
gem push *.gem --config-file ../rubygems.yml
|
17
|
+
commanders:
|
18
|
+
- yegor256
|
19
|
+
architect:
|
20
|
+
- yegor256
|
21
|
+
merge:
|
22
|
+
commanders: []
|
23
|
+
script: |-
|
24
|
+
bundle install
|
25
|
+
rake
|
26
|
+
deploy: {}
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2018-2019 Yegor Bugayenko
|
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 all
|
13
|
+
# 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 THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
source 'https://rubygems.org'
|
24
|
+
ruby '~>2.3'
|
25
|
+
gemspec
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
[![EO principles respected here](http://www.elegantobjects.org/badge.svg)](http://www.elegantobjects.org)
|
2
|
+
[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/syncem)](http://www.rultor.com/p/yegor256/syncem)
|
3
|
+
[![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/yegor256/syncem.svg)](https://travis-ci.org/yegor256/syncem)
|
6
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/po1mn8ca96jk0llr?svg=true)](https://ci.appveyor.com/project/yegor256/syncem)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/syncem.svg)](http://badge.fury.io/rb/syncem)
|
8
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/5528e182bb5e4a2ecc1f/maintainability)](https://codeclimate.com/github/yegor256/syncem/maintainability)
|
9
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/yegor256/syncem/master/frames)
|
10
|
+
|
11
|
+
Sometimes you have an object that is not thread-safe,
|
12
|
+
but you need to make sure each of its methods is thread-safe, because they
|
13
|
+
deal with some resources, like files or databases and you want them to
|
14
|
+
manage those resources sequentially. This small gem will help you achieve
|
15
|
+
exactly that without any re-design of the objects you already have. Just
|
16
|
+
decorate them with `SyncEm` decorator and that is it.
|
17
|
+
|
18
|
+
First, install it:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
$ gem install syncem
|
22
|
+
```
|
23
|
+
|
24
|
+
Then, use it like this:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'syncem'
|
28
|
+
obj = SyncEm.new(obj)
|
29
|
+
```
|
30
|
+
|
31
|
+
That's it.
|
32
|
+
|
33
|
+
# How to contribute
|
34
|
+
|
35
|
+
Read [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
|
36
|
+
Make sure you build is green before you contribute
|
37
|
+
your pull request. You will need to have [Ruby](https://www.ruby-lang.org/en/) 2.3+ and
|
38
|
+
[Bundler](https://bundler.io/) installed. Then:
|
39
|
+
|
40
|
+
```
|
41
|
+
$ bundle update
|
42
|
+
$ rake
|
43
|
+
```
|
44
|
+
|
45
|
+
If it's clean and you don't see any error messages, submit your pull request.
|
46
|
+
|
47
|
+
# License
|
48
|
+
|
49
|
+
(The MIT License)
|
50
|
+
|
51
|
+
Copyright (c) 2018-2019 Yegor Bugayenko
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
54
|
+
of this software and associated documentation files (the 'Software'), to deal
|
55
|
+
in the Software without restriction, including without limitation the rights
|
56
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
57
|
+
copies of the Software, and to permit persons to whom the Software is
|
58
|
+
furnished to do so, subject to the following conditions:
|
59
|
+
|
60
|
+
The above copyright notice and this permission notice shall be included in all
|
61
|
+
copies or substantial portions of the Software.
|
62
|
+
|
63
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
64
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
65
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
66
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
67
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
68
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
69
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2018-2019 Yegor Bugayenko
|
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 all
|
13
|
+
# 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 THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'rake'
|
25
|
+
require 'rake/clean'
|
26
|
+
|
27
|
+
CLEAN = FileList['coverage']
|
28
|
+
|
29
|
+
def name
|
30
|
+
@name ||= File.basename(Dir['*.gemspec'].first, '.*')
|
31
|
+
end
|
32
|
+
|
33
|
+
def version
|
34
|
+
Gem::Specification.load(Dir['*.gemspec'].first).version
|
35
|
+
end
|
36
|
+
|
37
|
+
task default: %i[clean test rubocop]
|
38
|
+
|
39
|
+
require 'rake/testtask'
|
40
|
+
desc 'Run all unit tests'
|
41
|
+
Rake::TestTask.new(:test) do |test|
|
42
|
+
test.libs << 'lib' << 'test'
|
43
|
+
test.pattern = 'test/**/test_*.rb'
|
44
|
+
test.verbose = false
|
45
|
+
end
|
46
|
+
|
47
|
+
require 'rdoc/task'
|
48
|
+
RDoc::Task.new do |rdoc|
|
49
|
+
rdoc.main = 'README.md'
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
|
52
|
+
end
|
53
|
+
|
54
|
+
require 'rubocop/rake_task'
|
55
|
+
desc 'Run RuboCop on all directories'
|
56
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
57
|
+
task.fail_on_error = true
|
58
|
+
task.requires << 'rubocop-rspec'
|
59
|
+
end
|
data/appveyor.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
version: '{build}'
|
2
|
+
skip_tags: true
|
3
|
+
clone_depth: 10
|
4
|
+
branches:
|
5
|
+
only:
|
6
|
+
- master
|
7
|
+
except:
|
8
|
+
- gh-pages
|
9
|
+
os: Windows Server 2012
|
10
|
+
environment:
|
11
|
+
matrix:
|
12
|
+
- ruby_version: "25-x64"
|
13
|
+
install:
|
14
|
+
- ps: |
|
15
|
+
$Env:PATH = "C:\Ruby${Env:ruby_version}\bin;${Env:PATH}"
|
16
|
+
- bundle config --local path vendor/bundle
|
17
|
+
- ruby -v
|
18
|
+
- bundle -v
|
19
|
+
build_script:
|
20
|
+
- bundle update
|
21
|
+
- bundle install
|
22
|
+
test_script:
|
23
|
+
- bundle exec rake --quiet
|
24
|
+
cache:
|
25
|
+
- vendor/bundle
|
data/lib/syncem.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018-2019 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
# SyncEm is a simple decorator of an existing object that makes all its
|
26
|
+
# methods thread-safe.
|
27
|
+
#
|
28
|
+
# For more information read
|
29
|
+
# {README}[https://github.com/yegor256/syncem/blob/master/README.md] file.
|
30
|
+
#
|
31
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
32
|
+
# Copyright:: Copyright (c) 2018-2019 Yegor Bugayenko
|
33
|
+
# License:: MIT
|
34
|
+
class SyncEm
|
35
|
+
def initialize(origin)
|
36
|
+
@origin = origin
|
37
|
+
@mutex = Mutex.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(*args)
|
41
|
+
@mutex.synchronize do
|
42
|
+
if @origin.respond_to?(args[0])
|
43
|
+
@origin.send(*args)
|
44
|
+
else
|
45
|
+
super
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def respond_to?(method, include_private = false)
|
51
|
+
@origin.respond_to?(method, include_private)
|
52
|
+
end
|
53
|
+
|
54
|
+
def respond_to_missing?(_method, _include_private = false)
|
55
|
+
true
|
56
|
+
end
|
57
|
+
end
|
data/syncem.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018-2019 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'English'
|
26
|
+
Gem::Specification.new do |s|
|
27
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
28
|
+
if s.respond_to? :required_rubygems_version=
|
29
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 0')
|
30
|
+
end
|
31
|
+
s.rubygems_version = '2.3.3'
|
32
|
+
s.required_ruby_version = '>=2.3'
|
33
|
+
s.name = 'syncem'
|
34
|
+
s.version = '0.1.0'
|
35
|
+
s.license = 'MIT'
|
36
|
+
s.summary = 'Thread-safe decorator of Ruby objects'
|
37
|
+
s.description = 'Sometimes you have an object that is not thread-safe,
|
38
|
+
but you need to make sure each of its methods is thread-safe, because they
|
39
|
+
deal with some resources, like files or databases and you want them to
|
40
|
+
manage those resources sequentially. This small gem will help you achieve
|
41
|
+
exactly that without any re-design of the objects you already have. Just
|
42
|
+
decorate them with SyncEm decorator and that is it.'
|
43
|
+
s.authors = ['Yegor Bugayenko']
|
44
|
+
s.email = 'yegor256@gmail.com'
|
45
|
+
s.homepage = 'http://github.com/yegor256/syncem'
|
46
|
+
s.files = `git ls-files`.split($RS)
|
47
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
48
|
+
s.rdoc_options = ['--charset=UTF-8']
|
49
|
+
s.extra_rdoc_files = ['README.md']
|
50
|
+
s.add_development_dependency 'minitest', '5.11.3'
|
51
|
+
s.add_development_dependency 'rake', '12.3.1'
|
52
|
+
s.add_development_dependency 'rdoc', '4.3.0'
|
53
|
+
s.add_development_dependency 'rubocop', '0.62.0'
|
54
|
+
s.add_development_dependency 'rubocop-rspec', '1.31.0'
|
55
|
+
s.add_development_dependency 'threads', '0.3.0'
|
56
|
+
end
|
data/test/test_syncem.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018-2019 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'minitest/autorun'
|
26
|
+
require 'threads'
|
27
|
+
require_relative '../lib/syncem'
|
28
|
+
|
29
|
+
# Syncem test.
|
30
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
31
|
+
# Copyright:: Copyright (c) 2018-2019 Yegor Bugayenko
|
32
|
+
# License:: MIT
|
33
|
+
class SyncEmTest < Minitest::Test
|
34
|
+
class Account
|
35
|
+
def initialize(file)
|
36
|
+
@file = file
|
37
|
+
end
|
38
|
+
|
39
|
+
def balance
|
40
|
+
File.exist?(@file) ? IO.read(@file).to_i : 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def add(amount)
|
44
|
+
now = File.exist?(@file) ? IO.read(@file).to_i : 0
|
45
|
+
IO.write(@file, (now + amount).to_s)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_wraps_simple_object
|
50
|
+
Dir.mktmpdir do |dir|
|
51
|
+
path = File.join(dir, 'f.txt')
|
52
|
+
acc = Account.new(path)
|
53
|
+
assert_equal(0, acc.balance)
|
54
|
+
acc.add(50)
|
55
|
+
assert_equal(50, acc.balance)
|
56
|
+
acc.add(-10)
|
57
|
+
assert_equal(40, acc.balance)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_respond_to
|
62
|
+
Dir.mktmpdir do |dir|
|
63
|
+
path = File.join(dir, 'f.txt')
|
64
|
+
acc = SyncEm.new(Account.new(path))
|
65
|
+
assert(acc.respond_to?(:balance))
|
66
|
+
assert(acc.respond_to?(:add))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_multiple_threads
|
71
|
+
Dir.mktmpdir do |dir|
|
72
|
+
path = File.join(dir, 'f.txt')
|
73
|
+
acc = SyncEm.new(Account.new(path))
|
74
|
+
threads = 10
|
75
|
+
Threads.new(threads).assert do
|
76
|
+
acc.add(10)
|
77
|
+
end
|
78
|
+
assert_equal(threads * 10, acc.balance)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: syncem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yegor Bugayenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.11.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.11.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 12.3.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 12.3.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.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: 4.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.62.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.62.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.31.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.31.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: threads
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.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.3.0
|
97
|
+
description: |-
|
98
|
+
Sometimes you have an object that is not thread-safe,
|
99
|
+
but you need to make sure each of its methods is thread-safe, because they
|
100
|
+
deal with some resources, like files or databases and you want them to
|
101
|
+
manage those resources sequentially. This small gem will help you achieve
|
102
|
+
exactly that without any re-design of the objects you already have. Just
|
103
|
+
decorate them with SyncEm decorator and that is it.
|
104
|
+
email: yegor256@gmail.com
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files:
|
108
|
+
- README.md
|
109
|
+
files:
|
110
|
+
- ".0pdd.yml"
|
111
|
+
- ".gitignore"
|
112
|
+
- ".pdd"
|
113
|
+
- ".rubocop.yml"
|
114
|
+
- ".rultor.yml"
|
115
|
+
- ".travis.yml"
|
116
|
+
- Gemfile
|
117
|
+
- README.md
|
118
|
+
- Rakefile
|
119
|
+
- appveyor.yml
|
120
|
+
- lib/syncem.rb
|
121
|
+
- syncem.gemspec
|
122
|
+
- test/test_syncem.rb
|
123
|
+
homepage: http://github.com/yegor256/syncem
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata: {}
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options:
|
129
|
+
- "--charset=UTF-8"
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '2.3'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubygems_version: 3.0.1
|
144
|
+
signing_key:
|
145
|
+
specification_version: 2
|
146
|
+
summary: Thread-safe decorator of Ruby objects
|
147
|
+
test_files:
|
148
|
+
- test/test_syncem.rb
|