take_care 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTY1ZjQyYjYxYmJkN2NiOGM0YzZmOGNjYWIzOGRmZDRhZjY2ZTFiYQ==
5
+ data.tar.gz: !binary |-
6
+ OGVkMWJhNTY5YTM5YTE0MTkzNjBlZTQ3YjY4YjMxYjliN2I2ZWJlNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZjQ3Y2Q2ODM0MmU1MDM0MTMzYzU4NzRiNjRjNWZlZTYzMWUyNjU5N2ZiZDdl
10
+ MGI2ZjgzOWNjNTBmOTkwZGQ5YzdlMGUxODRjZWVlMGYxYjUwMjJkZWVkMjA5
11
+ ZDhjMzRjNDY5ZjFlMjE1NWMyNGI0OTMyZDY1NWQ4YmNkZDE4NDA=
12
+ data.tar.gz: !binary |-
13
+ NGQ1MDM3M2VhZmFmMjJiOWJjMWI5YTNkOTU0MjgzMzAzMmE3ZjdlZDU3ZWI0
14
+ Yzg1ZjIyZGVkY2IzMWI0MzBkY2FmMGIxODVlZGUyMDhhYmViMjBlYjg2NWY4
15
+ ZDJlYWYwYmY5MjM1OTdlNTI2ZWJkZTU0NmE0NGQ1YmY4Y2Q3MTQ=
data/README.md CHANGED
@@ -44,6 +44,28 @@ human.take_care_of_hard_work 'heavy box', 'second heavy box'
44
44
 
45
45
  ```
46
46
 
47
+ There is also support for class methods for all kind of classes (not only ActiveRecord::Base).
48
+
49
+ ```rb
50
+ class Human
51
+ include TakeCare::Reliable
52
+
53
+ def self.do_stuff(s1, s2)
54
+ # Code that needs to be executed in background
55
+ end
56
+ end
57
+
58
+ Human.take_care :do_stuff, 'arg1', 'arg2' # This goes to sidekiq
59
+
60
+ Human.take_care_of :do_stuff, 'arg1', 'arg2' # Same using alias
61
+
62
+ # Same using dynamic methods (method is defined after first call)
63
+ Human.take_care_do_stuff 'arg1', 'arg2'
64
+ # Or
65
+ Human.take_care_of_do_stuff 'arg1', 'arg2'
66
+
67
+ ```
68
+
47
69
  ## Contributing
48
70
 
49
71
  1. Fork it
@@ -1,5 +1,7 @@
1
1
  module TakeCare
2
2
  module Reliable
3
+ extend ActiveSupport::Concern
4
+
3
5
  def take_care(method_name, *args)
4
6
  TakeCare::Worker.perform_async(self.class.to_s, self.id, method_name, *args)
5
7
  end
@@ -15,10 +17,34 @@ module TakeCare
15
17
  raise NoMethodError, "private method `#{method_name}' called for #{self}"
16
18
  end
17
19
  return super unless /\Atake_care_(?:of_)?(.+)\Z/ =~ method_name && self.respond_to?($1)
18
- self.class.__send__(:define_method, method_name) do |*args|
19
- self.take_care($1.to_sym, *args)
20
+ self.class.__send__(:define_method, method_name) do |*d_args|
21
+ self.take_care($1.to_sym, *d_args)
20
22
  end
21
23
  self.take_care($1.to_sym, *args)
22
24
  end
25
+
26
+ module ClassMethods
27
+ def take_care(method_name, *args)
28
+ TakeCare::WorkerC.perform_async(self.to_s, method_name, *args)
29
+ end
30
+ alias_method :take_care_of, :take_care
31
+
32
+ def respond_to?(method_name, include_private = false)
33
+ return include_private if self.class.private_method_defined?(method_name)
34
+ /\Atake_care_(?:of_)?(.+)\Z/ =~ method_name && self.respond_to?($1) || super
35
+ end
36
+
37
+ def method_missing(method_name, *args, &block)
38
+ if self.class.private_method_defined?(method_name)
39
+ raise NoMethodError, "private method `#{method_name}' called for #{self}"
40
+ end
41
+ return super unless /\Atake_care_(?:of_)?(.+)\Z/ =~ method_name && self.respond_to?($1)
42
+ eigenclass = class << self; self; end
43
+ eigenclass.__send__(:define_method, method_name) do |*d_args|
44
+ self.take_care($1.to_sym, *d_args)
45
+ end
46
+ self.take_care($1.to_sym, *args)
47
+ end
48
+ end
23
49
  end
24
50
  end
@@ -1,3 +1,3 @@
1
1
  module TakeCare
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,12 @@
1
+ require "sidekiq"
2
+ require "active_support"
3
+
4
+ module TakeCare
5
+ class WorkerC
6
+ include ::Sidekiq::Worker
7
+
8
+ def perform(class_name, class_method, *args)
9
+ class_name.constantize.__send__(class_method, *args)
10
+ end
11
+ end
12
+ end
data/lib/take_care.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "take_care/version"
2
2
  require "take_care/worker"
