rules_engine 0.1.0 → 0.1.1

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,11 +1,13 @@
1
1
  module RulesEngine
2
2
  class Job
3
-
3
+
4
4
  @@max_rules = 500
5
- attr_reader :re_job, :re_pipeline, :re_rule
5
+ attr_reader :re_job, :re_pipeline, :re_rule
6
+ attr_accessor :audit_level
6
7
 
7
8
  def initialize(re_job)
8
9
  @re_job = re_job
10
+ @audit_level = ReJobAudit::AUDIT_INFO
9
11
  end
10
12
 
11
13
  def self.create()
@@ -109,6 +111,7 @@ module RulesEngine
109
111
  end
110
112
 
111
113
  def audit(message, code=ReJobAudit::AUDIT_INFO)
114
+ if audit_level != ReJobAudit::AUDIT_NONE && code >= audit_level
112
115
  ReJobAudit.create({
113
116
  :re_job_id => re_job ? re_job.id : nil,
114
117
  :re_pipeline_id => re_pipeline ? re_pipeline.id : nil,
@@ -116,7 +119,7 @@ module RulesEngine
116
119
  :audit_date => Time.now,
117
120
  :audit_code => code,
118
121
  :audit_message => message});
119
-
122
+ end
120
123
  # puts "#{'*' * 5} #{re_job ? re_job.id : nil}, #{re_pipeline ? re_pipeline.id : nil}, #{re_rule ? re_rule.id : nil}, #{code}, #{message}"
121
124
  end
122
125
 
@@ -41,7 +41,7 @@ module RulesEngine
41
41
  end
42
42
 
43
43
  def expected_outcomes
44
- return [:outcome => RulesEngine::RuleOutcome::OUTCOME_NEXT, :pipeline_code => nil]
44
+ [:outcome => RulesEngine::RuleOutcome::OUTCOME_NEXT]
45
45
  end
46
46
 
47
47
  ##################################################################
@@ -72,13 +72,12 @@ module RulesEngine
72
72
  # execute the rule
73
73
  # return an RulesEngine::RuleOutcome object to define what to do next
74
74
  # if nil to continue to the next rule
75
- def process(job_id, data)
76
- rule_outcome = RulesEngine::RuleOutcome.new
77
-
78
- rule_outcome.outcome = RulesEngine::RuleOutcome::OUTCOME_NEXT
79
- # rule_outcome.pipeline_code =
80
-
81
- rule_outcome
75
+ def process(job, data)
76
+ job.audit("process #{title}", ReJobAudit::AUDIT_INFO)
77
+ # RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_STOP_SUCCESS)
78
+ # RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_STOP_FAILURE)
79
+ # RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_START_PIPELINE, 'next_pipeline')
80
+ RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_NEXT)
82
81
  end
83
82
 
84
83
  end
@@ -1,6 +1,7 @@
1
1
  class ReJobAudit < ActiveRecord::Base
2
-
3
- AUDIT_INFO = 0
2
+
3
+ AUDIT_NONE = -1
4
+ AUDIT_INFO = 0
4
5
  AUDIT_SUCCESS = 1
5
6
  AUDIT_FAILURE = 2
6
7
 
@@ -25,7 +25,7 @@
25
25
  <%= yield :defer_re_javascript_include %>
26
26
  </head>
27
27
  <body>
28
- <div class="container">
28
+ <div class="container re_container">
29
29
  <%#= render :partial => '/layouts/header' %>
30
30
 
31
31
  <%= yield :defer_re_breadcrumbs %>
