Sha256: 5743f5ffac12e586d9b4b6554a652ef6a90da8f5863fec0af4ad2eabfc247574

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'shellwords'

module TaskwarriorWeb::CommandBuilder
  module Base

    TASK_COMMANDS = {
      :add => 'add',
      :query => '_query',
      :count => 'count',
      :complete => ':id done',
      :projects => '_projects',
      :tags => '_tags'
    }    

    def build
      unless @command_string
        task_command
        substitute_parts if @command_string =~ /:id/
      end
      parse_params
      @built = "#{@command_string}#{@params}"
    end

    def task_command
      if TASK_COMMANDS.has_key?(@command.to_sym)
        @command_string = TASK_COMMANDS[@command.to_sym].clone
      else
        raise InvalidCommandError
      end
    end

    def substitute_parts
      if @id
        @command_string.gsub!(':id', "uuid:#{@id.to_s}")
        return self
      else
        raise TaskwarriorWeb::CommandBuilder::MissingTaskIDError
      end
    end

    def parse_params
      string = ''
      string << " #{@params.delete(:description).shellescape}" if @params.has_key?(:description)

      if @params.has_key?(:tags)
        tags = @params.delete(:tags)
        tag_indicator = TaskwarriorWeb::Config.property('tag.indicator') || '+'
        tags.each { |tag| string << " #{tag_indicator}#{tag.to_s.shellescape}" } 
      end

      @params.each do |attr, value|
        if value.respond_to? :each
          value.each { |val| string << " #{attr.to_s}:#{val.to_s.shellescape}" }
        else
          string << " #{attr.to_s}:#{value.to_s}"
        end
      end

      @params = string
      return self
    end

  end

  class InvalidCommandError < Exception; end
  class MissingTaskIDError < Exception; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
taskwarrior-web-1.0.5 lib/taskwarrior-web/command_builders/base.rb