dalliance 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/dalliance.gemspec +8 -8
- data/lib/dalliance/version.rb +2 -2
- data/lib/dalliance.rb +31 -26
- data/spec/dalliance/asynchronous_spec.rb +32 -19
- data/spec/spec_helper.rb +3 -3
- metadata +12 -12
data/dalliance.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["eric.sullivan@annkissam.com"]
|
10
10
|
s.homepage = "https://github.com/annkissam/dalliance"
|
11
11
|
s.summary = %q{ Wrapper for an ActiveRecord model with a single ascynhronous method }
|
12
|
-
s.description = %q{
|
12
|
+
s.description = %q{ Background processing for ActiveRecord using a 'delay' method and a state_machine }
|
13
13
|
|
14
14
|
s.rubyforge_project = "dalliance"
|
15
15
|
|
@@ -17,14 +17,14 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
-
|
20
|
+
|
21
21
|
s.add_dependency('activerecord', '>= 3.0.0')
|
22
22
|
s.add_dependency('activesupport', '>= 3.0.0')
|
23
|
-
|
24
|
-
s.add_dependency('delayed_job', '>= 2.0.0')
|
23
|
+
|
25
24
|
s.add_dependency('state_machine')
|
26
|
-
|
27
|
-
s.add_development_dependency
|
28
|
-
s.add_development_dependency '
|
29
|
-
s.add_development_dependency
|
25
|
+
|
26
|
+
s.add_development_dependency('rspec')
|
27
|
+
s.add_development_dependency('delayed_job', '>= 2.0.0')
|
28
|
+
s.add_development_dependency('delayed_job_active_record')
|
29
|
+
s.add_development_dependency('sqlite3')
|
30
30
|
end
|
data/lib/dalliance/version.rb
CHANGED
data/lib/dalliance.rb
CHANGED
@@ -8,33 +8,34 @@ require 'dalliance/progress_meter'
|
|
8
8
|
|
9
9
|
module Dalliance
|
10
10
|
extend ActiveSupport::Concern
|
11
|
-
|
11
|
+
|
12
12
|
class << self
|
13
13
|
def options
|
14
14
|
@options ||= {
|
15
15
|
:background_processing => (defined?(Rails) ? Rails.env.production? : true),
|
16
|
-
:dalliance_progress_meter_total_count_method => :dalliance_progress_meter_total_count
|
16
|
+
:dalliance_progress_meter_total_count_method => :dalliance_progress_meter_total_count,
|
17
|
+
:delay_method => :delay
|
17
18
|
}
|
18
19
|
end
|
19
|
-
|
20
|
+
|
20
21
|
def background_processing?
|
21
22
|
options[:background_processing]
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
def background_processing=(value)
|
25
26
|
options[:background_processing] = value
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
def configure
|
29
30
|
yield(self) if block_given?
|
30
31
|
end
|
31
32
|
end
|
32
|
-
|
33
|
+
|
33
34
|
included do
|
34
35
|
has_one :dalliance_progress_meter, :as => :dalliance_progress_model, :class_name => '::Dalliance::ProgressMeter', :dependent => :destroy
|
35
|
-
|
36
|
+
|
36
37
|
serialize :dalliance_error_hash, Hash
|
37
|
-
|
38
|
+
|
38
39
|
#BEGIN state_machine(s)
|
39
40
|
scope :pending, where(:dalliance_status => 'pending')
|
40
41
|
scope :processing, where(:dalliance_status => 'processing')
|
@@ -65,37 +66,41 @@ module Dalliance
|
|
65
66
|
end
|
66
67
|
#END state_machine(s)
|
67
68
|
end
|
68
|
-
|
69
|
+
|
69
70
|
module ClassMethods
|
70
71
|
def dalliance_status_in_load_select_array
|
71
72
|
state_machine(:dalliance_status).states.map {|state| [state.human_name, state.name] }
|
72
73
|
end
|
73
74
|
end
|
74
|
-
|
75
|
+
|
75
76
|
def error_or_completed?
|
76
77
|
processing_error? || completed?
|
77
78
|
end
|
78
|
-
|
79
|
+
|
79
80
|
def pending_or_processing?
|
80
81
|
pending? || processing?
|
81
82
|
end
|
82
|
-
|
83
|
+
|
83
84
|
#Force backgound_processing w/ true
|
84
85
|
def dalliance_background_process(backgound_processing = nil)
|
85
86
|
if backgound_processing || (backgound_processing.nil? && Dalliance.background_processing?)
|
86
|
-
|
87
|
+
if respond_to?(self.class.dalliance_options[:delay_method])
|
88
|
+
self.send(self.class.dalliance_options[:delay_method]).dalliance_process(true)
|
89
|
+
else
|
90
|
+
raise NoMethodError.new("#{self.class.dalliance_options[:delay_method]} is undefined")
|
91
|
+
end
|
87
92
|
else
|
88
93
|
dalliance_process(false)
|
89
94
|
end
|
90
95
|
end
|
91
|
-
|
96
|
+
|
92
97
|
#backgound_processing == false will re-raise any exceptions
|
93
98
|
def dalliance_process(backgound_processing = false)
|
94
99
|
begin
|
95
100
|
start_dalliance!
|
96
101
|
|
97
102
|
build_dalliance_progress_meter(:total_count => calculate_dalliance_progress_meter_total_count).save!
|
98
|
-
|
103
|
+
|
99
104
|
self.send(self.class.dalliance_options[:dalliance_method])
|
100
105
|
|
101
106
|
finish_dalliance!
|
@@ -104,7 +109,7 @@ module Dalliance
|
|
104
109
|
self.dalliance_error_hash = {:error => e.class.name, :message => e.message, :backtrace => e.backtrace}
|
105
110
|
|
106
111
|
error_dalliance!
|
107
|
-
|
112
|
+
|
108
113
|
#Don't raise the error if we're backgound_processing...
|
109
114
|
raise e unless backgound_processing
|
110
115
|
ensure
|
@@ -115,7 +120,7 @@ module Dalliance
|
|
115
120
|
end
|
116
121
|
end
|
117
122
|
end
|
118
|
-
|
123
|
+
|
119
124
|
def dalliance_progress
|
120
125
|
if completed?
|
121
126
|
100
|
@@ -136,14 +141,14 @@ module Dalliance
|
|
136
141
|
1
|
137
142
|
end
|
138
143
|
end
|
139
|
-
|
144
|
+
|
140
145
|
module Glue
|
141
146
|
extend ActiveSupport::Concern
|
142
|
-
|
147
|
+
|
143
148
|
included do
|
144
149
|
class_attribute :dalliance_options
|
145
150
|
end
|
146
|
-
|
151
|
+
|
147
152
|
module ClassMethods
|
148
153
|
def dalliance(*args)
|
149
154
|
options = args.last.is_a?(Hash) ? Dalliance.options.merge(args.pop) : Dalliance.options
|
@@ -151,21 +156,21 @@ module Dalliance
|
|
151
156
|
case args.length
|
152
157
|
when 1
|
153
158
|
options[:dalliance_method] = args[0]
|
154
|
-
else
|
155
|
-
raise ArgumentError, "Incorrect number of Arguements provided"
|
159
|
+
else
|
160
|
+
raise ArgumentError, "Incorrect number of Arguements provided"
|
156
161
|
end
|
157
|
-
|
162
|
+
|
158
163
|
if dalliance_options.nil?
|
159
164
|
self.dalliance_options = {}
|
160
165
|
else
|
161
166
|
self.dalliance_options = self.dalliance_options.dup
|
162
167
|
end
|
163
|
-
|
168
|
+
|
164
169
|
self.dalliance_options.merge!(options)
|
165
|
-
|
170
|
+
|
166
171
|
include Dalliance
|
167
172
|
end
|
168
|
-
|
173
|
+
|
169
174
|
def dalliance_options
|
170
175
|
self.dalliance_options
|
171
176
|
end
|
@@ -2,87 +2,100 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe DallianceModel do
|
4
4
|
subject { DallianceModel.create }
|
5
|
-
|
5
|
+
|
6
6
|
before(:all) do
|
7
7
|
Dalliance.options[:background_processing] = true
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
|
+
context "no delay method" do
|
11
|
+
before(:all) do
|
12
|
+
DallianceModel.dalliance_options[:dalliance_method] = :dalliance_success_method
|
13
|
+
DallianceModel.dalliance_options[:delay_method] = :not_delay
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should raise an error" do
|
17
|
+
expect { subject.dalliance_background_process }.to raise_error(NoMethodError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
10
21
|
context "success" do
|
11
22
|
before(:all) do
|
12
23
|
DallianceModel.dalliance_options[:dalliance_method] = :dalliance_success_method
|
24
|
+
DallianceModel.dalliance_options[:delay_method] = :delay
|
13
25
|
end
|
14
26
|
|
15
27
|
it "should not call the dalliance_method w/o a Delayed::Worker" do
|
16
28
|
subject.dalliance_background_process
|
17
29
|
subject.reload
|
18
|
-
|
30
|
+
|
19
31
|
subject.should_not be_successful
|
20
32
|
Delayed::Job.count.should == 1
|
21
33
|
end
|
22
|
-
|
34
|
+
|
23
35
|
it "should call the dalliance_method w/ a Delayed::Worker" do
|
24
36
|
subject.dalliance_background_process
|
25
37
|
Delayed::Worker.new.work_off
|
26
38
|
subject.reload
|
27
|
-
|
39
|
+
|
28
40
|
subject.should be_successful
|
29
41
|
Delayed::Job.count.should == 0
|
30
42
|
end
|
31
|
-
|
43
|
+
|
32
44
|
it "should set the dalliance_status to completed" do
|
33
45
|
subject.dalliance_background_process
|
34
46
|
Delayed::Worker.new.work_off
|
35
47
|
subject.reload
|
36
|
-
|
48
|
+
|
37
49
|
subject.should be_completed
|
38
50
|
end
|
39
|
-
|
51
|
+
|
40
52
|
it "should set the dalliance_progress to 100" do
|
41
53
|
subject.dalliance_background_process
|
42
54
|
Delayed::Worker.new.work_off
|
43
55
|
subject.reload
|
44
|
-
|
56
|
+
|
45
57
|
subject.dalliance_progress.should == 100
|
46
58
|
end
|
47
59
|
end
|
48
|
-
|
60
|
+
|
49
61
|
context "raise error" do
|
50
62
|
before(:all) do
|
51
63
|
DallianceModel.dalliance_options[:dalliance_method] = :dalliance_error_method
|
64
|
+
DallianceModel.dalliance_options[:delay_method] = :delay
|
52
65
|
end
|
53
|
-
|
66
|
+
|
54
67
|
it "should NOT raise an error" do
|
55
68
|
subject.dalliance_background_process
|
56
|
-
|
69
|
+
|
57
70
|
Delayed::Worker.new.work_off
|
58
|
-
|
71
|
+
|
59
72
|
Delayed::Job.count.should == 0
|
60
73
|
end
|
61
|
-
|
74
|
+
|
62
75
|
it "should store the error" do
|
63
76
|
subject.dalliance_background_process
|
64
77
|
Delayed::Worker.new.work_off
|
65
78
|
subject.reload
|
66
|
-
|
79
|
+
|
67
80
|
subject.dalliance_error_hash.should_not be_empty
|
68
81
|
subject.dalliance_error_hash[:error].should == RuntimeError.name #We store the class name...
|
69
82
|
subject.dalliance_error_hash[:message].should == 'RuntimeError'
|
70
83
|
subject.dalliance_error_hash[:backtrace].should_not be_blank
|
71
84
|
end
|
72
|
-
|
85
|
+
|
73
86
|
it "should set the dalliance_status to processing_error" do
|
74
87
|
subject.dalliance_background_process
|
75
88
|
Delayed::Worker.new.work_off
|
76
89
|
subject.reload
|
77
|
-
|
90
|
+
|
78
91
|
subject.should be_processing_error
|
79
92
|
end
|
80
|
-
|
93
|
+
|
81
94
|
it "should set the dalliance_progress to 0" do
|
82
95
|
subject.dalliance_background_process
|
83
96
|
Delayed::Worker.new.work_off
|
84
97
|
subject.reload
|
85
|
-
|
98
|
+
|
86
99
|
subject.dalliance_progress.should == 0
|
87
100
|
end
|
88
101
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,9 +4,9 @@ require 'bundler/setup'
|
|
4
4
|
#Automatically included in a rails app...
|
5
5
|
require 'active_support'
|
6
6
|
require 'state_machine'
|
7
|
-
require 'delayed_job'
|
8
7
|
|
9
8
|
#Required for testing...
|
9
|
+
require 'delayed_job'
|
10
10
|
require 'delayed_job_active_record'
|
11
11
|
|
12
12
|
require 'dalliance'
|
@@ -17,12 +17,12 @@ RSpec.configure do |config|
|
|
17
17
|
ActiveRecord::Base.connection.begin_db_transaction
|
18
18
|
ActiveRecord::Base.connection.increment_open_transactions
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
config.after do
|
22
22
|
if ActiveRecord::Base.connection.open_transactions != 0
|
23
23
|
ActiveRecord::Base.connection.rollback_db_transaction
|
24
24
|
ActiveRecord::Base.connection.decrement_open_transactions
|
25
|
-
end
|
25
|
+
end
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dalliance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -44,13 +44,13 @@ dependencies:
|
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 3.0.0
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
|
-
name:
|
47
|
+
name: state_machine
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
51
|
- - ! '>='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: '0'
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,16 +58,16 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: rspec
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
67
67
|
- - ! '>='
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
|
-
type: :
|
70
|
+
type: :development
|
71
71
|
prerelease: false
|
72
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
@@ -76,13 +76,13 @@ dependencies:
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
79
|
+
name: delayed_job
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
81
81
|
none: false
|
82
82
|
requirements:
|
83
83
|
- - ! '>='
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
-
version:
|
85
|
+
version: 2.0.0
|
86
86
|
type: :development
|
87
87
|
prerelease: false
|
88
88
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -90,7 +90,7 @@ dependencies:
|
|
90
90
|
requirements:
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
93
|
+
version: 2.0.0
|
94
94
|
- !ruby/object:Gem::Dependency
|
95
95
|
name: delayed_job_active_record
|
96
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,8 +123,8 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
-
description: ! '
|
127
|
-
|
126
|
+
description: ! ' Background processing for ActiveRecord using a ''delay'' method and
|
127
|
+
a state_machine '
|
128
128
|
email:
|
129
129
|
- eric.sullivan@annkissam.com
|
130
130
|
executables: []
|