@@ -10,7 +10,10 @@ html, body {
10
10
 
11
11
  .container {
12
12
  background: #fff;
13
- min-height: 700px;
13
+ }
14
+
15
+ .re_container {
16
+ min-height: 700px;
14
17
  }
15
18
 
16
19
  h1, h2, h3, h4, h5, h6 {
@@ -7,10 +7,14 @@ class ReJob
7
7
  JOB_STATUS_FAILURE = 4567
8
8
  end
9
9
 
10
+ class RePipeline
11
+ end
12
+
10
13
  class RePipelineActivated
11
14
  end
12
15
 
13
16
  class ReJobAudit
17
+ AUDIT_NONE = -1
14
18
  AUDIT_INFO = 1234
15
19
  AUDIT_SUCCESS = 2345
16
20
  AUDIT_FAILURE = 3456
@@ -70,10 +74,7 @@ describe "RulesEngine::Job" do
70
74
  end
71
75
 
72
76
  describe "run" do
73
- # def run_pipeline
74
- # RulesEngine::JobRunner.run_pipleine(1001, 'mock pipeline code',{:data_key => "data value"})
75
- # end
76
- #
77
+
77
78
  before(:each) do
78
79
  @re_job = ReJob.new
79
80
  @re_job.stub!(:id).and_return(1001)
@@ -83,6 +84,7 @@ describe "RulesEngine::Job" do
83
84
  @re_pipeline_activated = RePipelineActivated.new
84
85
  @re_pipeline_activated.stub!(:id)
85
86
  RePipelineActivated.stub!(:find_by_code).and_return(@re_pipeline_activated)
87
+ RePipeline.stub!(:find_by_code).and_return(@re_pipeline_activated)
86
88
 
87
89
  @re_pipeline = mock("RePipeline")
88
90
  @re_pipeline.stub!(:id).and_return(2001)
@@ -145,8 +147,7 @@ describe "RulesEngine::Job" do
145
147
  end
146
148
 
147
149
  it "should process all of the rules if the outcome is OUTCOME_NEXT" do
148
- rule_outcome = RulesEngine::RuleOutcome.new
149
- rule_outcome.outcome = RulesEngine::RuleOutcome::OUTCOME_NEXT
150
+ rule_outcome = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_NEXT)
150
151
 
151
152
  @rule_1.should_receive(:process).and_return(rule_outcome)
152
153
  @rule_2.should_receive(:process).and_return(rule_outcome)
@@ -155,8 +156,7 @@ describe "RulesEngine::Job" do
155
156
  end
156
157
 
157
158
  it "should not process rule 2 if rule 1 is OUTCOME_STOP_FAILURE" do
158
- rule_outcome = RulesEngine::RuleOutcome.new
159
- rule_outcome.outcome = RulesEngine::RuleOutcome::OUTCOME_STOP_FAILURE
159
+ rule_outcome = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_STOP_FAILURE)
160
160
 
161
161
  @rule_1.should_receive(:process).and_return(rule_outcome)
162
162
  @rule_2.should_not_receive(:process)
@@ -165,8 +165,7 @@ describe "RulesEngine::Job" do
165
165
  end
166
166
 
167
167
  it "should not process rule 2 if rule 1 is OUTCOME_STOP_SUCCESS" do
168
- rule_outcome = RulesEngine::RuleOutcome.new
169
- rule_outcome.outcome = RulesEngine::RuleOutcome::OUTCOME_STOP_SUCCESS
168
+ rule_outcome = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_STOP_SUCCESS)
170
169
 
171
170
  @rule_1.should_receive(:process).and_return(rule_outcome)
172
171
  @rule_2.should_not_receive(:process)
@@ -175,26 +174,17 @@ describe "RulesEngine::Job" do
175
174
  end
176
175
 
177
176
  it "should start a new pipeline if rule 1 is OUTCOME_START_PIPELINE" do
178
- rule_outcome_pipeline = RulesEngine::RuleOutcome.new
179
- rule_outcome_pipeline.outcome = RulesEngine::RuleOutcome::OUTCOME_START_PIPELINE
180
- rule_outcome_pipeline.pipeline_code = "next pipeline"
181
-
182
- rule_outcome_stop = RulesEngine::RuleOutcome.new
183
- rule_outcome_stop.outcome = RulesEngine::RuleOutcome::OUTCOME_STOP_SUCCESS
184
-
185
- RePipelineActivated.should_receive(:find_by_code).with("next pipeline").and_return(@re_pipeline_activated)
177
+ rule_outcome_pipeline = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_START_PIPELINE, "next pipeline")
178
+ rule_outcome_stop = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_STOP_SUCCESS)
186
179
 
180
+ RePipelineActivated.should_receive(:find_by_code).with("next pipeline").and_return(@re_pipeline_activated)
187
181
  @rule_1.should_receive(:process).and_return(rule_outcome_pipeline, rule_outcome_stop)
188
- @rule_2.should_not_receive(:process)
189
182
 
190
183
  RulesEngine::Job.create.run("test").should == true
191
184
  end
192
185
 
193
186
  it "should stop if we are starting too many pipelines" do
194
- rule_outcome = RulesEngine::RuleOutcome.new
195
- rule_outcome.outcome = RulesEngine::RuleOutcome::OUTCOME_START_PIPELINE
196
- rule_outcome.pipeline_code = "next pipeline"
197
-
187
+ rule_outcome = RulesEngine::RuleOutcome.new(RulesEngine::RuleOutcome::OUTCOME_START_PIPELINE, "next pipeline")
198
188
  @rule_1.stub!(:process).and_return(rule_outcome)
199
189
  @rule_2.should_not_receive(:process)
200
190
 
@@ -206,7 +196,6 @@ describe "RulesEngine::Job" do
206
196
  before(:each) do
207
197
  @re_job = ReJob.new
208
198
  @re_job.stub!(:id).and_return(1001)
209
- # @re_job.stub(:update_attributes)
210
199
  ReJob.stub!(:create).and_return(@re_job)
211
200
  end
212
201
 
@@ -219,12 +208,12 @@ describe "RulesEngine::Job" do
219
208
  :re_pipeline_id => nil,
220
209
  :re_rule_id => nil,
221
210
  :audit_date => now,
222
- :audit_code => "mock_code",
211
+ :audit_code => ReJobAudit::AUDIT_INFO,
223
212
  :audit_message => "mock_message"
224
213
  ))
225
214
 
226
215
  job = RulesEngine::Job.create
227
- job.audit("mock_message", "mock_code")
216
+ job.audit("mock_message", ReJobAudit::AUDIT_INFO)
228
217
  end
229
218
 
230
219
  it "should set the re_pipeline_id" do
@@ -236,13 +225,13 @@ describe "RulesEngine::Job" do
236
225
  :re_pipeline_id => 2001,
237
226
  :re_rule_id => nil,
238
227
  :audit_date => now,
239
- :audit_code => "mock_code",
228
+ :audit_code => ReJobAudit::AUDIT_INFO,
240
229
  :audit_message => "mock_message"
241
230
  ))
242
231
 
243
232
  job = RulesEngine::Job.create
244
233
  job.stub!(:re_pipeline).and_return(mock("RePipeline", :id => 2001))
245
- job.audit("mock_message", "mock_code")
234
+ job.audit("mock_message", ReJobAudit::AUDIT_INFO)
246
235
  end
247
236
 
248
237
  it "should set the re_rule_id" do
@@ -254,13 +243,81 @@ describe "RulesEngine::Job" do
254
243
  :re_pipeline_id => nil,
255
244
  :re_rule_id => 3001,
256
245
  :audit_date => now,
257
- :audit_code => "mock_code",
246
+ :audit_code => ReJobAudit::AUDIT_INFO,
258
247
  :audit_message => "mock_message"
259
248
  ))
260
249
 
261
250
  job = RulesEngine::Job.create
262
251
  job.stub!(:re_rule).and_return(mock("ReRule", :id => 3001))
263
- job.audit("mock_message", "mock_code")
252
+ job.audit("mock_message", ReJobAudit::AUDIT_INFO)
264
253
  end
