Class: MyDiff::CLI

Inherits:
HighLine
  • Object
show all
Defined in:
lib/mydiff/cli.rb

Instance Method Summary collapse

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

#acceptObject



45
46
47
48
49
50
51
52
53
# File 'lib/mydiff/cli.rb', line 45

def accept
  submenu 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


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 main_menu
  continue = true
  begin
    while continue
      choose do |menu|
        menu.prompt = "What now?"
      
        menu.choice("Status") { status }
        menu.choice("Accept") { accept }
        menu.choice("Reject") { reject }
        menu.choice("Patch")  { patch  }
        menu.choice("Apply")  { apply! }
        menu.choice("Quit")   { continue = false }
      end
    end
  rescue Interrupt
  end
end

#patchObject



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
  submenu([: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

#rejectObject



55
56
57
58
59
60
61
62
# File 'lib/mydiff/cli.rb', line 55

def reject
  submenu 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