Class: Dekiru::DataMigrationOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/dekiru/data_migration_operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @title = title
  @stream = options[:output] || $stdout
end

Instance Attribute Details

#canceledObject (readonly)

Returns the value of attribute canceled.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def canceled
  @canceled
end

#ended_atObject (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

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def error
  @error
end

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def result
  @result
end

#started_atObject (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

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/dekiru/data_migration_operator.rb', line 5

def stream
  @stream
end

#titleObject (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, options = {}, &block)
  self.new(title, options).execute(&block)
end

Instance Method Details

#cancel!Object

Raises:

  • (ActiveRecord::Rollback)


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

Returns:

  • (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?(message = '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

#durationObject



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, options = {})
  opt = {
    format: '%a |%b>>%i| %p%% %t',
  }.merge(options).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(message)
  stream.puts(message)
end

#newlineObject



73
74
75
# File 'lib/dekiru/data_migration_operator.rb', line 73

def newline
  log ''
end