Class: Webby::Resources::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/resources/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.

Direct Known Subclasses

Layout, Page, Partial, Static

Instance Attribute 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.



36
37
38
39
40
41
42
43
44
# File 'lib/webby/resources/resource.rb', line 36

def initialize( fn )
  @path     = fn
  @dir      = ::Webby::Resources::File.dirname(@path)
  @filename = ::Webby::Resources::File.basename(@path)
  @ext      = ::Webby::Resources::File.extname(@path)
  @mtime    = ::File.mtime @path

  @mdata = @@mdata ||= {}
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.



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

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



20
21
22
# File 'lib/webby/resources/resource.rb', line 20

def dir
  @dir
end

#extObject (readonly)

Extesion of the resource file



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

def ext
  @ext
end

#filenameObject (readonly)

The resource filename excluding path and extension



23
24
25
# File 'lib/webby/resources/resource.rb', line 23

def filename
  @filename
end

#mtimeObject (readonly)

Resource file modification time



29
30
31
# File 'lib/webby/resources/resource.rb', line 29

def mtime
  @mtime
end

#pathObject (readonly)

The full path to the resource file



17
18
19
# File 'lib/webby/resources/resource.rb', line 17

def path
  @path
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.



66
67
68
69
# File 'lib/webby/resources/resource.rb', line 66

def <=>( other )
  return unless other.kind_of? ::Webby::Resources::Resource
  @path <=> other.path
end

#[](key) ⇒ Object

call-seq:

resource[key]    => value or nil

Returns the value associated with the given meta-data key. Key is usually a string.



77
78
79
# File 'lib/webby/resources/resource.rb', line 77

def []( key )
  @mdata[key]
end

#destinationObject

:stopdoc:

Raises:

  • (NotImplementedError)


136
137
138
# File 'lib/webby/resources/resource.rb', line 136

def destination
  raise NotImplementedError
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)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/webby/resources/resource.rb', line 100

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

  # if the destination file does not exist, then we are dirty
  return true unless test(?e, destination)

  # if this file's mtime is larger than the destination file's
  # mtime, then we are dirty
  dirty = @mtime > ::File.mtime(destination)
  return dirty if dirty

  # check to see if the layout is dirty, and if it is then we
  # are dirty, too
  if @mdata.has_key? 'layout'
    lyt = ::Webby::Resources.layouts.find :filename => @mdata['layout']
    unless lyt.nil?
      return true if lyt.dirty?
    end
  end

  # if we got here, then we are not 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)


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

def equal?( other )
  return false unless other.kind_of? ::Webby::Resources::Resource
  @path == other.path
end

#extensionObject

Raises:

  • (NotImplementedError)


140
141
142
# File 'lib/webby/resources/resource.rb', line 140

def extension
  raise NotImplementedError
end

#urlObject

call-seq

url    => string or nil

Returns a string suitable for use as a URL linking to this page. Nil is returned for layouts.



130
131
132
133
# File 'lib/webby/resources/resource.rb', line 130

def url
  return @url if defined? @url and @url
  @url = destination.sub(::Webby.site.output_dir, '')
end