Sha256: 8ec968d0a15d6945d87838c13766765310c5c4f881a8235f3467ad07add4f6b1

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require "yaml"

module Todoloo
  class TaskList
    def initialize
      @tasks = []
    end

    def add(task)
      if task.is_a?(Array)
        task.each { |t| add(t) }
      else
        raise ArgumentError, "Task type must be Todoloo::Task: #{task.inspect}" unless task.is_a?(Todoloo::Task)
        @tasks << task
      end

      self
    end

    # Structure:
    #   TOPIC:
    #     TYPE:
    #       - description:
    #         path:
    def write(path, error: :raise)
      output = YAML.dump(
        converted_tasks(
          error_handler: ErrorHandler.new(error)
        )
      )

      if path.is_a?(String)
        File.write(path, output)
      else
        path.write(output)
      end
    end

    private

    def converted_tasks(error_handler)
      output = {"" => {}}

      @tasks.each do |task|
        output_task = {
          "description" => FoldedString.new(task.description.to_s),
          "path" => "#{task.path}:#{task.line}:#{task.column}"
        }

        task.topics.each do |topic|
          by_type = output[topic.to_s] ||= {}
          tasks = by_type[task.type.to_s.downcase] ||= []
          tasks << output_task.dup
        end
      rescue Error => e
        if task.respond_to?(:line) && task.respond_to?(:column)
          error_handler.call("#{path}:#{task.line}:#{task.column}: Error: #{e}", original_exception: e)
        else
          error_handler.call("#{path}:??:??:Error: #{e}", original_exception: e)
        end
      end

      output
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
todoloo-0.0.1 lib/todoloo/task_list.rb