Class: DraftManager
Instance Attribute Summary collapse
Instance Method Summary
collapse
#check_count, #check_filename, #check_index, #delete_attribute, #delete_index, #ensure_not_deleted, #get_attribute, #load_index, #print_index, #save_index, #set_attribute
Instance Attribute Details
#draft_dir ⇒ Object
Returns the value of attribute draft_dir.
27
28
29
|
# File 'lib/draft_manager.rb', line 27
def draft_dir
@draft_dir
end
|
#index_file ⇒ Object
Returns the value of attribute index_file.
28
29
30
|
# File 'lib/draft_manager.rb', line 28
def index_file
@index_file
end
|
#launch_editor_cmd ⇒ Object
Returns the value of attribute launch_editor_cmd.
26
27
28
|
# File 'lib/draft_manager.rb', line 26
def launch_editor_cmd
@launch_editor_cmd
end
|
#temp_dir ⇒ Object
Returns the value of attribute temp_dir.
27
28
29
|
# File 'lib/draft_manager.rb', line 27
def temp_dir
@temp_dir
end
|
Instance Method Details
#create_draft ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/draft_manager.rb', line 51
def create_draft
temp_file = Tempfile.new('draft', @temp_dir)
temp_file.close
if system "#{@launch_editor_cmd} #{temp_file.path}"
if temp_file.size > 0
move_temp_file temp_file.path
end
else
msg = "couldn't launch editor with command #{@launch_editor_cmd}"
raise RuntimeError, msg
end
print_index if @verbose
end
|
#delete_draft(filename) ⇒ Object
90
91
92
93
|
# File 'lib/draft_manager.rb', line 90
def delete_draft(filename)
set_attribute(filename, "deleted", Time.now.to_s)
print_index if @verbose
end
|
#edit_draft(filename) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/draft_manager.rb', line 77
def edit_draft(filename)
check_filename(filename) do |draft_filename|
if system "#{@launch_editor_cmd} #{draft_filename}"
set_attribute(filename, "edited_time", Time.now.to_s)
else
msg = "couldn't launch editor with command #{@launch_editor_cmd}"
raise RuntimeError, msg
end
end
print_index if @verbose
end
|
#get_draft_by_title(title) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/draft_manager.rb', line 154
def get_draft_by_title(title)
check_count do
index = load_index
index.keys.each do |key|
draft_title = index[key]["title"]
return key if draft_title == title
end
end
end
|
#get_latest_created_draft ⇒ Object
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/draft_manager.rb', line 143
def get_latest_created_draft
check_count do
index = load_index
index.reject do |key, val|
val["deleted"] != nil
end.sort do |a,b|
Time.parse(a[1]["creation_time"]) <=> Time.parse(b[1]["creation_time"])
end[-1][0]
end
end
|
#list ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/draft_manager.rb', line 95
def list
block = Enumerator.new do |g|
index = load_index
index.keys.each do |k|
g << k if index[k]["deleted"] == nil
end
end
block
end
|
#move_temp_file(tempfile) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/draft_manager.rb', line 67
def move_temp_file(tempfile)
if system "mv #{tempfile} #{@draft_dir}"
index_key = File.basename(tempfile)
set_attribute(index_key, "creation_time", Time.now.to_s)
else
msg = "failed to move the temp file to the draft folder"
raise RuntimeError, msg
end
end
|
#output(filename) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/draft_manager.rb', line 125
def output(filename)
ensure_not_deleted filename
check_filename(filename) do |draft_filename|
File.open(draft_filename, 'r') do |f|
if f.eof?
''
else
text = ''
f.each do |l|
text += l
end
text
end
end
end
end
|
#setup(editor_cmd, temp_dir, draft_dir, options = {}) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/draft_manager.rb', line 32
def setup(editor_cmd, temp_dir, draft_dir, options = {})
@verbose = options[:verbose] ||= false
@launch_editor_cmd = editor_cmd
@temp_dir = temp_dir
@draft_dir = draft_dir
@index_file = "#{@draft_dir}/.draft_index"
value = true
value = system "touch #{@index_file}" unless File.exists? @index_file
if value
print_index if @verbose
else
msg = 'could not create the draft index file'
raise RuntimeError, msg
end
end
|
#show_info(filename) ⇒ Object
106
107
108
109
110
|
# File 'lib/draft_manager.rb', line 106
def show_info(filename)
check_index(filename) do |full_index, index|
index.to_json
end
end
|
#show_preview(filename) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/draft_manager.rb', line 112
def show_preview(filename)
check_filename(filename) do |draft_filename|
File.open(draft_filename, 'r') do |f|
if f.eof?
''
else
line = f.readline.gsub("\n", '')
line.length > 76 ? line[0..75] + '...' : line
end
end
end
end
|