Sha256: 6d1336eeaff04f059abbe94e0677edfe5fc3bea660a7b918281d929faad2cb5a

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module HabiticaCli
  # Functionality common to all commands
  module Commands
    def self.validate_type(type)
      types = %w(todo habit daily)
      fail "Not a valid type (#{types})" unless types.include?(type)
    end

    def self.filter_tasks(env, tasks, type = nil)
      tasks.select do |task|
        (type.nil? || task['type'] == type) &&
          (env.options['show_completed'] == true || task['completed'] != true)
      end
    end

    def self.select_attributes(tasks)
      keys = %w(completed id text type)
      tasks.map do |task|
        task.select { |k, _| keys.include?(k) }
      end
    end

    def self.cache_tasks(env, tasks, type)
      env.cache.store_tasks(
        select_attributes(filter_tasks(env, tasks, type))
      )
    end

    def self.display(env, raw_tasks, type)
      tasks = cache_tasks(env, raw_tasks, type)
      puts type.capitalize unless type.nil?
      tasks.each do |item|
        output = type.nil? ? "(#{item['type']}) " : ''
        output += "[#{item['cid']}] #{item['text']}"
        puts output
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
habitica_cli-0.0.1 lib/habitica_cli/commands/shared.rb