asynchronize 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b87faff7002bd120d62dfdb0492443ea9ff339298116a65f4b648d3e69698ba
4
- data.tar.gz: 4f278555322d36220fdf8e32ef750e550946c553009f43e9c4273005b594219f
3
+ metadata.gz: af0aada41350e739802a92c04ef32bc64fbb897dd4d3ff7b0450c1485f9a243a
4
+ data.tar.gz: 457e2ccdf4b87955b8dc68011f8e7d84be3458fa6a83333f56b755457729121f
5
5
  SHA512:
6
- metadata.gz: 73d49afc4158a3d06e42428b008790cee9303f739bac56c8cd6dacecae57963204439e0f7d3631dc090bd5a77bde8c3985bcdf9bb13871aaf911dc9819dcdcaf
7
- data.tar.gz: 60028e43fb1f7ca8a7fa9055750d777dfd91da8f1ae327306cc73927ab1db35632f8aacaec4cd43a80a146c77f97d740c1f7b76368de0ec3389b84a500b5b437
6
+ metadata.gz: d0f2214354035c147289553f31326709f7707a1a6a5a6101778f87198f18abd99c511763297f87662cec869426c20bae9ee5522d19232ce33878c4e238a573ae
7
+ data.tar.gz: 7abf0867f9c2e238f475b0925db4610194de9ef954baa48648d79492ae4fc89eaddba1e2801394cafc4034f675aaf5e5f4b0b80677c348404ef3b97cc81567af
@@ -2,7 +2,7 @@ require 'date'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'asynchronize'
5
- s.version = '0.1.1'
5
+ s.version = '0.1.2'
6
6
  s.date = Date.today.to_s
7
7
  s.summary = 'Easily make multiple methods asynchronous with one line of code.'
8
8
  s.description = 'Take any synchronous method, and run it asynchronously, ' +
@@ -12,12 +12,14 @@ Gem::Specification.new do |s|
12
12
  s.files = [
13
13
  'lib/asynchronize.rb',
14
14
  'spec/spec.rb',
15
+ 'spec/minitest_helper.rb',
15
16
  'asynchronize.gemspec',
16
17
  'Rakefile',
17
- 'readme.md'
18
+ 'readme.md',
18
19
  ]
19
20
  s.test_files = [
20
- 'spec/spec.rb'
21
+ 'spec/spec.rb',
22
+ 'spec/minitest_helper.rb'
21
23
  ]
22
24
  s.homepage = 'https://github.com/kennycoc/asynchronize'
23
25
  s.license = 'MIT'
@@ -11,6 +11,17 @@ module Asynchronize
11
11
  # threads adding methods to your class.
12
12
  @@methods_asyncing = Set.new
13
13
 
14
+ ##
15
+ # Call to asynchronize a method.
16
+ # That method will be added to a list of methods to asynchronize.
17
+ # If the method already exists, it will be redefined to an asynchronous
18
+ # version. If it does not, method_missing will redefine it when it does.
19
+ # If the method is redefined afterwards, method_missing will also
20
+ # asynchronize that version.
21
+ #
22
+ # @param methods [Symbol] The methods to be asynchronized.
23
+ # @example To add any number of methods to be asynchronized.
24
+ # asynchronize :method1, :method2, :methodn
14
25
  def self.asynchronize(*methods)
15
26
  @@methods_to_async.merge(methods)
16
27
  methods.each do |method|
@@ -20,22 +31,33 @@ module Asynchronize
20
31
  end
21
32
  end
22
33
 
34
+ # require 'pry'; binding.pry
23
35
  # Save the old method_added so we don't overwrite it.
24
- if method_defined? :method_added
25
- alias_method :old_method_added, :method_added
26
- undef_method(:method_added)
36
+ if self.methods.include? :method_added
37
+ singleton_class.send(:alias_method, :old_method_added, :method_added)
38
+ singleton_class.undef_method(:method_added)
27
39
  end
28
40
 
41
+ ##
42
+ # Will asynchronize a method if it has not been asynchronized already, and
43
+ # it is in the list of methods to asynchronize. If method missing was
44
+ # already defined, it will call the previous method_missing before
45
+ # anything else Ruby calls this automatically when defining a method; it
46
+ # should not be called directly.
29
47
  def self.method_added(method)
30
48
  # Don't do anything else if we're not actually adding a new method
31
49
  return if @@methods_asyncing.include? method
32
- old_method_added(method) if method_defined? :old_method_added
50
+ @@methods_asyncing.add(method)
51
+ self.old_method_added(method) if self.methods.include? :old_method_added
33
52
  return unless @@methods_to_async.include? method
53
+ # This will delete from @@methods_asyncing
34
54
  Asynchronize.create_new_method(method, self)
35
55
  end
36
56
  end
37
57
  end
38
58
 
