class DiaryEditor::Diary

Constants

CONFIGURATION_FILE

Attributes

configuration[RW]

Public Class Methods

new(*args, opts) click to toggle source
# File lib/moon-diary/editor.rb, line 11
def initialize(*args, opts)
        @day = Time.now.strftime("%d")
        @month = Time.now.strftime("%m")
        @year = Time.now.strftime("%Y")

        say "#{@day}"
        say "#{@month}"
end

Public Instance Methods

configure() click to toggle source
# File lib/moon-diary/editor.rb, line 25
def configure
        FileUtils.touch(CONFIGURATION_FILE) unless File.exist?(CONFIGURATION_FILE)
        @configuration = JSON::load(File.open(CONFIGURATION_FILE)) || {}
        diary_path unless @configuration['path']
end
create_diary() click to toggle source
# File lib/moon-diary/editor.rb, line 43
    def create_diary
            diary_path = "#{create_month_dir}/#{@year}-#{@month}-#{@day}.md"

            FileUtils.touch(diary_path) unless File.exist?(diary_path)

cmd = ["vim", '--nofork', diary_path].join(' ')
    system(cmd) or raise SystemCallError, "`#{cmd}` gave exit status: #{$?.exitstatus}"
    save_to_dayone(diary_path)
    end
create_directory(path) click to toggle source
# File lib/moon-diary/editor.rb, line 37
def create_directory(path)
                say path
                unless File.directory?(path)
                        FileUtils.mkdir_p(path)
                end
        end
create_month_dir() click to toggle source
# File lib/moon-diary/editor.rb, line 73
def create_month_dir
        month_dir = "#{create_year_dir}/#{@month}月"
        
        unless File.directory?(month_dir)
                FileUtils.mkdir_p(month_dir)
        end
        month_dir
end
create_year_dir() click to toggle source
# File lib/moon-diary/editor.rb, line 82
def create_year_dir
        year_dir = "#{@configuration['path']}/#{@year}"

        unless File.directory?(year_dir)
                FileUtils.mkdir_p(year_dir)
        end
        year_dir
end
diary_path() click to toggle source
# File lib/moon-diary/editor.rb, line 31
def diary_path
        path = ask("You should input the directory of diary.") { |q| q.default = "none" }
        @configuration['path'] = path
        create_directory(path)
        write_configuration
end
run() click to toggle source
# File lib/moon-diary/editor.rb, line 20
def run
        configure
        create_diary
end
save_to_dayone(file_path) click to toggle source
# File lib/moon-diary/editor.rb, line 53
def save_to_dayone(file_path)
        if (which("dayone"))
                cmd = ["dayone -d=#{@day}/#{@month}/#{@year}",'-s=true new <', file_path].join(' ')
                system(cmd) or raise SystemCallError, "`#{cmd}` gave exit status: #{$?.exitstatus}"
        else
                say "if you want to save to dayone, you can visit http://dayoneapp.com/tools/cli-man/"
        end
end
which(cmd) click to toggle source
# File lib/moon-diary/editor.rb, line 62
def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable? exe
    }
  end
  return nil
end
write_configuration() click to toggle source
# File lib/moon-diary/editor.rb, line 92
def write_configuration
        File.open(CONFIGURATION_FILE, "w") do |file|
                file.write @configuration.to_json
  end
end