Class: Dekiru::DataMigrationOperator
- Inherits:
-
Object
- Object
- Dekiru::DataMigrationOperator
- Defined in:
- lib/dekiru/data_migration_operator.rb
Instance Attribute Summary collapse
-
#canceled ⇒ Object
readonly
Returns the value of attribute canceled.
-
#ended_at ⇒ Object
readonly
Returns the value of attribute ended_at.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #cancel! ⇒ Object
- #confirm?(message = 'Are you sure?') ⇒ Boolean
- #duration ⇒ Object
- #execute(&block) ⇒ Object
- #find_each_with_progress(target_scope, options = {}) ⇒ Object
-
#initialize(title, options = {}) ⇒ DataMigrationOperator
constructor
A new instance of DataMigrationOperator.
- #log(message) ⇒ Object
- #newline ⇒ Object
Constructor Details
#initialize(title, options = {}) ⇒ DataMigrationOperator
Returns a new instance of DataMigrationOperator.
11 12 13 14 |
# File 'lib/dekiru/data_migration_operator.rb', line 11 def initialize(title, = {}) @title = title @stream = [:output] || $stdout end |
Instance Attribute Details
#canceled ⇒ Object (readonly)
Returns the value of attribute canceled.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def canceled @canceled end |
#ended_at ⇒ Object (readonly)
Returns the value of attribute ended_at.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def ended_at @ended_at end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def error @error end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def result @result end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def started_at @started_at end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def stream @stream end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/dekiru/data_migration_operator.rb', line 5 def title @title end |
Class Method Details
.execute(title, options = {}, &block) ⇒ Object
7 8 9 |
# File 'lib/dekiru/data_migration_operator.rb', line 7 def self.execute(title, = {}, &block) self.new(title, ).execute(&block) end |
Instance Method Details
#cancel! ⇒ Object
77 78 79 80 |
# File 'lib/dekiru/data_migration_operator.rb', line 77 def cancel! log "Canceled: #{title}" raise ActiveRecord::Rollback end |
#confirm?(message = 'Are you sure?') ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dekiru/data_migration_operator.rb', line 59 def confirm?( = 'Are you sure?') loop do stream.print "#{message} (yes/no) > " case STDIN.gets.strip when 'yes' newline return true when 'no' newline cancel! end end end |
#duration ⇒ Object
40 41 42 |
# File 'lib/dekiru/data_migration_operator.rb', line 40 def duration ((self.ended_at || Time.current) - self.started_at) end |
#execute(&block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dekiru/data_migration_operator.rb', line 20 def execute(&block) @started_at = Time.current log "Start: #{title} at #{started_at}\n\n" @result = ActiveRecord::Base.transaction(requires_new: true) do instance_eval(&block) confirm?("\nAre you sure to commit?") end log "Finished successfully: #{title}" if @result == true rescue => e @error = e @result = false ensure @ended_at = Time.current log "Total time: #{self.duration.round(2)} sec" raise error if error return @result end |
#find_each_with_progress(target_scope, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dekiru/data_migration_operator.rb', line 44 def find_each_with_progress(target_scope, = {}) opt = { format: '%a |%b>>%i| %p%% %t', }.merge().merge( total: target_scope.count, output: stream ) pb = ::ProgressBar.create(opt) target_scope.find_each do |target| yield target pb.increment end pb.finish end |
#log(message) ⇒ Object
16 17 18 |
# File 'lib/dekiru/data_migration_operator.rb', line 16 def log() stream.puts() end |
#newline ⇒ Object
73 74 75 |
# File 'lib/dekiru/data_migration_operator.rb', line 73 def newline log '' end |