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

Returns a new instance of FileModel



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nesta/models.rb', line 70

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

.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

Returns:

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



86
87
88
89
90
91
92
93
# File 'lib/nesta/models.rb', line 86

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



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

def description
  ('description')
end

#flagged_as?(flag) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#index_page?Boolean

Returns:

  • (Boolean)


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

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

#keywordsObject



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

def keywords
  ('keywords')
end

#last_modifiedObject



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

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

#layoutObject



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

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

#metadata(key) ⇒ Object



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

def (key)
  @metadata[key]
end

#parse_metadata(first_paragraph) ⇒ Object

Raises:



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nesta/models.rb', line 136

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



95
96
97
# File 'lib/nesta/models.rb', line 95

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


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

def permalink
  File.basename(path)
end

#templateObject



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

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

#to_html(scope = Object.new) ⇒ Object



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

def to_html(scope = Object.new)
  convert_to_html(@format, scope, markup)
end