statesman 3.4.1 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +9 -9
  3. data/.rubocop_todo.yml +67 -2
  4. data/CHANGELOG.md +7 -0
  5. data/Gemfile +0 -4
  6. data/README.md +15 -1
  7. data/lib/generators/statesman/active_record_transition_generator.rb +1 -1
  8. data/lib/generators/statesman/migration_generator.rb +1 -1
  9. data/lib/generators/statesman/mongoid_transition_generator.rb +1 -1
  10. data/lib/generators/statesman/templates/create_migration.rb.erb +3 -3
  11. data/lib/generators/statesman/templates/update_migration.rb.erb +2 -2
  12. data/lib/statesman/adapters/active_record.rb +3 -3
  13. data/lib/statesman/adapters/active_record_queries.rb +7 -7
  14. data/lib/statesman/adapters/memory.rb +2 -2
  15. data/lib/statesman/adapters/mongoid.rb +1 -1
  16. data/lib/statesman/config.rb +0 -1
  17. data/lib/statesman/machine.rb +1 -1
  18. data/lib/statesman/version.rb +1 -1
  19. data/spec/generators/statesman/active_record_transition_generator_spec.rb +14 -9
  20. data/spec/generators/statesman/migration_generator_spec.rb +9 -9
  21. data/spec/generators/statesman/mongoid_transition_generator_spec.rb +7 -5
  22. data/spec/statesman/adapters/active_record_queries_spec.rb +17 -15
  23. data/spec/statesman/adapters/active_record_spec.rb +14 -4
  24. data/spec/statesman/adapters/memory_spec.rb +1 -0
  25. data/spec/statesman/{transition_spec.rb → adapters/memory_transition_spec.rb} +0 -0
  26. data/spec/statesman/adapters/mongoid_spec.rb +5 -1
  27. data/spec/statesman/adapters/shared_examples.rb +18 -9
  28. data/spec/statesman/callback_spec.rb +18 -6
  29. data/spec/statesman/config_spec.rb +6 -3
  30. data/spec/statesman/guard_spec.rb +3 -1
  31. data/spec/statesman/machine_spec.rb +52 -14
  32. data/spec/support/generators_shared_examples.rb +3 -1
  33. data/statesman.gemspec +12 -12
  34. metadata +23 -23
@@ -1,14 +1,17 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Statesman::Config do
4
- let(:instance) { Statesman::Config.new }
4
+ let(:instance) { described_class.new }
5
+
5
6
  after { Statesman.configure { storage_adapter(Statesman::Adapters::Memory) } }
6
7
 
7
8
  describe "#storage_adapter" do
8
- let(:adapter) { Class.new }
9
- before { instance.storage_adapter(adapter) }
10
9
  subject { instance.adapter_class }
11
10
 
11
+ let(:adapter) { Class.new }
12
+
13
+ before { instance.storage_adapter(adapter) }
14
+
12
15
  it { is_expected.to be(adapter) }
13
16
 
14
17
  it "is DSL configurable" do
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Statesman::Guard do
4
4
  let(:callback) { -> {} }
5
- let(:guard) { Statesman::Guard.new(from: nil, to: nil, callback: callback) }
5
+ let(:guard) { described_class.new(from: nil, to: nil, callback: callback) }
6
6
 
7
7
  specify { expect(guard).to be_a(Statesman::Callback) }
8
8
 
@@ -11,11 +11,13 @@ describe Statesman::Guard do
11
11
 
12
12
  context "success" do
13
13
  let(:callback) { -> { true } }
14
+
14
15
  specify { expect { call }.to_not raise_error }
15
16
  end
16
17
 
17
18
  context "error" do
18
19
  let(:callback) { -> { false } }
20
+
19
21
  specify { expect { call }.to raise_error(Statesman::GuardFailedError) }
20
22
  end
21
23
  end
@@ -6,11 +6,14 @@ describe Statesman::Machine do
6
6
 
7
7
  describe ".state" do
8
8
  before { machine.state(:x) }
9
+
9
10
  before { machine.state(:y) }
11
+
10
12
  specify { expect(machine.states).to eq(%w[x y]) }
11
13
 
12
14
  context "initial" do
13
15
  before { machine.state(:x, initial: true) }
16
+
14
17
  specify { expect(machine.initial_state).to eq("x") }
