Class: Nesta::FileModel

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

Direct Known Subclasses

Page

Defined Under Namespace

Classes: CaseInsensitiveHash

Constant Summary

FORMATS =
[:mdown, :md, :haml, :textile]
@@page_cache =
{}
@@filename_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileModel



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nesta/models.rb', line 75

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

#filenameObject (readonly)

Returns the value of attribute filename



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

def filename
  @filename
end

#mtimeObject (readonly)

Returns the value of attribute mtime



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

def mtime
  @mtime
end

Class Method Details

.find_allObject



31
32
33
34
35
36
37
# File 'lib/nesta/models.rb', line 31

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nesta/models.rb', line 39

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



58
59
60
61
62
63
# File 'lib/nesta/models.rb', line 58

def self.load(path)
  if (filename = find_file_for_path(path)) && needs_loading?(path, filename)
    @@page_cache[path] = self.new(filename)
  end
  @@page_cache[path]
end


70
71
72
73
# File 'lib/nesta/models.rb', line 70

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



27
28
29
# File 'lib/nesta/models.rb', line 27

def self.model_path(basename = nil)
  Nesta::Config.content_path(basename)
end

.needs_loading?(path, filename) ⇒ Boolean



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

def self.needs_loading?(path, filename)
  @@page_cache[path].nil? || File.mtime(filename) > @@page_cache[path].mtime
end

.purge_cacheObject



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

def self.purge_cache
  @@page_cache = {}
  @@filename_cache = {}
end

Instance Method Details

#abspathObject



91
92
93
94
95
96
97
98
# File 'lib/nesta/models.rb', line 91

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

#descriptionObject



124
125
126
# File 'lib/nesta/models.rb', line 124

def description
  ('description')
end

#flagged_as?(flag) ⇒ Boolean



136
137
138
139
# File 'lib/nesta/models.rb', line 136

def flagged_as?(flag)
  flags = ('flags')
  flags && flags.split(',').map { |name| name.strip }.include?(flag)
end

#index_page?Boolean



87
88
89
# File 'lib/nesta/models.rb', line 87

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

#keywordsObject



128
129
130
# File 'lib/nesta/models.rb', line 128

def keywords
  ('keywords')
end

#last_modifiedObject



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

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

#layoutObject



108
109
110
# File 'lib/nesta/models.rb', line 108

def layout
  (('layout') || 'layout').to_sym
end

#metadata(key) ⇒ Object



132
133
134
# File 'lib/nesta/models.rb', line 132

def (key)
  @metadata[key]
end

#parse_metadata(first_paragraph) ⇒ Object

Raises:



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nesta/models.rb', line 141

def (first_paragraph)
   = first_paragraph.split("\n").first =~ /^[\w ]+:/
  raise MetadataParseError unless 
   = CaseInsensitiveHash.new
  first_paragraph.split("\n").each do |line|
    key, value = line.split(/\s*:\s*/, 2)
    next if value.nil?
    [key.downcase] = value.chomp
  end
  
end

#pathObject



100
101
102
# File 'lib/nesta/models.rb', line 100

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


104
105
106
# File 'lib/nesta/models.rb', line 104

def permalink
  File.basename(path)
end

#templateObject



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

def template
  (('template') || 'page').to_sym
end

#to_html(scope = nil) ⇒ Object



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

def to_html(scope = nil)
  convert_to_html(@format, scope, markup)
end