Class: MyDiff::CLI
- Inherits:
-
HighLine
- Object
- HighLine
- MyDiff::CLI
- Defined in:
- lib/mydiff/cli.rb
Instance Method Summary collapse
- #accept ⇒ Object
- #apply! ⇒ Object
-
#initialize(parent) ⇒ CLI
constructor
A new instance of CLI.
- #main_menu ⇒ Object
- #patch ⇒ Object
- #reject ⇒ Object
- #status(type = :all) ⇒ Object
Constructor Details
#initialize(parent) ⇒ CLI
Returns a new instance of CLI.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/mydiff/cli.rb', line 5 def initialize(parent) @md = parent @tables = [] @md.new_tables.each {|t| @tables << {:status => :new, :table => t}} @md.dropped_tables.each {|t| @tables << {:status => :drop, :table => t}} @md.changed_tables.each {|t| @tables << {:status => :change, :table => t}} @tables.sort! {|t1,t2| t1[:table] <=> t2[:table]} @changes = {} super() end |
Instance Method Details
#accept ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/mydiff/cli.rb', line 45 def accept do |table| change = @changes[table[:table]] if change #and change.type.eql?(:patch) @changes.delete(table[:table]) end @changes[table[:table]] = Change.new(@md, :add, table[:table]) end end |
#apply! ⇒ Object
146 147 148 149 150 |
# File 'lib/mydiff/cli.rb', line 146 def apply! @changes.each_value do |change| change.apply! end end |
#main_menu ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mydiff/cli.rb', line 17 def continue = true begin while continue choose do || .prompt = "What now?" .choice("Status") { status } .choice("Accept") { accept } .choice("Reject") { reject } .choice("Patch") { patch } .choice("Apply") { apply! } .choice("Quit") { continue = false } end end rescue Interrupt end end |
#patch ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mydiff/cli.rb', line 64 def patch ([:change, :new]) do |table| continue = true while continue rows = [] @md.deleted_rows(table[:table]).each {|r| rows << {:status => :delete, :row => r}} @md.changed_rows(table[:table]).each {|r| rows << {:status => :change, :row => r}} @md.new_rows(table[:table]).each {|r| rows << {:status => :new, :row => r}} rows.each_with_index do |row, count| status = row[:status] row = row[:row] pkey = {} @md.pkey_of(table[:table]).each do |f| pkey[f] = row[f] end ndata = @md.data_fields_of(table[:table]).map do |f| row["n_#{f}"] end odata = @md.data_fields_of(table[:table]).map do |f| row["o_#{f}"] end begin if @changes[table[:table]].has_chunk?(pkey) status = "*" else status = " " end rescue status = " " end say "" say "%2d. [%s] (%s) %s" % [count, status, pkey.values.join("||"), odata.join("||")] puts " [%s] (%s) %s" % [status, pkey.values.join("||"), ndata.join("||")] end opt = ask "Which one?" if opt.empty? or opt.downcase.eql?("q") continue = false else if opt =~ /(\d+)-(\d+)/ opts = ($1..$2).to_a elsif opt =~ /(\d+)/ opts = [$1] end opts.each do |opt| row = rows[opt.to_i] if row.nil? or not opt =~ /[0-9+]/ say "Wrong answer!" next end change = @changes[table[:table]] if (change and not change.type.eql?(:patch)) or not change @changes.delete(table[:table]) change = Change.new(@md, :patch, table[:table]) end pkey = {} @md.pkey_of(table[:table]).each do |f| pkey[f] = row[:row][f] end if change.has_chunk?(pkey) change.delete_chunk(pkey) else data = {} @md.data_fields_of(table[:table]).each do |f| data[f] = row[:row]["n_#{f}"] end change.add_chunk(row[:status], pkey, data) end @changes[table[:table]] = change if change.chunks.empty? @changes.delete(table[:table]) end end end end end end |
#reject ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/mydiff/cli.rb', line 55 def reject do |table| change = @changes[table[:table]] if change #and change.type.eql?(:patch) @changes.delete(table[:table]) end end end |
#status(type = :all) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/mydiff/cli.rb', line 36 def status(type = :all) type = [type] unless type.is_a?(Array) @tables.each_with_index do |table, count| if type.include?(:all) or type.include?(table[:status]) render_row(count, table[:table], table[:status]) end end end |