Class: Nesta::FileModel
- Inherits:
-
Object
show all
- Defined in:
- lib/nesta/models/file_model.rb
Direct Known Subclasses
Page
Defined Under Namespace
Classes: CaseInsensitiveHash
Constant Summary
collapse
- FORMATS =
[:mdown, :md, :haml, :textile]
- @@model_cache =
{}
- @@filename_cache =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(filename) ⇒ FileModel
Returns a new instance of FileModel.
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/nesta/models/file_model.rb', line 73
def initialize(filename)
@filename = filename
@format = filename.split('.').last.to_sym
if File.zero?(filename)
@metadata = {}
@markup = ''
else
@metadata, @markup = parse_file
end
@mtime = File.mtime(filename)
end
|
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
22
23
24
|
# File 'lib/nesta/models/file_model.rb', line 22
def filename
@filename
end
|
#mtime ⇒ Object
Returns the value of attribute mtime.
22
23
24
|
# File 'lib/nesta/models/file_model.rb', line 22
def mtime
@mtime
end
|
Class Method Details
.find_all ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/nesta/models/file_model.rb', line 34
def self.find_all
file_pattern = File.join(model_path, "**", "*.{#{FORMATS.join(',')}}")
Dir.glob(file_pattern).map do |path|
relative = path.sub("#{model_path}/", "")
load(relative.sub(/\.(#{FORMATS.join('|')})/, ""))
end
end
|
.find_file_for_path(path) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/nesta/models/file_model.rb', line 42
def self.find_file_for_path(path)
if ! @@filename_cache.has_key?(path)
FORMATS.each do |format|
[path, File.join(path, 'index')].each do |basename|
filename = model_path("#{basename}.#{format}")
if File.exist?(filename)
@@filename_cache[path] = filename
break
end
end
end
end
@@filename_cache[path]
end
|
.load(path) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/nesta/models/file_model.rb', line 61
def self.load(path)
if (filename = find_file_for_path(path)) && needs_loading?(path, filename)
@@model_cache[path] = self.new(filename)
end
@@model_cache[path]
end
|
.model_path(basename = nil) ⇒ Object
30
31
32
|
# File 'lib/nesta/models/file_model.rb', line 30
def self.model_path(basename = nil)
Nesta::Config.content_path(basename)
end
|
.needs_loading?(path, filename) ⇒ Boolean
57
58
59
|
# File 'lib/nesta/models/file_model.rb', line 57
def self.needs_loading?(path, filename)
@@model_cache[path].nil? || File.mtime(filename) > @@model_cache[path].mtime
end
|
.purge_cache ⇒ Object
68
69
70
71
|
# File 'lib/nesta/models/file_model.rb', line 68
def self.purge_cache
@@model_cache = {}
@@filename_cache = {}
end
|
Instance Method Details
#==(other) ⇒ Object
85
86
87
|
# File 'lib/nesta/models/file_model.rb', line 85
def ==(other)
other.respond_to?(:path) && (self.path == other.path)
end
|
#abspath ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/nesta/models/file_model.rb', line 93
def abspath
file_path = @filename.sub(self.class.model_path, '')
if index_page?
File.dirname(file_path)
else
File.join(File.dirname(file_path), File.basename(file_path, '.*'))
end
end
|
#description ⇒ Object
126
127
128
|
# File 'lib/nesta/models/file_model.rb', line 126
def description
metadata('description')
end
|
#flagged_as?(flag) ⇒ Boolean
138
139
140
141
|
# File 'lib/nesta/models/file_model.rb', line 138
def flagged_as?(flag)
flags = metadata('flags')
flags && flags.split(',').map { |name| name.strip }.include?(flag)
end
|
#index_page? ⇒ Boolean
89
90
91
|
# File 'lib/nesta/models/file_model.rb', line 89
def index_page?
@filename =~ /\/?index\.\w+$/
end
|
#keywords ⇒ Object
130
131
132
|
# File 'lib/nesta/models/file_model.rb', line 130
def keywords
metadata('keywords')
end
|
#last_modified ⇒ Object
122
123
124
|
# File 'lib/nesta/models/file_model.rb', line 122
def last_modified
@last_modified ||= File.stat(@filename).mtime
end
|
#layout ⇒ Object
110
111
112
|
# File 'lib/nesta/models/file_model.rb', line 110
def layout
(metadata('layout') || 'layout').to_sym
end
|
134
135
136
|
# File 'lib/nesta/models/file_model.rb', line 134
def metadata(key)
@metadata[key]
end
|
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/nesta/models/file_model.rb', line 143
def parse_metadata(first_paragraph)
is_metadata = first_paragraph.split("\n").first =~ /^[\w ]+:/
raise MetadataParseError unless is_metadata
metadata = CaseInsensitiveHash.new
first_paragraph.split("\n").each do |line|
key, value = line.split(/\s*:\s*/, 2)
next if value.nil?
metadata[key.downcase] = value.chomp
end
metadata
end
|
#path ⇒ Object
102
103
104
|
# File 'lib/nesta/models/file_model.rb', line 102
def path
abspath.sub(/^\//, '')
end
|
#permalink ⇒ Object
106
107
108
|
# File 'lib/nesta/models/file_model.rb', line 106
def permalink
File.basename(path)
end
|
#template ⇒ Object
114
115
116
|
# File 'lib/nesta/models/file_model.rb', line 114
def template
(metadata('template') || 'page').to_sym
end
|
#to_html(scope = Object.new) ⇒ Object
118
119
120
|
# File 'lib/nesta/models/file_model.rb', line 118
def to_html(scope = Object.new)
convert_to_html(@format, scope, markup)
end
|