lib/troo/cli/main_cli.rb in troo-0.0.4 vs lib/troo/cli/main_cli.rb in troo-0.0.5

- old
+ new

@@ -1,74 +1,35 @@ module Troo module CLI class Main < ThorFixes + include Helpers + default_task :status class_option :debug, type: :boolean, desc: "Enable debugging." desc "status", "Get troo status." def status - board_count = Troo::Board.count - list_count = Troo::List.count - card_count = Troo::Card.count + say "Status:" + get_status(:board) + get_status(:list) + get_status(:card) + say "" - if board_count > 0 - if board = Troo::BoardRetrieval.default - say "Board: #{BoardDecorator.new(board).short}" - else - say "Board: No default set." - end - else - say "No local board data." - end - - if list_count > 0 - if list = Troo::ListRetrieval.default - say "List: #{ListDecorator.new(list).short}" - else - say "List: No default set." - end - else - say "No local list data." - end - - if card_count > 0 - if card = Troo::CardRetrieval.default - say "Card: #{CardDecorator.new(card).short}" - else - say "Card: No default set." - end - else - say "No local card data." - end - - puts [plural(board_count, "board"), - plural(list_count, "list"), - plural(card_count, "card") ].join(", ") - puts - help end - private - - def plural(size, singular) - (size == 1) ? "#{size} #{singular}" : "#{size} #{singular}s" - end - - public - desc "refresh", "Refresh all data for default board." method_option :all, type: :boolean, desc: "Refresh all boards, lists, cards and comments." method_option :lists, type: :boolean, desc: "Refresh all lists for default board." method_option :cards, type: :boolean, desc: "Refresh all cards for default board." def refresh if options["all"] RefreshAll.all(nil, options) say "All local data has been refreshed." else - if board = Troo::BoardRetrieval.default + if board = BoardRetrieval.default if options["lists"] RefreshAll.lists(board, options) say "All lists for the default board have been refreshed." elsif options["cards"] RefreshAll.cards(board, options) @@ -107,14 +68,14 @@ desc "default [board|list|card] <id>", "Set board, list or card to be default." subcommand :default, Troo::CLI::Default desc "move <card_id> <list_id>", "Move a card <card_id> to list <list_id>." def move(card_id, list_id) - if card = Troo::CardRetrieval.retrieve(card_id) - if list = Troo::ListRetrieval.retrieve(list_id) + if card = CardRetrieval.retrieve(card_id) + if list = ListRetrieval.retrieve(list_id) if result = MoveCard.with(card, list) - say "Card moved from '#{card.list.name}' to '#{list.name}'." + say "Card moved from '#{card.list.decorator.name}' to '#{list.decorator.name}'." else say "Card could not be moved." end else say "Card could not be moved, as list was not found." @@ -122,9 +83,50 @@ else say "Card could not be moved, as card was not found." end rescue Troo::InvalidAccessToken say "Your Trello access credentials have expired, please renew and try again." + end + + private + attr_reader :id, :type + + def get_status(type) + @id, @type = nil, type + return show if resource_found && resource + return no_default_found if resource_count > 0 + end + + def resource_found + label = " #{type.to_s.capitalize}s:".ljust(10) + say label + plural(resource_count, type.to_s) + " found." + end + + def resource_count + @count = case type + when :board then Troo::Board.count + when :list then Troo::List.count + when :card then Troo::Card.count + end + end + + def show + say " #{resource.decorator.title}" + end + + def no_default_found + say " " + Esc.red + "No default #{type} set." + Esc.reset + end + + def plural(size, singular) + pluralized = singular + "s" + if size == 0 + "No #{pluralized}" + elsif size == 1 + "#{size} #{singular}" + else + "#{size} #{pluralized}" + end end end end end