dekiru 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5236c7dec0f57098ef3278eb56f1ba700885cf19acd5a94fa4b0dea97d9ead96
4
- data.tar.gz: 8e820fb5946bd66d0a81be3c4b5a07343fd41ee2947641be0ae307c1540c9e44
3
+ metadata.gz: c0f03d8577117755bdd83832d5385e841f9b9062ed9280ff113126678fe246b4
4
+ data.tar.gz: 6b1b4051c93f6fbacdf95feb0d5f89daafaf4b86ee8c871d8d35dd4c86e9aea7
5
5
  SHA512:
6
- metadata.gz: 4ede10e1096b73e97b2c6fb8d21dd7f85a60660df486a9df0716b4a2ea6c0cbca60cc352be45737b7f1a392d18fbf1e50b011c096416d7f5cfb7c931f70126d4
7
- data.tar.gz: bed88a1ec88187001cff444c441ab91ea5e884665b96fee28610a01e3f52150a5f30b1230b27b073b47deb900fedc341bbe297ac995370dbced8520bcc2d7951
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 find_each_with_progress(target_scope, options = {})
57
- total = options.delete(:total)
58
- opt = {
59
- format: '%a |%b>>%i| %p%% %t',
60
- }.merge(options).merge(
61
- total: total || target_scope.count,
62
- output: stream
63
- )
64
- @pb = ::ProgressBar.create(opt)
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?
@@ -1,3 +1,3 @@
1
1
  module Dekiru
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -106,17 +106,38 @@ describe Dekiru::DataMigrationOperator do
106
106
  end
107
107
  end
108
108
 
109
- describe '#find_each_with_progress' do
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
- 10
136
+ raise "won't call"
114
137
  end
115
138
 
116
- def self.find_each
117
- (0...count).to_a.each do |num|
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
- find_each_with_progress(Dekiru::DummyRecord, title: 'count up number') do |num|
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
- yield 99
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.5.2
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: 2023-10-18 00:00:00.000000000 Z
11
+ date: 2024-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails