dekiru 0.5.2 → 0.6.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 +4 -4
- data/lib/dekiru/data_migration_operator.rb +15 -11
- data/lib/dekiru/version.rb +1 -1
- data/spec/dekiru/data_migration_operator_spec.rb +60 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0f03d8577117755bdd83832d5385e841f9b9062ed9280ff113126678fe246b4
|
4
|
+
data.tar.gz: 6b1b4051c93f6fbacdf95feb0d5f89daafaf4b86ee8c871d8d35dd4c86e9aea7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15bd327a6571a4d56c7f0ea1fb3931c517496b02003f709a7185e052abb42fc66e5555eec0ba8fa3824cc4f64b4ab3e234517d5615f8287e97e6c6b30aa21a53
|
7
|
+
data.tar.gz: adc18f216af47e11d68daaeb86304a6e0d00351a019bdc516ce14afa89b6e69338248c6c7a29aef7c610cf1ab69945dae7653e107c662f50f57ae9a80f2c6c9f
|
@@ -53,22 +53,26 @@ module Dekiru
|
|
53
53
|
((self.ended_at || Time.current) - self.started_at)
|
54
54
|
end
|
55
55
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
target_scope.find_each do |target|
|
66
|
-
yield target
|
56
|
+
def each_with_progress(enum, options = {})
|
57
|
+
options = options.dup
|
58
|
+
options[:total] ||= ((enum.size == Float::INFINITY ? nil : enum.size) rescue nil)
|
59
|
+
options[:format] ||= options[:total] ? '%a |%b>>%i| %p%% %t' : '%a |%b>>%i| ??%% %t'
|
60
|
+
options[:output] = stream
|
61
|
+
|
62
|
+
@pb = ::ProgressBar.create(options)
|
63
|
+
enum.each do |item|
|
64
|
+
yield item
|
67
65
|
@pb.increment
|
68
66
|
end
|
69
67
|
@pb.finish
|
70
68
|
end
|
71
69
|
|
70
|
+
def find_each_with_progress(target_scope, options = {}, &block)
|
71
|
+
# `LocalJumpError: no block given (yield)` が出る場合、 find_each メソッドが enumerator を返していない可能性があります
|
72
|
+
# 直接 each_with_progress を使うか、 find_each が enumerator を返すように修正してください
|
73
|
+
each_with_progress(target_scope.find_each, options, &block)
|
74
|
+
end
|
75
|
+
|
72
76
|
private
|
73
77
|
|
74
78
|
def current_transaction_open?
|
data/lib/dekiru/version.rb
CHANGED
@@ -106,17 +106,38 @@ describe Dekiru::DataMigrationOperator do
|
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
|
-
describe '#
|
109
|
+
describe '#each_with_progress' do
|
110
110
|
it '進捗が表示される' do
|
111
|
+
record = (0...10)
|
112
|
+
|
113
|
+
allow(STDIN).to receive(:gets) do
|
114
|
+
"yes\n"
|
115
|
+
end
|
116
|
+
|
117
|
+
sum = 0
|
118
|
+
operator.execute do
|
119
|
+
each_with_progress(record, title: 'count up number') do |num|
|
120
|
+
sum += num
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
expect(sum).to eq(45)
|
125
|
+
expect(operator.result).to eq(true)
|
126
|
+
expect(operator.error).to eq(nil)
|
127
|
+
expect(operator.stream.out).to include('Are you sure to commit?')
|
128
|
+
expect(operator.stream.out).to include('count up number:')
|
129
|
+
expect(operator.stream.out).to include('Finished successfully:')
|
130
|
+
expect(operator.stream.out).to include('Total time:')
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'total をオプションで渡すことができる' do
|
111
134
|
class Dekiru::DummyRecord
|
112
135
|
def self.count
|
113
|
-
|
136
|
+
raise "won't call"
|
114
137
|
end
|
115
138
|
|
116
|
-
def self.
|
117
|
-
|
118
|
-
yield(num)
|
119
|
-
end
|
139
|
+
def self.each
|
140
|
+
yield 99
|
120
141
|
end
|
121
142
|
end
|
122
143
|
|
@@ -126,7 +147,34 @@ describe Dekiru::DataMigrationOperator do
|
|
126
147
|
|
127
148
|
sum = 0
|
128
149
|
operator.execute do
|
129
|
-
|
150
|
+
each_with_progress(Dekiru::DummyRecord, title: 'pass total as option', total: 1) do |num|
|
151
|
+
sum += num
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
expect(sum).to eq(99)
|
156
|
+
expect(operator.result).to eq(true)
|
157
|
+
expect(operator.error).to eq(nil)
|
158
|
+
expect(operator.stream.out).to include('Are you sure to commit?')
|
159
|
+
expect(operator.stream.out).to include('pass total as option:')
|
160
|
+
expect(operator.stream.out).to include('Finished successfully:')
|
161
|
+
expect(operator.stream.out).to include('Total time:')
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe '#find_each_with_progress' do
|
166
|
+
it '進捗が表示される' do
|
167
|
+
record = (0...10).to_a.tap do |r|
|
168
|
+
r.singleton_class.alias_method(:find_each, :each)
|
169
|
+
end
|
170
|
+
|
171
|
+
allow(STDIN).to receive(:gets) do
|
172
|
+
"yes\n"
|
173
|
+
end
|
174
|
+
|
175
|
+
sum = 0
|
176
|
+
operator.execute do
|
177
|
+
find_each_with_progress(record, title: 'count up number') do |num|
|
130
178
|
sum += num
|
131
179
|
end
|
132
180
|
end
|
@@ -147,7 +195,11 @@ describe Dekiru::DataMigrationOperator do
|
|
147
195
|
end
|
148
196
|
|
149
197
|
def self.find_each
|
150
|
-
|
198
|
+
if block_given?
|
199
|
+
yield 99
|
200
|
+
else
|
201
|
+
Enumerator.new { |y| y << 99 }
|
202
|
+
end
|
151
203
|
end
|
152
204
|
end
|
153
205
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dekiru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akihiro Matsumura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|