Class: Webby::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/resource.rb

Overview

A Webby::Resource is any file that can be found in the content directory or in the layout directory. This class contains information about the resources available to Webby. This information includes the resource type (static, page, layout), if the resource is dirty (it needs to be rendered), the output location of the rendered resource, etc.

A resource is a “layout” if the resource is found in the layout directory. Static and page resources are found in the content directory.

A resource is considered static only if it *does not* contain a YAML meta-data header at the top of the file. These resources will be copied as-is from the content directory to the output directory.

If a resouce does have meta-data, then it will be processed (i.e. rendered/filtered) by Webby, and the rendered results will be written to the output directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ Resource

call-seq:

Resource.new( filename )    => resource

Creates a new resource object given the filename.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/webby/resource.rb', line 62

def initialize( fn )
  @path     = fn.sub(%r/\A(?:\.\/|\/)/o, '').freeze
  @dir      = ::File.dirname(@path).sub(%r/\A[^\/]+\/?/o, '')
  @filename = ::File.basename(@path).sub(%r/\.\w+\z/o, '')
  @ext      = ::File.extname(@path).sub(%r/\A\.?/o, '')
  @mtime    = ::File.mtime @path

  @rendering = false

  # deal with the meta-data
  @mdata = ::Webby::File.(@path)
  @have_mdata = !@mdata.nil?

  @mdata ||= {}
  @mdata = ::Webby.page_defaults.merge(@mdata) if is_page?
  @mdata.sanitize!

  self.class.pages << self if is_page? or is_static?
  self.class.layouts << self if is_layout?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a, &b) ⇒ Object

call-seq:

method_missing( symbol [, *args, &block] )    => result

Invoked by Ruby when a message is sent to the resource that it cannot handle. The default behavior is to convert symbol to a string and search for that string in the resource’s meta-data. If found, the meta-data item is returned; otherwise, nil is returned.



243
244
245
# File 'lib/webby/resource.rb', line 243

def method_missing( name, *a, &b )
  @mdata[name.to_s]
end

Instance Attribute Details

#dirObject (readonly)

The directory of the resource excluding the content directory



46
47
48
# File 'lib/webby/resource.rb', line 46

def dir
  @dir
end

#extObject (readonly)

Extesion of the resource file



52
53
54
# File 'lib/webby/resource.rb', line 52

def ext
  @ext
end

#filenameObject (readonly)

The resource filename excluding path and extension



49
50
51
# File 'lib/webby/resource.rb', line 49

def filename
  @filename
end

#mtimeObject (readonly)

Resource file modification time



55
56
57
# File 'lib/webby/resource.rb', line 55

def mtime
  @mtime
end

#pathObject (readonly)

The full path to the resource file



43
44
45
# File 'lib/webby/resource.rb', line 43

def path
  @path
end

Class Method Details

.clearObject

Clear the contents of the layouts and the pages hash objects.



36
37
38
39
# File 'lib/webby/resource.rb', line 36

def clear
  self.pages.clear
  self.layouts.clear
end

.layoutsObject

Returns the layouts hash object.



31
32
33
# File 'lib/webby/resource.rb', line 31

def layouts
  @layouts ||= PagesDB.new
end

.pagesObject

Returns the pages hash object.



26
27
28
# File 'lib/webby/resource.rb', line 26

def pages
  @pages ||= PagesDB.new
end

Instance Method Details

#<=>(other) ⇒ Object

call-seq:

resource <=> other    => -1, 0, +1, or nil

Resource comparison operates on the full path of the resource objects and uses the standard String comparison operator. Returns nil if other is not a Resource instance.



103
104
105
106
# File 'lib/webby/resource.rb', line 103

def <=>( other )
  return unless self.class == other.class
  @path <=> other.path
end

#destinationObject

call-seq:

destination    => string

Returns the path in the output directory where the results of rendering this resource should be stored. This path is used to determine if the resource is dirty and in need of rendering.

The destination for any resource can be overridden by explicitly setting the ‘destination’ propery in the resource’s meta-data.



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

def destination
  return @dest if defined? @dest
  return @dest = ::Webby.config['output_dir'] if is_layout?

  @dest = if @mdata.has_key? 'destination' then @mdata['destination']
          else File.join(dir, filename) end

  @dest = File.join(::Webby.config['output_dir'], @dest)
  @dest << '.'
  @dest << extension
  @dest
end

#dirty?Boolean

call-seq:

dirty?    => true or false

Returns true if this resource is newer than its corresponding output product. The resource needs to be rendered (if a page or layout) or copied (if a static file) to the output directory.

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/webby/resource.rb', line 212

def dirty?
  return @mdata['dirty'] if @mdata.has_key? 'dirty'

  # if the destination file does not exist, then we are dirty
  return @mdata['dirty'] = true unless test ?e, destination

  # if this file's mtime is larger than the destination file's
  # mtime, then we are dirty
  @mdata['dirty'] = @mtime > File.mtime(destination)
  return @mdata['dirty'] if is_static? or @mdata['dirty']

  # check to see if the layout is dirty, and it it is then we
  # are dirty, too
  if @mdata.has_key? 'layout'
    lyt = self.class.layouts.find_by_name @mdata['layout']
    break if lyt.nil?
    return @mdata['dirty'] = true if lyt.dirty?
  end

  # if we got here, then we are not dirty
  @mdata['dirty'] = false
end

#equal?(other) ⇒ Boolean Also known as: ==, eql?

call-seq:

equal?( other )    => true or false

Returns true if the path of this resource is equivalent to the path of the other resource. Returns false if this is not the case.

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/webby/resource.rb', line 89

def equal?( other )
  return false unless self.class == other.class
  @path == other.path
end

#extensionObject

call-seq:

extension    => string

Returns the extension that will be appended to the output destination filename. The extension is determined by looking at the following:

  • this resource’s meta-data for an ‘extension’ property

  • the meta-data of this resource’s layout for an ‘extension’ propery

  • the extension of this resource file



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/webby/resource.rb', line 118

def extension
  return @mdata['extension'] if @mdata.has_key? 'extension'

  if @mdata.has_key? 'layout'
    lyt = self.class.layouts.find_by_name @mdata['layout']
    break if lyt.nil?
    return lyt.extension
  end

  @ext
end

#is_layout?Boolean

call-seq:

is_layout?    => true or false

Returns true if this resource is a layout.

Returns:

  • (Boolean)


182
183
184
185
# File 'lib/webby/resource.rb', line 182

def is_layout?
  @is_layout ||=
      !(%r/\A(?:\.\/|\/)?#{::Webby.config['layout_dir']}\//o =~ @path).nil?
end

#is_page?Boolean

call-seq:

is_page?    => true or false

Returns true if this resource is a page suitable for rendering.

Returns:

  • (Boolean)


201
202
203
# File 'lib/webby/resource.rb', line 201

def is_page?
  @have_mdata and !is_layout?
end

#is_static?Boolean

call-seq:

is_static?    => true or false

Returns true if this resource is a static file.

Returns:

  • (Boolean)


192
193
194
# File 'lib/webby/resource.rb', line 192

def is_static?
  !@have_mdata
end

#renderObject

call-seq:

render   => string

Creates a new Webby::Renderer instance and uses that instance to render the resource contents using the configured filter(s). The filter(s) to use is defined in the resource’s meta-data as the ‘filter’ key.

Note, this only renders this resource. The returned string does not include any layout rendering.



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/webby/resource.rb', line 163

def render
  raise Error, "page '#@path' is in a rendering loop" if @rendering

  @rendering = true
  content = Renderer.new(self).render_page
  @rendering = false

  return content

rescue
  @rendering = false
  raise
end