lib/lionel/export.rb in lionel_richie-0.1.5 vs lib/lionel/export.rb in lionel_richie-0.1.5.1

- old
+ new

@@ -55,89 +55,78 @@ end def download Lionel.logger.info "Exporting trello board '#{board.name}' (#{trello_board_id}) to " + "google doc '#{spreadsheet.title}' (#{google_doc_id})" - start_row = 2 - rows = worksheet.size - - card_rows = {} - - # Find existing rows for current cards - (start_row..rows).each do |row| - cell_id = worksheet["B",row] - next unless cell_id.present? - card = cards.find { |c| c.id == cell_id } - next unless card.present? - card_rows[row] = card - end - - # Set available rows for new cards - new_cards = cards - card_rows.values - new_cards.each_with_index do |card, i| - row = rows + i + 1 - card_rows[row] = card - end - - card_rows.each do |row, card| + card_map.each do |row, card| begin - Timeout.timeout(5) { sync_row(row, card) } + Timeout.timeout(5) { + Lionel.logger.info "row[#{row}] = #{card.name}" + + sync_columns(card).each do |col, value| + worksheet[col,row] = value + end + } rescue Timeout::Error, Trello::Error => e Lionel.logger.warn e.inspect Lionel.logger.warn card.inspect end end end + def card_map + @card_map ||= CardMap.new(cards, worksheet) + end + def upload worksheet.save end def rows worksheet.rows end - def sync_row(row, card) - Lionel.logger.info "row[#{row}] = #{card.name}" + def sync_columns(card) + {}.tap do |columns| + columns["B"] = card.id - worksheet["B",row] = card.id + # Card link + columns["C"] = card.link(card.name.gsub(/^\[.*\]\s*/, "")) - # Card link - worksheet["C",row] = card.link + # Ready date + ready_action = card.first_action do |a| + (a.create? && a.board_id == trello_board_id) || a.moved_to?("Ready") + end + columns["D"] = card.format_date(ready_action.date) if ready_action - # Ready date - ready_action = card.first_action do |a| - (a.create? && a.board_id == trello_board_id) || a.moved_to?("Ready") - end - worksheet["D",row] = card.format_date(ready_action.date) if ready_action + # In Progress date + columns["E"] = card.date_moved_to("In Progress") - # In Progress date - worksheet["E",row] = card.date_moved_to("In Progress") + # Code Review date + columns["F"] = card.date_moved_to("Code Review") - # Code Review date - worksheet["F",row] = card.date_moved_to("Code Review") + # Review date + columns["G"] = card.date_moved_to("Review") - # Review date - worksheet["G",row] = card.date_moved_to("Review") + # Deploy date + columns["H"] = card.date_moved_to("Deploy") - # Deploy date - worksheet["H",row] = card.date_moved_to("Deploy") + # Completed date + columns["I"] = card.date_moved_to("Completed") - # Completed date - worksheet["I",row] = card.date_moved_to("Completed") + # Type + columns["J"] = card.type - # Type - worksheet["J",row] = card.type + # Project + columns["K"] = card.project - # Project - worksheet["K",row] = card.project + # Estimate + columns["L"] = card.estimate - # Estimate - worksheet["L",row] = card.estimate - - # Due Date - worksheet["M",row] = card.due_date + # Due Date + columns["M"] = card.due_date + end end def authenticate return if @authenticated authenticate_trello @@ -185,16 +174,41 @@ def initialize(cards, worksheet) @cards, @worksheet = cards, worksheet end def each(&block) - card_rows.each(&block) + rows.each(&block) end - def card_rows - @card_rows ||= {}.tap do |map| + def rows + @rows ||= populate_rows + end + + private + + def populate_rows + Lionel.logger.info "Using CardMap!!!!" + {}.tap do |card_rows| + + start_row = 2 + rows = worksheet.size + + # Find existing rows for current cards + (start_row..rows).each do |row| + cell_id = worksheet["B",row] + next unless cell_id.present? + card = cards.find { |c| c.id == cell_id } + next unless card.present? + card_rows[row] = card + end + + # Set available rows for new cards + new_cards = cards - card_rows.values + new_cards.each_with_index do |card, i| + row = rows + i + 1 + card_rows[row] = card + end end end end - end end