3
+ require "take_care/worker_c"
3
4
  require "take_care/reliable"
4
5
 
5
6
  module TakeCare
@@ -10,6 +10,19 @@ module HumanMacros
10
10
 
11
11
  def hard_work(box1, box2)
12
12
  end
13
+
14
+ private
15
+ def take_care_hard_work_in_guts(box1, box2)
16
+ end
17
+
18
+ class << self
19
+ def do_stuff(garbage1, garbage2)
20
+ end
21
+
22
+ private
23
+ def take_care_do_stuff_in_guts(garbage1, garbage2)
24
+ end
25
+ end
13
26
  end)
14
27
  end
15
28
  end
@@ -2,59 +2,114 @@ require "spec_helper"
2
2
 
3
3
  describe TakeCare::Reliable do
4
4
  redefine_human_class
5
- subject { Human.new }
5
+ context "Instance methods" do
6
+ subject { Human.new }
6
7
 
7
- describe "#take_care" do
8
- it "delegates method calls to Worker" do
9
- TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2)
10
- subject.take_care :hard_work, :box1, :box2
11
- end
12
- end
13
-
14
- describe "#take_care_of (alias of #take_care)" do
15
- it "delegates method calls to Worker" do
16
- TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2)
17
- subject.take_care_of :hard_work, :box1, :box2
8
+ describe "#take_care" do
9
+ it "delegates method calls to Worker" do
10
+ TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2)
11
+ subject.take_care :hard_work, :box1, :box2
12
+ end
18
13
  end
19
- end
20
14
 
21
- context 'Dynamic methods "take_care_..."' do
22
- describe "#method_missing" do
15
+ describe "#take_care_of (alias of #take_care)" do
23
16
  it "delegates method calls to Worker" do
24
17
  TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2)
25
- subject.take_care_hard_work :box1, :box2
18
+ subject.take_care_of :hard_work, :box1, :box2
26
19
  end
20
+ end
21
+
22
+ context 'Dynamic methods "take_care_..."' do
23
+ describe "#method_missing" do
24
+ it "delegates method calls to Worker" do
25
+ TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2)
26
+ subject.take_care_hard_work :box1, :box2
27
+ end
27
28
 
28
- it "defines dynamic method after first call (method missing should not be called after that)" do
29
- TakeCare::Worker.stub(:perform_async)
30
- subject.take_care_hard_work :box1, :box2
31
- Human.instance_methods(false).should be_include :take_care_hard_work
29
+ it "defines dynamic method after first call (method missing should not be called after that)" do
30
+ TakeCare::Worker.stub(:perform_async)
31
+ subject.take_care_hard_work :box1, :box2
32
+ Human.instance_methods(false).should be_include :take_care_hard_work
33
+ end
34
+
35
+ it "second time acts same as first (dynamically defined method)" do
36
+ TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2).twice
37
+ subject.take_care_hard_work :box1, :box2
38
+ subject.take_care_hard_work :box1, :box2
39
+ end
40
+
41
+ it "raises error if exists private method with same name" do
42
+ expect { subject.take_care_hard_work_in_guts :box1, :box2 }.to raise_error NameError
43
+ end
32
44
  end
33
45
 
34
- it "second time acts same as first (dynamically defined method)" do
35
- TakeCare::Worker.should_receive(:perform_async).with("Human", 4, :hard_work, :box1, :box2).twice
36
- subject.take_care_hard_work :box1, :box2
37
- subject.take_care_hard_work :box1, :box2
46
+ describe "#respond_to?" do
47
+ it { should be_respond_to :take_care_hard_work }
48
+ it { should be_respond_to :take_care_of_hard_work }
49
+
50
+ it "is false if exists private method with such name" do
51
+ should_not be_respond_to :take_care_hard_work_in_guts
52
+ end
53
+
54
+ it "is true if exists private method with such name and we are checking for private methods as well" do
55
+ should be_respond_to(:take_care_hard_work_in_guts, true)
56
+ end
38
57
  end
58
+ end
59
+ end
39
60
 
40
- it "raises error if exists private method with same name" do
41
- Human.class_eval { private; def take_care_hard_work; end }
42
- expect { subject.take_care_hard_work :box1, :box2 }.to raise_error NameError
61
+ context "Class methods" do
62
+ subject { Human }
63
+
64
+ describe "#take_care" do
65
+ it "delegates class method calls to WorkerC" do
66
+ TakeCare::WorkerC.should_receive(:perform_async).with("Human", :do_stuff, :s1, :s2)
67
+ subject.take_care :do_stuff, :s1, :s2
43
68
  end
44
69
  end
45
70
 
