41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/days/helpers.rb', line 41
def lookup_entry(path)
regexp = Regexp.compile(Regexp.escape(config.permalink).gsub(/\\{(\w+?)\\}/) { "(?<#{$1}>.+?)" } + "$")
m = regexp.match(path)
return nil unless m
match = m.names.inject({}) do |hash, k|
hash[k.to_sym] = m[k]
hash
end
if match[:id] || match[:slug]
if match[:id]
query = Entry.where(id: match[:id])
else
query = Entry.where(slug: match[:slug])
end
entry = query.first
return nil unless entry
published_at = entry.published_at
return nil unless published_at
return nil if match[:slug] && match[:slug] != entry.slug
return nil if match[:id] && match[:id].to_i != entry.id
return nil if match[:year] && match[:year].to_i != published_at.year
return nil if match[:month] && match[:month].to_i != published_at.month
return nil if match[:day] && match[:day].to_i != published_at.day
return nil if match[:hour] && match[:hour].to_i != published_at.hour
return nil if match[:minute] && match[:minute].to_i != published_at.min
return nil if match[:second] && match[:second].to_i != published_at.sec
return entry
else
match_time = {}.tap do |h|
[:year, :month, :day, :hour, :minute, :second].each do |k|
h[k] = match[k] && match[k].to_i
end
end
range_begin = Time.local(
match_time[:year], match_time[:month] || 1, match_time[:day] || 1,
match_time[:hour] || 0, match_time[:minute] || 0, match_time[:second] || 0
)
range_end = Time.local(
match_time[:year], match_time[:month] || 12, match_time[:day] || 31,
match_time[:hour] || 23, match_time[:minute] || 59, match_time[:second] || 59
)
query = Entry.where(published_at: (range_begin .. range_end))
if query.count.zero?
return nil
elsif query.count == 1
return query.first
else
return query
end
end
end
|