clockwork-mocks 1.1.1 → 1.2.0

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
  SHA1:
3
- metadata.gz: 5b436d9635aa6860b72e5bac1b000a593d4ef227
4
- data.tar.gz: 7b83532781c99ab48aa9fbc5fc779edbfb6250c9
3
+ metadata.gz: 17b8f640f1ade961325aa2a3f09024a14afc5eea
4
+ data.tar.gz: acca7e106a0e1e1fef75a3d3da5c2fdf2c91a294
5
5
  SHA512:
6
- metadata.gz: 33323cf68d95c4adca58d2f829ae6a6ec0e18564944abec53c941cb369f57ce2d6e3a570c345246420239a7db1495240399e3daacb273f3cd1da21b2c5adde46
7
- data.tar.gz: 0106ded73688114d79879b0a9b5a315af1dc292fd614de25d98c6d80c7921d790823882786694d439116d600e069d23988d3c3b564c819290e673de6b5e8797b
6
+ metadata.gz: 736060ca732b91939c22dd0f7df7fd3c898111de6ddc4acdfab8c2256fe4457b3718b1baa768bb34682b5c7d0af564396d8923ef8a3bef34b37bc139321accf6
7
+ data.tar.gz: 432f1b04c841fd621d6ca6b2df0adf95aa38213f4b9bb1521a17f7e1d407e7db25cfca9c4f100cf9c06aba5f6037cdfaa847dd1902952035b17a911d76d4c316
data/README.md CHANGED
@@ -26,42 +26,35 @@ Or install it yourself as:
26
26
 
27
27
  ```ruby
28
28
  RSpec.describe ... do
29
- let!(:clockwork_scheduler) { ClockworkMocks::Scheduler.init_rspec(->(a) { allow a }, ->(a) { receive a }, 'path/to/clock.rb') }
29
+ before(:all) { ClockworkMocks.init_rspec(method(:allow), method(:receive), 'path/to/clock.rb') }
30
+ before { ClockworkMocks.reset! }
30
31
  end
31
32
  ```
32
33
 
33
- If you do not pass a clock file path to `ClockworkMocks::Scheduler.new` and you are in a rails environment it will assume `"#{Rails.root}/clock.rb"` by default.
34
- This reloads the `clock.rb` file in every test.
35
- If you care about that performance leak, there is a more verbose initialization option:
36
-
37
- ```ruby
38
- RSpec.describe ... do
39
- clockwork_scheduler = nil
40
-
41
- before do
42
- clockwork_scheduler ||= ClockworkMocks::Scheduler.init_rspec(metod(:allow), method(:receive), 'path/to/clock.rb')
43
- clockwork_scheduler.reset!
44
- end
45
- end
46
- ```
34
+ If you do not pass a clock file path to `ClockworkMocks.init_rspec` and you are in a rails environment it will assume `"#{Rails.root}/clock.rb"` by default.
47
35
 
48
36
  ### General Initialization
49
37
 
50
38
  ```ruby
51
- clockwork_scheduler = ClockworkMocks::Scheduler.new
52
-
53
- # ...
39
+ ClockworkMocks.scheduler.handler do |job, time|
40
+ # something
41
+ end
54
42
 
55
- clockwork_scheduler.every(1.day, 'some task', at: '23:00') do
43
+ ClockworkMocks.scheduler.every(1.day, 'some task', at: '23:00') do
56
44
  # something
57
45
  end
58
46
  ```
59
47
 
60
- Using this interface, you can use any stub provider to stub `Clockwork.every` and call `clockwork_scheduler.every` instead, e.g. with rspec-mock:
48
+ Using this interface, you can use any stub provider to stub `Clockwork`'s methods and call `ClockworkMocks.scheduler`'s methods instead.
49
+ For example with rspec-mock:
61
50
 
