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



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

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



21
22
23
# File 'lib/nesta/models.rb', line 21

def filename
  @filename
end

#mtimeObject (readonly)

Returns the value of attribute mtime



21
22
23
# File 'lib/nesta/models.rb', line 21

def mtime
  @mtime
end

Class Method Details

.find_allObject



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

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



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

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



60
61
62
63
64
65
# File 'lib/nesta/models.rb', line 60

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



29
30
31
# File 'lib/nesta/models.rb', line 29

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

.needs_loading?(path, filename) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/nesta/models.rb', line 56

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

.purge_cacheObject



67
68
69
70
# File 'lib/nesta/models.rb', line 67

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

Instance Method Details

#==(other) ⇒ Object



84
85
86
# File 'lib/nesta/models.rb', line 84

def ==(other)
  other.respond_to?(:path) && (self.path == other.path)
end

#abspathObject



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

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

#descriptionObject



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

def description
  ('description')
end

#flagged_as?(flag) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#index_page?Boolean

Returns:

  • (Boolean)


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

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

#keywordsObject



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

def keywords
  ('keywords')
end

#last_modifiedObject



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

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

#layoutObject



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

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

#metadata(key) ⇒ Object



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

def (key)
  @metadata[key]
end

#parse_metadata(first_paragraph) ⇒ Object

Raises:



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

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



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

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


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

def permalink
  File.basename(path)
end

#templateObject



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

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

#to_html(scope = Object.new) ⇒ Object



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

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