Sha256: ef81d0dc61e2f597562535fe4a8b558700a33717f52b18fa57bb0fb4568765c4

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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 topics_for_task(task)
      if task.topics.empty?
        [""]
      else
        task.topics
      end
    end

    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}"
        }

        topics_for_task(task).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.2 lib/todoloo/task_list.rb