Class: Nesta::FileModel
- Inherits:
-
Object
- Object
- Nesta::FileModel
- Defined in:
- lib/nesta/models.rb
Direct Known Subclasses
Constant Summary collapse
- FORMATS =
[:mdown, :haml, :textile]
- @@cache =
{}
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#mtime ⇒ Object
readonly
Returns the value of attribute mtime.
Class Method Summary collapse
- .find_all ⇒ Object
- .load(path) ⇒ Object
- .menu_items ⇒ Object
- .model_path(basename = nil) ⇒ Object
- .needs_loading?(path, filename) ⇒ Boolean
- .purge_cache ⇒ Object
Instance Method Summary collapse
- #abspath ⇒ Object
- #coderay(text) ⇒ Object
- #description ⇒ Object
- #index_page? ⇒ Boolean
-
#initialize(filename) ⇒ FileModel
constructor
A new instance of FileModel.
- #keywords ⇒ Object
- #last_modified ⇒ Object
- #layout ⇒ Object
- #metadata(key) ⇒ Object
- #path ⇒ Object
- #permalink ⇒ Object
- #template ⇒ Object
- #to_html(scope = nil) ⇒ Object
Constructor Details
#initialize(filename) ⇒ FileModel
Returns a new instance of FileModel.
58 59 60 61 62 63 |
# File 'lib/nesta/models.rb', line 58 def initialize(filename) @filename = filename @format = filename.split(".").last.to_sym parse_file @mtime = File.mtime(filename) end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
18 19 20 |
# File 'lib/nesta/models.rb', line 18 def filename @filename end |
#mtime ⇒ Object (readonly)
Returns the value of attribute mtime.
18 19 20 |
# File 'lib/nesta/models.rb', line 18 def mtime @mtime end |
Class Method Details
.find_all ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nesta/models.rb', line 24 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 |
.load(path) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nesta/models.rb', line 36 def self.load(path) FORMATS.each do |format| [path, File.join(path, 'index')].each do |basename| filename = model_path("#{basename}.#{format}") if File.exist?(filename) && needs_loading?(path, filename) @@cache[path] = self.new(filename) break end end end @@cache[path] end |
.menu_items ⇒ Object
53 54 55 56 |
# File 'lib/nesta/models.rb', line 53 def self. Nesta.deprecated('Page.menu_items', 'see Menu.top_level and Menu.for_path') Menu.top_level end |
.model_path(basename = nil) ⇒ Object
20 21 22 |
# File 'lib/nesta/models.rb', line 20 def self.model_path(basename = nil) Nesta::Config.content_path(basename) end |
.needs_loading?(path, filename) ⇒ Boolean
32 33 34 |
# File 'lib/nesta/models.rb', line 32 def self.needs_loading?(path, filename) @@cache[path].nil? || File.mtime(filename) > @@cache[path].mtime end |
.purge_cache ⇒ Object
49 50 51 |
# File 'lib/nesta/models.rb', line 49 def self.purge_cache @@cache = {} end |
Instance Method Details
#abspath ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/nesta/models.rb', line 69 def abspath page_path = @filename.sub(Nesta::Config.page_path, '') if index_page? File.dirname(page_path) else File.join(File.dirname(page_path), File.basename(page_path, '.*')) end end |
#coderay(text) ⇒ Object
105 106 107 108 109 |
# File 'lib/nesta/models.rb', line 105 def coderay(text) text.gsub(/^```\s(\w+?)\s+?(^.*?)^```/m) do CodeRay.scan($2, $1).div(:css => :class) end end |
#description ⇒ Object
115 116 117 |
# File 'lib/nesta/models.rb', line 115 def description ("description") end |
#index_page? ⇒ Boolean
65 66 67 |
# File 'lib/nesta/models.rb', line 65 def index_page? @filename =~ /\/?index\.\w+$/ end |
#keywords ⇒ Object
119 120 121 |
# File 'lib/nesta/models.rb', line 119 def keywords ("keywords") end |
#last_modified ⇒ Object
111 112 113 |
# File 'lib/nesta/models.rb', line 111 def last_modified @last_modified ||= File.stat(@filename).mtime end |
#layout ⇒ Object
86 87 88 |
# File 'lib/nesta/models.rb', line 86 def layout (("layout") || "layout").to_sym end |
#metadata(key) ⇒ Object
123 124 125 |
# File 'lib/nesta/models.rb', line 123 def (key) @metadata[key] end |
#path ⇒ Object
78 79 80 |
# File 'lib/nesta/models.rb', line 78 def path abspath.sub(/^\//, '') end |
#permalink ⇒ Object
82 83 84 |
# File 'lib/nesta/models.rb', line 82 def permalink File.basename(path) end |
#template ⇒ Object
90 91 92 |
# File 'lib/nesta/models.rb', line 90 def template (("template") || "page").to_sym end |
#to_html(scope = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/nesta/models.rb', line 94 def to_html(scope = nil) case @format when :mdown Maruku.new(coderay(markup)).to_html when :haml Haml::Engine.new(markup).to_html(scope || Object.new) when :textile RedCloth.new(markup).to_html end end |