254
+
255
+ describe "audit levels" do
256
+ describe "job audit level ReJobAudit::AUDIT_INFO" do
257
+ before(:each) do
258
+ @job = RulesEngine::Job.create
259
+ @job.audit_level = ReJobAudit::AUDIT_INFO
260
+ end
261
+
262
+ it "should audit the at level AUDIT_INFO" do
263
+ ReJobAudit.should_receive(:create)
264
+ @job.audit("mock_message", ReJobAudit::AUDIT_INFO)
265
+ end
266
+
267
+ it "should audit the at level AUDIT_SUCCESS" do
268
+ ReJobAudit.should_receive(:create)
269
+ @job.audit("mock_message", ReJobAudit::AUDIT_SUCCESS)
270
+ end
271
+
272
+ it "should audit the at level AUDIT_FAILURE" do
273
+ ReJobAudit.should_receive(:create)
274
+ @job.audit("mock_message", ReJobAudit::AUDIT_FAILURE)
275
+ end
276
+ end
277
+
278
+ describe "job audit level ReJobAudit::AUDIT_SUCCESS" do
279
+ before(:each) do
280
+ @job = RulesEngine::Job.create
281
+ @job.audit_level = ReJobAudit::AUDIT_SUCCESS
282
+ end
283
+
284
+ it "should not audit the at level AUDIT_INFO" do
285
+ ReJobAudit.should_not_receive(:create)
286
+ @job.audit("mock_message", ReJobAudit::AUDIT_INFO)
287
+ end
288
+
289
+ it "should audit the at level AUDIT_SUCCESS" do
290
+ ReJobAudit.should_receive(:create)
291
+ @job.audit("mock_message", ReJobAudit::AUDIT_SUCCESS)
292
+ end
293
+
294
+ it "should audit the at level AUDIT_FAILURE" do
295
+ ReJobAudit.should_receive(:create)
296
+ @job.audit("mock_message", ReJobAudit::AUDIT_FAILURE)
297
+ end
298
+ end
299
+
300
+ describe "job audit level ReJobAudit::AUDIT_FAILURE" do
301
+ before(:each) do
302
+ @job = RulesEngine::Job.create
303
+ @job.audit_level = ReJobAudit::AUDIT_FAILURE
304
+ end
305
+
306
+ it "should not audit the at level AUDIT_INFO" do
307
+ ReJobAudit.should_not_receive(:create)
308
+ @job.audit("mock_message", ReJobAudit::AUDIT_INFO)
309
+ end
310
+
311
+ it "should not audit the at level AUDIT_FAILURE" do
312
+ ReJobAudit.should_not_receive(:create)
313
+ @job.audit("mock_message", ReJobAudit::AUDIT_SUCCESS)
314
+ end
315
+
316
+ it "should audit the at level AUDIT_FAILURE" do
317
+ ReJobAudit.should_receive(:create)
318
+ @job.audit("mock_message", ReJobAudit::AUDIT_FAILURE)
319
+ end
320
+ end
321
+ end
265
322
  end
266
323
  end
@@ -64,12 +64,16 @@ describe "RulesEngine::Rule" do
64
64
  end
65
65
 
66
66
  describe "processing a rule" do
67
+ before(:each) do
68
+ @job = mock("job")
69
+ @job.stub(:audit)
70
+ end
67
71
  it "should return a rule_outcome" do
68
- MockRule.new.process(1, {}).should be_instance_of(RulesEngine::RuleOutcome)
72
+ MockRule.new.process(@job, {}).should be_instance_of(RulesEngine::RuleOutcome)
69
73
  end
70
74
 
71
75
  it "should set the outcome to OUTCOME_NEXT by default" do
72
- MockRule.new.process(1, {}).outcome.should == RulesEngine::RuleOutcome::OUTCOME_NEXT
76
+ MockRule.new.process(@job, {}).outcome.should == RulesEngine::RuleOutcome::OUTCOME_NEXT
73
77
  end
74
78
  end
75
79
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rules_engine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Douglas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-25 00:00:00 +10:00
18
+ date: 2010-06-28 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency