<repo_name>kazukousen/dadan<file_sep>/lib/dadan/time.rb
class Time
  def yesterday
    self - 60*60*24
  end
end
<file_sep>/lib/dadan/memo.rb
require 'zlib'
require 'fileutils'

module Dadan
  class Memo

    module WorkSpace
      Dadan = "#{Dir.home}/.dadan"
      Objects = "#{Dadan}/objects"
      Path = "#{Dadan}/worker.md"
    end

    def initialize(editor)
      @editor = editor
      if File.exists?(WorkSpace::Dadan)
        FileUtils.rm(WorkSpace::Path) if File.exists?(WorkSpace::Path)
      else
        FileUtils.mkdir_p(WorkSpace::Dadan)
      end
    end

    attr_reader :editor

    def edit
      system("#{@editor} #{WorkSpace::Path}")
    end

    def save
      time = Time.now.strftime("%Y%m%d%H%M%S")
      packed_body = Zlib::Deflate.deflate(File.read(WorkSpace::Path))
      FileUtils.mkdir_p(WorkSpace::Objects) unless File.exists?(WorkSpace::Objects)

      open("#{WorkSpace::Objects}/#{time}", 'w') do |file|
        file.write packed_body
      end

      time
    end

    def self.open_dayfile(day)
      statements = ""
      Dir.glob("#{Dir.home}/.dadan/objects/#{day}*").each do |file|
        statements << self.open_object(file[-14..-1])
        statements << "---\n"
      end
      puts statements
    end

    def self.open_file(time)
      puts self.open_object(time)
    end

    private
    def self.open_object(time)
      object_path = "#{WorkSpace::Objects}/#{time}"
      raise 'Invalid File at this time' unless File.exists?(object_path)
      Zlib::Inflate.inflate(File.read(object_path))
    end
  end
end
<file_sep>/lib/dadan/cli.rb
# coding: utf-8
require 'dadan'
require 'thor'

module Dadan
  class CLI < Thor
    module Worker
      Editor = 'vi'
      Path = "#{Dir.home}/.dadan/worker.md"
    end
    default_command :first_run

    desc 'hello NAME', 'say hello to NAME'
    def hello(name)
      puts 'hello ' << name
    end

    desc 'run', 'default new file'
    def first_run
      memo = Memo.new(Worker::Editor)
      memo.edit
      time = memo.save
      puts "Memorized at #{time}"
    end

    desc 'worker', 'workspace'
    def worker
      dir = "#{Dir.home}/.dadan/"
      FileUtils.mkdir_p(dir) unless File.exists?(dir)

      worker_path = "#{dir}/worker.md"

      FileUtils.rm(worker_path) if File.exists?(worker_path)
    end

    desc 'edior', 'vim output'
    def edit
      system("#{Worker::Editor} #{Worker::Path}")
    end

    desc 'save', 'file save'
    def save
      Memo.save_file(Worker::Path)
    end

    desc "today's file", "today's file open"
    def today
      day = Time.now.strftime("%Y%m%d")
      Memo.open_dayfile(day)
    end

    desc "yesterday's file", "yesterday's file open"
    def yesterday
      day = Time.now.yesterday.strftime("%Y%m%d")
      Memo.open_dayfile(day)
    end

    desc "day's file", "day's file open"
    def day(daytime)
      if daytime.length == 4
        year = Time.now.strftime("%Y")
        daytime = "#{year}#{daytime}"
      end
      Memo.open_dayfile(daytime)
    end
  end
end
<file_sep>/lib/dadan.rb
require 'dadan/version'
require 'dadan/memo'
require 'dadan/time'
require 'dadan/cli'
module Dadan
  # Your code goes here...
end
<file_sep>/exe/dadan
#!/usr/bin/env ruby

require "dadan"

Dadan::CLI.start(ARGV)
