Skip to content

Commit f8d2286

Browse files
authored
Add AnnotateRb::Runner.running? method (#242)
This is so that we can facilitate dynamic logic during the annotation process. This is something that the historical `annotate_models` gem provided, via `Annotate::Migration.class_variable_get(:@@working))` https://github.com/ctran/annotate_models/blob/5d01c4171990c4fe7b9b0977b05ce14a98209e53/lib/tasks/annotate_models_migrate.rake#L36-L41 Instead of having to reach into the Migration class, this instead stores some state on the `AnnotateRb::Runner` class so that we can ask it directly if it's in the middle of running.
1 parent 1b13396 commit f8d2286

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/annotate_rb/runner.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@
33
module AnnotateRb
44
class Runner
55
class << self
6+
attr_reader :runner
7+
68
def run(args)
7-
new.run(args)
9+
self.runner = new
10+
11+
runner.run(args)
12+
13+
self.runner = nil
814
end
15+
16+
def running?
17+
!!runner
18+
end
19+
20+
private
21+
22+
attr_writer :runner
923
end
1024

1125
def run(args)

spec/lib/annotate_rb/runner_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,30 @@
8484
expect(command_double).to have_received(:call)
8585
end
8686
end
87+
88+
describe ".running?" do
89+
context "when an instance is not running" do
90+
it "is false" do
91+
expect(AnnotateRb::Runner).not_to be_running
92+
end
93+
end
94+
95+
context "when an instance is running" do
96+
it "is true" do
97+
expect(AnnotateRb::Runner).not_to be_running
98+
99+
double = instance_double(described_class)
100+
101+
allow(described_class).to receive(:new).and_return(double)
102+
103+
expect(double).to receive(:run) do |original_method, *args|
104+
expect(AnnotateRb::Runner).to be_running
105+
106+
true
107+
end
108+
109+
AnnotateRb::Runner.run({})
110+
end
111+
end
112+
end
87113
end

0 commit comments

Comments
 (0)