59
+ ##
60
+ # Responsible for actually creating the new methods and removing the old.
39
61
  def self.create_new_method(method, klass)
40
62
  klass.instance_eval do
41
63
  old_method = instance_method(method)
@@ -45,22 +67,22 @@ module Asynchronize
45
67
  undef_method method
46
68
 
47
69
  @@methods_asyncing.add(method)
48
- define_method(method) do |*args, &block|
49
- return _build_thread(old_method, args, block)
50
- end
70
+ define_method(method, Asynchronize._build_new_method(old_method))
51
71
  @@methods_asyncing.delete(method)
52
72
  @@asynced_methods.add(instance_method(method))
53
73
  end
54
74
  end
55
75
 
56
76
  private
57
- def _build_thread(old_method, args, block)
58
- return Thread.new(old_method, args, block) do |told_method, targs, tblock|
59
- result = told_method.bind(self).call(*targs)
60
- if tblock.nil?
61
- Thread.current[:return_value] = result
62
- else
63
- tblock.call(result)
77
+ def self._build_new_method(old_method)
78
+ return Proc.new do |*args, &block|
79
+ return Thread.new(old_method, args, block) do |told_method, targs, tblock|
80
+ result = told_method.bind(self).call(*targs)
81
+ if tblock.nil?
82
+ Thread.current[:return_value] = result
83
+ else
84
+ tblock.call(result)
85
+ end
64
86
  end
65
87
  end
66
88
  end
data/readme.md CHANGED
@@ -1,11 +1,12 @@
1
1
  [![Build Status](https://travis-ci.org/kennycoc/asynchronize.svg?branch=master)](https://travis-ci.org/kennycoc/asynchronize)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/30d40e270a3d7a0775a9/maintainability)](https://codeclimate.com/github/kennycoc/asynchronize/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/30d40e270a3d7a0775a9/test_coverage)](https://codeclimate.com/github/kennycoc/asynchronize/test_coverage)
2
4
  # Asynchronize
3
5
  ### The easiest way to make multiple methods asynchronous.
4
6
 
5
7
  Find yourself writing the same boilerplate for all your asynchronous methods?
6
8
  Get dryyy with asynchronize.
7
9
 
8
- There are no dependencies other than Ruby.
9
10
  Just install with `gem install asynchronize` or add to your Gemfile and `bundle`
10
11
 
11
12
  ## Usage
@@ -0,0 +1,6 @@
1
+ require 'simplecov'
2
+ require 'simplecov-console'
3
+ SimpleCov.formatter = SimpleCov::Formatter::Console
4
+ SimpleCov.start
5
+
6
+ require 'minitest/autorun'
@@ -1,4 +1,4 @@
1
- require 'minitest/autorun'
1
+ require './spec/minitest_helper.rb'
2
2
  require './lib/asynchronize.rb'
3
3
 
4
4
  class BasicSpec < Minitest::Test
@@ -82,8 +82,8 @@ class BasicSpec < Minitest::Test
82
82
 
83
83
  describe "when we call asynchronized before defining the method" do
84
84
  before do
85
- Test.asynchronize :othertest
86
85
  class Test
86
+ asynchronize :othertest
87
87
  def othertest
88
88
  return 5
89
89
  end
@@ -104,5 +104,34 @@ class BasicSpec < Minitest::Test
104
104
  temp.must_equal 5, "temp is equal to #{temp}."
105
105
  end
106
106
  end
107
+
108
+ describe "when there is an existing method_added" do
109
+ before do
110
+ if defined? AnotherTest
111
+ BasicSpec.send(:remove_const, :AnotherTest)
112
+ end
113
+ class AnotherTest
114
+ @running = false
115
+ def self.method_added(method)
116
+ return if @running
117
+ @running = true
118
+ old_method = instance_method(method)
119
+ undef_method(method)
120
+ define_method(method) do
121
+ return old_method.bind(self).call + 1
122
+ end
123
+ @running = false
124
+ end
125
+ include Asynchronize
126
+ asynchronize :test
127
+ def test
128
+ return 4
129
+ end
130
+ end
131
+ end
132
+ it "should call that method_added before, and only once." do
133
+ AnotherTest.new.test.join[:return_value].must_equal 5
134
+ end
135
+ end
107
136
  end
108
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asynchronize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenneth Cochran
@@ -21,6 +21,7 @@ files:
21
21
  - asynchronize.gemspec
22
22
  - lib/asynchronize.rb
23
23
  - readme.md
24
+ - spec/minitest_helper.rb
24
25
  - spec/spec.rb
25
26
  homepage: https://github.com/kennycoc/asynchronize
26
27
  licenses:
@@ -48,3 +49,4 @@ specification_version: 4
48
49
  summary: Easily make multiple methods asynchronous with one line of code.
49
50
  test_files:
50
51
  - spec/spec.rb
52
+ - spec/minitest_helper.rb