62
51
  ```ruby
52
+ allow.call(Clockwork).to receive.call(:handler) do |&block|
53
+ ClockworkMocks.scheduler.handler(&block)
54
+ end
55
+
63
56
  allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
64
- clockwork_scheduler.every interval, name, hash, &block
57
+ ClockworkMocks.scheduler.every interval, name, hash, &block
65
58
  end
66
59
 
67
60
  load 'path/to/clock.rb'
@@ -69,12 +62,12 @@ load 'path/to/clock.rb'
69
62
 
70
63
  ### Executing clockwork tasks
71
64
 
72
- At any time you can call `clockwork_scheduler.work` to execute all tasks that are due.
65
+ At any time you can call `ClockworkMocks.work` to execute all tasks that are due.
73
66
  This works especially well in combination with timecop (although the latter is not a requirement):
74
67
 
75
68
  ```ruby
76
69
  Timecop.freeze(2.days.from_now) do
77
- clockwork_scheduler.work
70
+ ClockworkMocks.work
78
71
  end
79
72
  ```
80
73
 
@@ -82,11 +75,11 @@ Tasks will be executed in correct order.
82
75
  If enough time passed, tasks will be executed multiple times:
83
76
 
84
77
  ```ruby
85
- clockwork_scheduler.every(1.second, 'often') { puts 'often' }
86
- clockwork_scheduler.every(2.seconds, 'not-so-often') { puts 'not so often' }
78
+ ClockworkMocks.scheduler.every(1.second, 'often') { puts 'often' }
79
+ ClockworkMocks.scheduler.every(2.seconds, 'not-so-often') { puts 'not so often' }
87
80
 
88
81
  Timecop.freeze(3.seconds.from_now) do
89
- clockwork_scheduler.work
82
+ ClockworkMocks.work
90
83
  end
91
84
  ```
92
85
 
@@ -95,7 +88,7 @@ outputs
95
88
  ```
96
89
  often
97
90
  often
98
- not-so-often
91
+ not so often
99
92
  often
100
93
  ```
101
94
 
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "clockwork/mocks"
4
+ require "clockwork_mocks"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -5,3 +5,21 @@ require 'clockwork'
5
5
  require 'clockwork_mocks/version'
6
6
  require 'clockwork_mocks/clockwork_task'
7
7
  require 'clockwork_mocks/scheduler'
8
+
9
+ module ClockworkMocks
10
+ def self.init_rspec(allow, receive, clock_file = nil, &block)
11
+ scheduler.init_rspec(allow, receive, clock_file, &block)
12
+ end
13
+
14
+ def self.scheduler
15
+ @scheduler ||= Scheduler.new
16
+ end
17
+
18
+ def self.reset!
19
+ scheduler.reset!
20
+ end
21
+
22
+ def self.work
23
+ scheduler.work
24
+ end
25
+ end
@@ -25,7 +25,7 @@ module ClockworkMocks
25
25
  @due = calc_due
26
26
  end
27
27
 
28
- attr_reader :due
28
+ attr_reader :due, :name
29
29
 
30
30
  private
31
31
 
@@ -8,15 +8,17 @@ module ClockworkMocks
8
8
  @tasks = []
9
9
  end
10
10
 
11
- def self.init_rspec(allow, receive, clock_file = nil)
12
- scheduler = Scheduler.new
11
+ def self.init_rspec(allow, receive, clock_file = nil, &block)
12
+ Scheduler.new.tap { |s| s.init_rspec(allow, receive, clock_file, &block) }
13
+ end
13
14
 
15
+ def init_rspec(allow, receive, clock_file = nil)
14
16
  allow.call(Clockwork).to receive.call(:handler) do |&block|
15
- scheduler.handler(&block)
17
+ handler(&block)
16
18
  end
17
19
 
18
20
  allow.call(Clockwork).to receive.call(:every) do |interval, name, hash, &block|
19
- scheduler.every interval, name, hash, &block
21
+ every interval, name, hash, &block
20
22
  end
21
23
 
22
24
  if block_given?
@@ -29,8 +31,6 @@ module ClockworkMocks
29
31
 
30
32
  load clock_file if clock_file
31
33
  end
32
-
33
- scheduler
34
34
  end
35
35
 
36
36
  def work
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ClockworkMocks
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clockwork-mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Poetzsch-Heffter