15
18
 
16
19
  context "when an initial state is already defined" do
@@ -23,6 +26,12 @@ describe Statesman::Machine do
23
26
  end
24
27
 
25
28
  describe ".retry_conflicts" do
29
+ subject(:transition_state) do
30
+ described_class.retry_conflicts(retry_attempts) do
31
+ instance.transition_to(:y)
32
+ end
33
+ end
34
+
26
35
  before do
27
36
  machine.class_eval do
28
37
  state :x, initial: true
@@ -32,15 +41,10 @@ describe Statesman::Machine do
32
41
  transition from: :y, to: :z
33
42
  end
34
43
  end
44
+
35
45
  let(:instance) { machine.new(my_model) }
36
46
  let(:retry_attempts) { 2 }
37
47
 
38
- subject(:transition_state) do
39
- Statesman::Machine.retry_conflicts(retry_attempts) do
40
- instance.transition_to(:y)
41
- end
42
- end
43
-
44
48
  context "when no exception occurs" do
45
49
  it "runs the transition once" do
46
50
  expect(instance).to receive(:transition_to).once
@@ -248,26 +252,31 @@ describe Statesman::Machine do
248
252
  context "with invalid states" do
249
253
  context "when both are invalid" do
250
254
  let(:options) { { from: :foo, to: :bar } }
255
+
251
256
  it_behaves_like "fails", Statesman::InvalidStateError
252
257
  end
253
258
 
254
259
  context "from a terminal state to anything" do
255
260
  let(:options) { { from: :y, to: [] } }
261
+
256
262
  it_behaves_like "fails", Statesman::InvalidTransitionError
257
263
  end
258
264
 
259
265
  context "to an initial state and from anything" do
260
266
  let(:options) { { from: nil, to: :x } }
267
+
261
268
  it_behaves_like "fails", Statesman::InvalidTransitionError
262
269
  end
263
270
 
264
271
  context "from a terminal state and to multiple states" do
265
272
  let(:options) { { from: :y, to: %i[x z] } }
273
+
266
274
  it_behaves_like "fails", Statesman::InvalidTransitionError
267
275
  end
268
276
 
269
277
  context "to an initial state and other states" do
270
278
  let(:options) { { from: nil, to: %i[y x z] } }
279
+
271
280
  it_behaves_like "fails", Statesman::InvalidTransitionError
272
281
  end
273
282
  end
@@ -275,21 +284,25 @@ describe Statesman::Machine do
275
284
  context "with validate_states" do
276
285
  context "from anything" do
277
286
  let(:options) { { from: nil, to: :y } }
287
+
278
288
  it_behaves_like "adds callback"
279
289
  end
280
290
 
281
291
  context "to anything" do
282
292
  let(:options) { { from: :x, to: [] } }
293
+
283
294
  it_behaves_like "adds callback"
284
295
  end
285
296
 
286
297
  context "to several" do
287
298
  let(:options) { { from: :x, to: %i[y z] } }
299
+
288
300
  it_behaves_like "adds callback"
289
301
  end
290
302
 
291
303
  context "from any to several" do
292
304
  let(:options) { { from: nil, to: %i[y z] } }
305
+
293
306
  it_behaves_like "adds callback"
294
307
  end
295
308
  end
@@ -349,6 +362,8 @@ describe Statesman::Machine do
349
362
  end
350
363
 
351
364
  describe "#current_state" do
365
+ subject { instance.current_state }
366
+
352
367
  before do
353
368
  machine.class_eval do
354
369
  state :x, initial: true
@@ -360,7 +375,6 @@ describe Statesman::Machine do
360
375
  end
361
376
 
362
377
  let(:instance) { machine.new(my_model) }
363
- subject { instance.current_state }
364
378
 
365
379
  context "with no transitions" do
366
380
  it { is_expected.to eq(machine.initial_state) }
@@ -368,6 +382,7 @@ describe Statesman::Machine do
368
382
 
369
383
  context "with multiple transitions" do
370
384
  before { instance.transition_to!(:y) }
385
+
371
386
  before { instance.transition_to!(:z) }
372
387
 
373
388
  it { is_expected.to eq("z") }
@@ -375,6 +390,8 @@ describe Statesman::Machine do
375
390
  end
376
391
 
377
392
  describe "#in_state?" do
