class DuyojiTodo::Command

コマンドラインベースの処理を行うクラスです @author Tsuyoshi Maeda

Public Class Methods

new(argv) click to toggle source
# File lib/duyoji_todo/command.rb, line 15
def initialize(argv)
        @argv = argv
end
run(argv) click to toggle source
# File lib/duyoji_todo/command.rb, line 10
def run(argv)
        new(argv).execute
end

Public Instance Methods

create_task(name, content) click to toggle source
# File lib/duyoji_todo/command.rb, line 45
          def create_task(name, content)
# タスク作成時のstatusはデフォルト値が使われNOT_YETとなる
Task.create!(name: name, content: content).reload
          end
delete_task(id) click to toggle source
# File lib/duyoji_todo/command.rb, line 50
          def delete_task(id)
task = Task.find(id)
                  task.destroy
          end
execute() click to toggle source
# File lib/duyoji_todo/command.rb, line 19
          def execute
options     = Options.parse!(@argv)
                  sub_command = options.delete(:command)

                  #puts options

                  DB.prepare

                  tasks = case sub_command
                                                  when 'create'
                                                          create_task(options[:name], options[:content])
                                                  when 'delete'
                                                          delete_task(options[:id])
                                                  when 'update'
                                                          update_task(options.delete(:id), options)
                                                  when 'list'
                                                          find_tasks(options[:status])
                                                  end

                  display_tasks tasks

          rescue => e
                  abort "Error: #{e.message}"

          end
find_tasks(status_name) click to toggle source
# File lib/duyoji_todo/command.rb, line 66
def find_tasks(status_name)
        all_tasks = Task.order('created_at DESC')

        if status_name
                status = DuyojiTodo::Task::STATUS.fetch(status_name.upcase)
                all_tasks.status_is(status)
        else
                all_tasks
        end
end
update_task(id, attributes) click to toggle source
# File lib/duyoji_todo/command.rb, line 55
def update_task(id, attributes)
        if status_name = attributes[:status]
                attributes[:status] = DuyojiTodo::Task::STATUS.fetch(status_name.upcase)
        end

        task = Task.find(id)
        task.update_attributes! attributes

        task.reload
end

Private Instance Methods

display_format(id, name, content, status) click to toggle source

タスク表示のフォーマット化

# File lib/duyoji_todo/command.rb, line 92
def display_format(id, name, content, status)
        name_length    = 20 - full_width_count(name)
        content_length = 40 - full_width_count(content)

        [ id.to_s.rjust(4), name.ljust(name_length), content.ljust(content_length), status.ljust(8) ].join(' | ')
end
display_tasks(tasks) click to toggle source

タスクの表示

# File lib/duyoji_todo/command.rb, line 80
def display_tasks(tasks)
        header = display_format('ID', 'Name', 'Content', 'Status')

        puts header
        puts '-' * header.size
        Array(tasks).each do |task|
                puts display_format(task.id, task.name, task.content, task.status_name)
        end

end
full_width_count(string) click to toggle source

リスト表示の際の文字調整

# File lib/duyoji_todo/command.rb, line 100
def full_width_count(string)
        string.each_char.select{ |char| !( /[ -~.]/.match(char) ) }.count
end