46
- describe "#respond_to?" do
47
- it { should be_respond_to :take_care_hard_work }
48
- it { should be_respond_to :take_care_of_hard_work }
71
+ describe "#take_care_of (alias of #take_care)" do
72
+ it "delegates class method calls to WorkerC" do
73
+ TakeCare::WorkerC.should_receive(:perform_async).with("Human", :do_stuff, :s1, :s2)
74
+ subject.take_care_of :do_stuff, :s1, :s2
75
+ end
76
+ end
49
77
 
50
- it "is false if exists private method with such name" do
51
- Human.class_eval { private; def take_care_hard_work; end }
52
- should_not be_respond_to :take_care_hard_work
78
+ context 'Dynamic methods "take_care_..."' do
79
+ describe "#method_missing" do
80
+ it "delegates method calls to WorkerC" do
81
+ TakeCare::WorkerC.should_receive(:perform_async).with("Human", :do_stuff, :s1, :s2)
82
+ subject.take_care_do_stuff :s1, :s2
83
+ end
84
+
85
+ it "defines dynamic method after first call (method missing should not be called after that)" do
86
+ TakeCare::WorkerC.stub(:perform_async)
87
+ subject.take_care_do_stuff :s1, :s2
88
+ subject.methods(false).should be_include :take_care_do_stuff
89
+ end
90
+
91
+ it "second time acts same as first (dynamically defined method)" do
92
+ TakeCare::WorkerC.should_receive(:perform_async).with("Human", :do_stuff, :s1, :s2).twice
93
+ subject.take_care_do_stuff :s1, :s2
94
+ subject.take_care_do_stuff :s1, :s2
95
+ end
96
+
97
+ it "raises error if exists private method with same name" do
98
+ expect { subject.take_care_do_stuff_in_guts :s1, :s2 }.to raise_error NameError
99
+ end
53
100
  end
54
101
 
55
- it "is true if exists private method with such name and we are checking for private methods as well" do
56
- Human.class_eval { private; def take_care_hard_work; end }
57
- should be_respond_to :take_care_hard_work, true
102
+ describe "#respond_to?" do
103
+ it { should be_respond_to :take_care_do_stuff }
104
+ it { should be_respond_to :take_care_of_do_stuff }
105
+
106
+ it "is false if exists private method with such name" do
107
+ should_not be_respond_to :take_care_do_stuff_in_guts
108
+ end
109
+
110
+ it "is true if exists private method with such name and we are checking for private methods as well" do
111
+ should be_respond_to :take_care_do_stuff_in_guts, true
112
+ end
58
113
  end
59
114
  end
60
115
  end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ describe TakeCare::WorkerC do
4
+ redefine_human_class
5
+
6
+ describe '#perform' do
7
+ it "should include Sidekiq::Worker" do
8
+ described_class.ancestors.should include Sidekiq::Worker
9
+ end
10
+
11
+ it "calls class method with received args" do
12
+ Human.should_receive(:do_stuff).with(:s1, :s2)
13
+ subject.perform("Human", :do_stuff, :s1, :s2)
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: take_care
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alexander Avoyants
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-29 00:00:00.000000000 Z
11
+ date: 2014-01-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: sidekiq
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: bundler
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -107,39 +96,41 @@ files:
107
96
  - lib/take_care/reliable.rb
108
97
  - lib/take_care/version.rb
109
98
  - lib/take_care/worker.rb
99
+ - lib/take_care/worker_c.rb
110
100
  - spec/spec_helper.rb
111
101
  - spec/support/human.rb
112
102
  - spec/take_care/reliable_spec.rb
103
+ - spec/take_care/worker_c_spec.rb
113
104
  - spec/take_care/worker_spec.rb
114
105
  - take_care.gemspec
115
106
  homepage: ''
116
107
  licenses:
117
108
  - MIT
109
+ metadata: {}
118
110
  post_install_message:
119
111
  rdoc_options: []
120
112
  require_paths:
121
113
  - lib
122
114
  required_ruby_version: !ruby/object:Gem::Requirement
123
- none: false
124
115
  requirements:
125
116
  - - ! '>='
126
117
  - !ruby/object:Gem::Version
127
118
  version: '0'
128
119
  required_rubygems_version: !ruby/object:Gem::Requirement
129
- none: false
130
120
  requirements:
131
121
  - - ! '>='
132
122
  - !ruby/object:Gem::Version
133
123
  version: '0'
134
124
  requirements: []
135
125
  rubyforge_project:
136
- rubygems_version: 1.8.23
126
+ rubygems_version: 2.2.0
137
127
  signing_key:
138
- specification_version: 3
128
+ specification_version: 4
139
129
  summary: Sidekiq wrapper for activerecord model (any class which instances fetched
140
130
  by id)
141
131
  test_files:
142
132
  - spec/spec_helper.rb
143
133
  - spec/support/human.rb
144
134
  - spec/take_care/reliable_spec.rb
135
+ - spec/take_care/worker_c_spec.rb
145
136
  - spec/take_care/worker_spec.rb