393
+ subject { instance.in_state?(state) }
394
+
378
395
  before do
379
396
  machine.class_eval do
380
397
  state :x, initial: true
@@ -384,21 +401,24 @@ describe Statesman::Machine do
384
401
  end
385
402
 
386
403
  let(:instance) { machine.new(my_model) }
387
- subject { instance.in_state?(state) }
404
+
388
405
  before { instance.transition_to!(:y) }
389
406
 
390
407
  context "when machine is in given state" do
391
408
  let(:state) { "y" }
409
+
392
410
  it { is_expected.to eq(true) }
393
411
  end
394
412
 
395
413
  context "when machine is not in given state" do
396
414
  let(:state) { "x" }
415
+
397
416
  it { is_expected.to eq(false) }
398
417
  end
399
418
 
400
419
  context "when given a symbol" do
401
420
  let(:state) { :y }
421
+
402
422
  it { is_expected.to eq(true) }
403
423
  end
404
424
 
@@ -406,11 +426,13 @@ describe Statesman::Machine do
406
426
  context "when given multiple arguments" do
407
427
  context "when one of the states is the current state" do
408
428
  subject { instance.in_state?(:x, :y) }
429
+
409
430
  it { is_expected.to eq(true) }
410
431
  end
411
432
 
412
433
  context "when none of the states are the current state" do
413
434
  subject { instance.in_state?(:x, :z) }
435
+
414
436
  it { is_expected.to eq(false) }
415
437
  end
416
438
  end
@@ -418,11 +440,13 @@ describe Statesman::Machine do
418
440
  context "when given an array" do
419
441
  context "when one of the states is the current state" do
420
442
  subject { instance.in_state?(%i[x y]) }
443
+
421
444
  it { is_expected.to eq(true) }
422
445
  end
423
446
 
424
447
  context "when none of the states are the current state" do
425
448
  subject { instance.in_state?(%i[x z]) }
449
+
426
450
  it { is_expected.to eq(false) }
427
451
  end
428
452
  end
@@ -430,6 +454,8 @@ describe Statesman::Machine do
430
454
  end
431
455
 
432
456
  describe "#allowed_transitions" do
457
+ subject { instance.allowed_transitions(metadata) }
458
+
433
459
  before do
434
460
  machine.class_eval do
435
461
  state :x, initial: true
@@ -442,7 +468,6 @@ describe Statesman::Machine do
442
468
 
443
469
  let(:instance) { machine.new(my_model) }
444
470
  let(:metadata) { { some: :metadata } }
445
- subject { instance.allowed_transitions(metadata) }
446
471
 
447
472
  context "with multiple possible states" do
448
473
  it { is_expected.to eq(%w[y z]) }
@@ -450,6 +475,7 @@ describe Statesman::Machine do
450
475
 
451
476
  context "with one possible state" do
452
477
  before { instance.transition_to!(:y) }
478
+
453
479
  it { is_expected.to eq(["z"]) }
454
480
 
455
481
  context "guarded using metadata" do
@@ -475,6 +501,7 @@ describe Statesman::Machine do
475
501
 
476
502
  context "with no possible transitions" do
477
503
  before { instance.transition_to!(:z) }
504
+
478
505
  it { is_expected.to eq([]) }
479
506
  end
480
507
  end
@@ -491,6 +518,8 @@ describe Statesman::Machine do
491
518
  end
492
519
 
493
520
  describe "#can_transition_to?" do
521
+ subject(:can_transition_to?) { instance.can_transition_to?(new_state, metadata) }
522
+
494
523
  before do
495
524
  machine.class_eval do
496
525
  state :x, initial: true
@@ -503,38 +532,43 @@ describe Statesman::Machine do
503
532
 
504
533
  let(:instance) { machine.new(my_model) }
505
534
  let(:metadata) { { some: :metadata } }
506
- subject { instance.can_transition_to?(new_state, metadata) }
507
535
 
508
536
  context "when the transition is invalid" do
509
537
  context "with an initial to state" do
510
538
  let(:new_state) { :x }
539
+
511
540
  it { is_expected.to be_falsey }
512
541
  end
513
542
 
514
543
  context "with a terminal from state" do
515
544
  before { instance.transition_to!(:y) }
545
+
516
546
  let(:new_state) { :y }
547
+
517
548
  it { is_expected.to be_falsey }
