Class: Ztodo::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/ztodo/project.rb

Overview

Represent Project object.

Constant Summary collapse

LOW =
-1
NORMAL =
0
HIGH =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProject

Returns a new instance of Project.



14
15
16
17
# File 'lib/ztodo/project.rb', line 14

def initialize
  @loaded = false
  @data = {}
end

Instance Attribute Details

#loadedObject (readonly)

Returns the value of attribute loaded.



8
9
10
# File 'lib/ztodo/project.rb', line 8

def loaded
  @loaded
end

Instance Method Details

#add(task) ⇒ Object

Add task Raise en exception if key is already used

Raises:

  • (Exception)


47
48
49
50
# File 'lib/ztodo/project.rb', line 47

def add task
  raise Exception.new('Such key already exists') if @data.include?(task[:key].to_sym)
  @data[task[:key].to_sym] = task
end

#createObject

Create task (but not add it to data)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ztodo/project.rb', line 68

def create
  new_key = 0
  
  @data.each do |k, elem| 
    begin            
      curr = elem[:key].to_s.to_i
      new_key = curr if new_key < curr
    rescue
    end
  end

  new_key += 1
  {:key=>new_key.to_s, :description=>'put description here', 
    :done=>false, :priority=> Ztodo::Project::NORMAL}    
end

#init!Object

Init project file (.ztodo) in current directory



20
21
22
23
# File 'lib/ztodo/project.rb', line 20

def init!
  @data = {}
  save!
end

#load!Object

load project from file in current directory

Raises:

  • (Exception)


26
27
28
29
30
31
# File 'lib/ztodo/project.rb', line 26

def load!
  raise Exception.new('ztodo project file is not exists') unless File.exists?(Dir.pwd+'/.ztodo')

  @data = YAML::load(File.open(Dir.pwd+'/.ztodo'))
  @loaded = true
end

#modify(key, task) ⇒ Object

Modify task Raise en exception if there is no task with such key

Raises:

  • (Exception)


54
55
56
57
58
# File 'lib/ztodo/project.rb', line 54

def modify key, task
  raise Exception.new('No task with such key') unless @data.include?(key.to_sym)
  @data.delete key
  add task
end

#remove(key) ⇒ Object

Remove task Raise en exception if there is no task with such key

Raises:

  • (Exception)


62
63
64
65
# File 'lib/ztodo/project.rb', line 62

def remove key
  raise Exception.new('No task with such key') unless @data.include?(key.to_sym)
  @data.delete key
end

#save!Object

Save project to file in current directory



34
35
36
37
# File 'lib/ztodo/project.rb', line 34

def save!    
  File.open(Dir.pwd + '/.ztodo', 'w+') { |f| f.write(@data.to_yaml) }
  @loaded = true
end

#tasks(show_all = FALSE) ⇒ Object

Return all tasks



40
41
42
43
# File 'lib/ztodo/project.rb', line 40

def tasks show_all = FALSE
  return @data.reject {|k,v| v[:done]} if !show_all    
  @data.clone
end