dekiru 0.5.1 → 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: 118e02daae0f7eea68172f325ac8465830aa9d44eae089dd27244f0ed517b820
4
- data.tar.gz: 13cf2c26e1bed62aad8dc32760e2b4ef071f19d55f080db3bdc2d2c7254927ec
3
+ metadata.gz: c0f03d8577117755bdd83832d5385e841f9b9062ed9280ff113126678fe246b4
4
+ data.tar.gz: 6b1b4051c93f6fbacdf95feb0d5f89daafaf4b86ee8c871d8d35dd4c86e9aea7
5
5
  SHA512:
6
- metadata.gz: 047e7598645d7dc800b440a3ba85d67d45d1aa482927a8c944fe5cb929afc37d683081b2c2714359b5a16d3ef92abe3b0bb054cbaf37ebdeb77fcb8dba8a5217
7
- data.tar.gz: 5c3c7f2a719bd56ca3c2c4cca1838739235f1d6113734c372193b83e1753811afd3df35abca57740a1afe530f6b8312650bce570b364bd051dc8a5b10de2e435
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?
@@ -4,7 +4,7 @@ namespace :db do
4
4
  task check_conflict: :environment do
5
5
  migrations_status =
6
6
  if ActiveRecord::Base.connection.respond_to?(:migration_context)
7
- ActiveRecord::Base.connection.migration_context.migrations_status
7
+ ActiveRecord::Base.connection.migration_context.current_version.zero? ? [] : ActiveRecord::Base.connection.migration_context.migrations_status
8
8
  else
9
9
  paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
10
10
  ActiveRecord::Migrator.migrations_status(paths)
@@ -1,3 +1,3 @@
1
1
  module Dekiru
2
- VERSION = '0.5.1'
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.1
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-04 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