518
549
  end
519
550
 
520
551
  context "and is guarded" do
521
552
  let(:guard_cb) { -> { false } }
522
553
  let(:new_state) { :z }
554
+
523
555
  before { machine.guard_transition(to: new_state, &guard_cb) }
524
556
 
525
557
  it "does not fire guard" do
526
- expect(guard_cb).not_to receive(:call)
527
- is_expected.to be_falsey
558
+ expect(guard_cb).to_not receive(:call)
559
+ expect(can_transition_to?).to be_falsey
528
560
  end
529
561
  end
530
562
  end
531
563
 
532
564
  context "when the transition valid" do
533
565
  let(:new_state) { :y }
566
+
534
567
  it { is_expected.to be_truthy }
535
568
 
536
569
  context "but it has a failing guard" do
537
570
  before { machine.guard_transition(to: :y) { false } }
571
+
538
572
  it { is_expected.to be_falsey }
539
573
  end
540
574
 
@@ -611,6 +645,7 @@ describe Statesman::Machine do
611
645
  context "with a guard" do
612
646
  let(:result) { true }
613
647
  let(:guard_cb) { ->(*_args) { result } }
648
+
614
649
  before { machine.guard_transition(from: :x, to: :y, &guard_cb) }
615
650
 
616
651
  context "and an object to act on" do
@@ -643,15 +678,17 @@ describe Statesman::Machine do
643
678
  end
644
679
 
645
680
  describe "#transition_to" do
681
+ subject { instance.transition_to(:some_state, metadata) }
682
+
646
683
  let(:instance) { machine.new(my_model) }
647
684
  let(:metadata) { { some: :metadata } }
648
- subject { instance.transition_to(:some_state, metadata) }
649
685
 
650
686
  context "when it is succesful" do
651
687
  before do
652
688
  expect(instance).to receive(:transition_to!).once.
653
689
  with(:some_state, metadata).and_return(:some_state)
654
690
  end
691
+
655
692
  it { is_expected.to be(:some_state) }
656
693
  end
657
694
 
@@ -660,6 +697,7 @@ describe Statesman::Machine do
660
697
  allow(instance).to receive(:transition_to!).
661
698
  and_raise(Statesman::GuardFailedError)
662
699
  end
700
+
663
701
  it { is_expected.to be_falsey }
664
702
  end
665
703
 
@@ -669,7 +707,7 @@ describe Statesman::Machine do
669
707
  and_raise(RuntimeError, "user defined exception")
670
708
  end
671
709
 
672
- it "should not rescue the exception" do
710
+ it "does not rescue the exception" do
673
711
  expect { instance.transition_to(:some_state, metadata) }.
674
712
  to raise_error(RuntimeError, "user defined exception")
675
713
  end
@@ -2,11 +2,12 @@ require "rails/version"
2
2
  require "rspec/rails"
3
3
  require "ammeter/init"
4
4
 
5
- TMP_GENERATOR_PATH = File.expand_path("../generator-tmp", __FILE__)
5
+ TMP_GENERATOR_PATH = File.expand_path("generator-tmp", __dir__)
6
6
 
7
7
  shared_examples "a generator" do
8
8
  destination TMP_GENERATOR_PATH
9
9
  before { prepare_destination }
10
+
10
11
  let(:gen) { generator %w[Yummy::Bacon Yummy::BaconTransition] }
11
12
 
12
13
  it "invokes create_model_file method" do
@@ -19,6 +20,7 @@ shared_examples "a generator" do
19
20
 
20
21
  describe "it generates a correctly named file" do
21
22
  subject { file(migration_name) }
23
+
22
24
  it { is_expected.to be_a_migration }
23
25
  end
24
26
  end
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "statesman/version"
4
4
 
@@ -19,18 +19,18 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.required_ruby_version = ">= 2.2"
21
21
 
22
- spec.add_development_dependency "ammeter", "~> 1.1"
23
- spec.add_development_dependency "bundler", "~> 1.3"
24
- spec.add_development_dependency "mysql2", "~> 0.4"
25
- spec.add_development_dependency "pg", "~> 0.18"
22
+ spec.add_development_dependency "ammeter", "~> 1.1"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "gc_ruboconfig", "~> 2.3.9"
25
+ spec.add_development_dependency "mysql2", "~> 0.4.0"
26
+ spec.add_development_dependency "pg", "~> 0.18"
26
27
  spec.add_development_dependency "pry"
27
- spec.add_development_dependency "rails", ">= 3.2"
28
- spec.add_development_dependency "rake", "~> 12.3.0"
29
- spec.add_development_dependency "rspec", "~> 3.1"
30
- spec.add_development_dependency "rspec-its", "~> 1.1"
31
- spec.add_development_dependency "rspec-rails", "~> 3.1"
32
- spec.add_development_dependency "rspec_junit_formatter", "~> 0.3.0"
33
- spec.add_development_dependency "rubocop", "~> 0.52.0"
28
+ spec.add_development_dependency "rails", ">= 3.2"
29
+ spec.add_development_dependency "rake", "~> 12.3.0"
30
+ spec.add_development_dependency "rspec", "~> 3.1"
31
+ spec.add_development_dependency "rspec-its", "~> 1.1"
32
+ spec.add_development_dependency "rspec-rails", "~> 3.1"
33
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.4.0"
34
34
  spec.add_development_dependency "sqlite3", "~> 1.3"
35
35
  spec.add_development_dependency "timecop", "~> 0.9.1"
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statesman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.1
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GoCardless
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-14 00:00:00.000000000 Z
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ammeter
@@ -38,20 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gc_ruboconfig
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.3.9
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.9
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: mysql2
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: '0.4'
61
+ version: 0.4.0
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '0.4'
68
+ version: 0.4.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pg
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -156,28 +170,14 @@ dependencies:
156
170
  requirements:
157
171
  - - "~>"
158
172
  - !ruby/object:Gem::Version
159
- version: 0.3.0
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - "~>"
165
- - !ruby/object:Gem::Version
166
- version: 0.3.0
167
- - !ruby/object:Gem::Dependency
168
- name: rubocop
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - "~>"
172
- - !ruby/object:Gem::Version
173
- version: 0.52.0
173
+ version: 0.4.0
174
174
  type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
- version: 0.52.0
180
+ version: 0.4.0
181
181
  - !ruby/object:Gem::Dependency
182
182
  name: sqlite3
183
183
  requirement: !ruby/object:Gem::Requirement
@@ -261,13 +261,13 @@ files:
261
261
  - spec/statesman/adapters/active_record_spec.rb
262
262
  - spec/statesman/adapters/active_record_transition_spec.rb
263
263
  - spec/statesman/adapters/memory_spec.rb
264
+ - spec/statesman/adapters/memory_transition_spec.rb
264
265
  - spec/statesman/adapters/mongoid_spec.rb
265
266
  - spec/statesman/adapters/shared_examples.rb
266
267
  - spec/statesman/callback_spec.rb
267
268
  - spec/statesman/config_spec.rb
268
269
  - spec/statesman/guard_spec.rb
269
270
  - spec/statesman/machine_spec.rb
270
- - spec/statesman/transition_spec.rb
271
271
  - spec/statesman/utils_spec.rb
272
272
  - spec/support/active_record.rb
273
273
  - spec/support/generators_shared_examples.rb
@@ -293,7 +293,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
293
293
  version: '0'
294
294
  requirements: []
295
295
  rubyforge_project:
296
- rubygems_version: 2.7.4
296
+ rubygems_version: 2.6.14
297
297
  signing_key:
298
298
  specification_version: 4
299
299
  summary: A statesman-like state machine library
@@ -309,13 +309,13 @@ test_files:
309
309
  - spec/statesman/adapters/active_record_spec.rb
310
310
  - spec/statesman/adapters/active_record_transition_spec.rb
311
311
  - spec/statesman/adapters/memory_spec.rb
312
+ - spec/statesman/adapters/memory_transition_spec.rb
312
313
  - spec/statesman/adapters/mongoid_spec.rb
313
314
  - spec/statesman/adapters/shared_examples.rb
314
315
  - spec/statesman/callback_spec.rb
315
316
  - spec/statesman/config_spec.rb
316
317
  - spec/statesman/guard_spec.rb
317
318
  - spec/statesman/machine_spec.rb
318
- - spec/statesman/transition_spec.rb
319
319
  - spec/statesman/utils_spec.rb
320
320
  - spec/support/active_record.rb
321
321
  - spec/support/generators_shared_examples.rb