Class: Nesta::FileModel

Inherits:
Object
  • Object
show all
Defined in:
lib/nesta/models.rb

Direct Known Subclasses

Page

Constant Summary collapse

FORMATS =
[:mdown, :haml, :textile]
@@cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#filenameObject (readonly)

Returns the value of attribute filename.



18
19
20
# File 'lib/nesta/models.rb', line 18

def filename
  @filename
end

#mtimeObject (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_allObject



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


53
54
55
56
# File 'lib/nesta/models.rb', line 53

def self.menu_items
  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

Returns:

  • (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_cacheObject



49
50
51
# File 'lib/nesta/models.rb', line 49

def self.purge_cache
  @@cache = {}
end

Instance Method Details

#abspathObject



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

#descriptionObject



115
116
117
# File 'lib/nesta/models.rb', line 115

def description
  ("description")
end

#index_page?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/nesta/models.rb', line 65

def index_page?
  @filename =~ /\/?index\.\w+$/
end

#keywordsObject



119
120
121
# File 'lib/nesta/models.rb', line 119

def keywords
  ("keywords")
end

#last_modifiedObject



111
112
113
# File 'lib/nesta/models.rb', line 111

def last_modified
  @last_modified ||= File.stat(@filename).mtime
end

#layoutObject



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

#pathObject



78
79
80
# File 'lib/nesta/models.rb', line 78

def path
  abspath.sub(/^\//, '')
end


82
83
84
# File 'lib/nesta/models.rb', line 82

def permalink
  File.basename(path)
end

#templateObject



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