Class: Retter::Entries
- Inherits:
-
Array
- Object
- Array
- Retter::Entries
show all
- Extended by:
- Configurable
- Defined in:
- lib/retter/entries.rb
Instance Method Summary
collapse
configurable, define_configurable_method, define_instance_shortcut_method
Constructor Details
Returns a new instance of Entries.
16
17
18
|
# File 'lib/retter/entries.rb', line 16
def initialize
load_entries retters_dir
end
|
Instance Method Details
#commit_wip_entry! ⇒ Object
75
76
77
78
79
80
81
82
83
|
# File 'lib/retter/entries.rb', line 75
def commit_wip_entry!
if wip_file.exist?
copy = wip_file.read
retter_file(Date.today).open('a') {|f| f.puts copy }
wip_file.unlink
end
reload
end
|
#detect_by_date(date) ⇒ Object
57
58
59
|
# File 'lib/retter/entries.rb', line 57
def detect_by_date(date)
detect {|e| e.date == date }
end
|
#detect_by_filename(filename) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/retter/entries.rb', line 44
def detect_by_filename(filename)
case filename
when wip_file.basename.to_path
wip_entry
else
detect {|e| e.path.basename.to_path == filename }
end
end
|
#detect_by_string(str) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/retter/entries.rb', line 24
def detect_by_string(str)
entry =
case str
when nil, ''
detect_by_today || wip_entry
when /^e([0-9]+)$/
index = $1.to_i
self[index]
when /^([0-9a-z]+\.md)$/
detect_by_filename($1)
else
date = parse_date_string(str)
detect_by_date(date) || wip_entry(date)
end
raise EntryLoadError, "Entry not found with keyword '#{str}'" unless entry
entry
end
|
#detect_by_today ⇒ Object
53
54
55
|
# File 'lib/retter/entries.rb', line 53
def detect_by_today
detect_by_date(Date.today)
end
|
#find_markup_files(path) ⇒ Object
102
103
104
105
|
# File 'lib/retter/entries.rb', line 102
def find_markup_files(path)
path = Pathname.new(path).realpath
Dir.open(path, &:to_a).grep(/^\d{4}(?:0[1-9]|1[012])(?:0[1-9]|[12][0-9]|3[01])\..*$/).map {|f| path.join f }
end
|
#load_entries(path) ⇒ Object
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/retter/entries.rb', line 85
def load_entries(path)
date_files = find_markup_files(path).map {|file|
date_str = file.basename('.*').to_path
[Date.parse(date_str), file]
}.sort_by(&:first)
date_files.reverse_each {|date, file|
self << Entry.new(date: date, body: rendered_body(file.read), entries: self)
}
end
|
#parse_date_string(date_str) ⇒ Object
61
62
63
64
65
|
# File 'lib/retter/entries.rb', line 61
def parse_date_string(date_str)
normalized = date_str.gsub(/\./, ' ')
(Chronic.parse(normalized) || Date.parse(normalized)).to_date
end
|
#reload ⇒ Object
96
97
98
99
100
|
# File 'lib/retter/entries.rb', line 96
def reload
clear
load_entries retters_dir
end
|
#rendered_body(body) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/retter/entries.rb', line 107
def rendered_body(body)
key = Digest::SHA1.hexdigest('entry_' + body)
cache.fetch(key) do
(markup || Markdown.instance).render(body)
end
end
|
#retter_file(date) ⇒ Object
20
21
22
|
# File 'lib/retter/entries.rb', line 20
def retter_file(date)
retters_dir.join(date ? date.strftime('%Y%m%d.md') : 'today.md')
end
|
#wip_entry(date = nil) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/retter/entries.rb', line 67
def wip_entry(date = nil)
file = retter_file(date)
date = date || Date.today
body = file.exist? ? file.read : ''
Entry.new date: date, body: rendered_body(body), path: file, entries: self
end
|