class Ztodo::Converter

Class for converting task hashes to strings and vice versa

Public Instance Methods

colored_priority(int_val) click to toggle source
# File lib/ztodo/converter.rb, line 27
def colored_priority int_val
  require 'colorize'
  if int_val == Ztodo::Project::LOW
    'low'.green
  elsif int_val == Ztodo::Project::NORMAL
    'normal'.yellow
  elsif int_val == Ztodo::Project::HIGH
    'high'.red
  else
    raise Exception.new('Priority should be -1, 0 or 1. Given: '+int_val)
  end
end
hash_to_str(hash) click to toggle source

Convert hash representation of task to string

# File lib/ztodo/converter.rb, line 18
def hash_to_str hash
  out = ''
  out += hash[:key].to_s+" # unique identifier of task\n"
  out += format_priority(hash[:priority])+" # priority: 'low', 'normal' or 'high'\n"
  out += (hash[:done] ? 'done' : 'undone' )+" # task completeness: 'done' or 'undone'\n"
  out += hash[:description].to_s
  out
end
str_to_hash(str) click to toggle source

Convert string representation of task to hash

# File lib/ztodo/converter.rb, line 7
def str_to_hash str
  out = {}
  data = str.split("\n")
  out[:key] = parse_key_line data[0]
  out[:priority] = parse_priority_line data[1]
  out[:done] = parse_completeness_line data[2]
  out[:description] = extract_description data.clone
  out
end

Protected Instance Methods

clear_line(line) click to toggle source

Return line without commented part

# File lib/ztodo/converter.rb, line 87
def clear_line line
  return nil if line.nil?

  if line.index('#').nil?
    res = line
  else
    res = line[0,line.index('#')]
  end
  res.strip!
  return res
end
extract_description(splitted_data) click to toggle source

Return description data

# File lib/ztodo/converter.rb, line 100
def extract_description splitted_data    
  return '' if splitted_data.size<=3
  3.times { splitted_data.slice! 0 }    
  splitted_data.join "\n"
end
format_priority(int_val) click to toggle source

Format priority integer value as string

# File lib/ztodo/converter.rb, line 43
def format_priority int_val
  if int_val == Ztodo::Project::LOW
    'low'
  elsif int_val == Ztodo::Project::NORMAL
    'normal'
  elsif int_val == Ztodo::Project::HIGH
    'high'
  else
    raise Exception.new('Priority should be -1, 0 or 1. Given: '+int_val)
  end
end
parse_completeness_line(line) click to toggle source

Parse line with completeness data

# File lib/ztodo/converter.rb, line 80
def parse_completeness_line line    
  line = clear_line line    
  return line == 'done'
end
parse_key_line(line) click to toggle source

Parse line with unique identifier

# File lib/ztodo/converter.rb, line 56
def parse_key_line line
  line = clear_line line    
  if line.nil? || line.empty? 
    'task'+Random.rand(1000).to_s
  else 
    line
  end
end
parse_priority_line(line) click to toggle source

Parse line with priority data

# File lib/ztodo/converter.rb, line 66
def parse_priority_line line
  return Ztodo::Project::NORMAL if line.nil? || line.empty?
  line = clear_line line    

  if line == 'low'
    Ztodo::Project::LOW    
  elsif line == 'high'
    Ztodo::Project::HIGH
  else # 'normal' and any other option
    Ztodo::Project::NORMAL
  end
end