Sha256: cdcb736109ef1f3f8787ae9a623f6355a0d19ef909c732fb5e093a0e2f53d44a

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 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, body, type)
      raw_tasks = body['data']
      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

5 entries across 5 versions & 1 rubygems

Version Path
habitica_cli-1.0.2 lib/habitica_cli/commands/shared.rb
habitica_cli-1.0.1 lib/habitica_cli/commands/shared.rb
habitica_cli-1.0.0 lib/habitica_cli/commands/shared.rb
habitica_cli-0.1.1 lib/habitica_cli/commands/shared.rb
habitica_cli-0.1.0 lib/habitica_cli/commands/shared.rb