Class: Dst

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/dst.rb,
lib/dst/models.rb

Defined Under Namespace

Modules: Models

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models

create_tables_if_necessary, establish_connection, schema

Class Method Details

.process_command(command) ⇒ Object



7
8
9
# File 'lib/dst.rb', line 7

def self.process_command(command)
  new.process_command(command)
end

Instance Method Details

#create_task(options = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dst.rb', line 22

def create_task(options={})
  task = Task.new(:description => options[:description])
  task.context = Context.find_or_create_by_name(options[:context]) if options.has_key?(:context)
  task.project = Project.find_or_create_by_name(options[:project]) if options.has_key?(:project)
  task.save
  puts "`#{task}' created."
end

#list_tasks(options = {}, include_completed = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dst.rb', line 37

def list_tasks(options={}, include_completed=false)
  context, project = options.values_at(:context, :project)
  tasks = if options.empty?
            Task.unfinished
          elsif context
            Task.unfinished.select { |task|
              task.context.name == context if task.context
            }
          elsif project
            Task.unfinished.select { |task|
              task.project.name == project if task.project
            }
          end || []
  puts tasks.empty? ? 'No tasks found' : tasks.map(&:to_s).join("\n")
end

#process_command(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/dst.rb', line 11

def process_command(command)
  options = extract_options_from_command(command)
  if command.blank? || !options.has_key?(:description)
    list_tasks(options)
  elsif command =~ /^\^(\d+)$/
    toggle_task($1.to_i)
  else
    create_task(options)
  end
end

#toggle_task(task_id) ⇒ Object



30
31
32
33
34
35
# File 'lib/dst.rb', line 30

def toggle_task(task_id)
  task = Task.toggle!(task_id)
  puts "Ok, `#{task}' marked as `#{task.status}'."    
rescue ActiveRecord::RecordNotFound
  puts "Oops, task ##{task_id} not found."
end