Class: DiaryEditor::Diary

Inherits:
Object
  • Object
show all
Defined in:
lib/moon-diary/editor.rb

Constant Summary collapse

CONFIGURATION_FILE =
File.expand_path("~/.oneday")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, opts) ⇒ Diary

Returns a new instance of Diary.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



10
11
12
# File 'lib/moon-diary/editor.rb', line 10

def configuration
  @configuration
end

Instance Method Details

#configureObject



25
26
27
28
29
# 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_diaryObject



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



37
38
39
40
41
42
# 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_dirObject



73
74
75
76
77
78
79
80
# 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_dirObject



82
83
84
85
86
87
88
89
# 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_pathObject



31
32
33
34
35
36
# 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

#runObject



20
21
22
23
# File 'lib/moon-diary/editor.rb', line 20

def run
	configure
	create_diary
end

#save_to_dayone(file_path) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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_configurationObject



92
93
94
95
96
# 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