Class: Webby::Resources::File

Inherits:
File
  • Object
show all
Defined in:
lib/webby/resources/file.rb

Overview

The Webby::Resources::File class is identical to the core Ruby file class except for YAML meta-data stored at the top of the file. This meta-data is made available through the meta_data and meta_data= functions.

The meta-data data must be found between two YAML block separators “—”, each on their own line.

Example:

---
layout: blog
filter: markdown
tags:
  - ruby
  - web development
---
This is a blog entry formatted using MarkDown and tagged as "ruby" and
"web development". The layout being used is the "blog" format.

Constant Summary collapse

META_SEP =

:nodoc:

%r/\A---\s*\r?\n\z/o

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ File

call-seq:

File.new( filename, mode = "r" )          => file
File.new( filename [, mode [, perm]] )    => file

Opens the file named by filename according to mode (default is ‘r’) and returns a new Webby::Resources::File object. See the description of class IO for a description of mode. The file mode may optionally be specified as a Fixnum by or-ing together the flags (O_RDONLY etc, again described under IO). Optional permission bits may be given in perm. These mode and permission bits are platform dependent; on Unix systems, see open(2) for details.

f = File.new("testfile", "r")
f = File.new("newfile",  "w+")
f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)


120
121
122
123
# File 'lib/webby/resources/file.rb', line 120

def initialize( *args )
  super
  @meta_end = 
end

Class Method Details

.basename(fn) ⇒ Object

call-seq:

File.basename( filename )    => base_name

Returns the last component of the filename, which must be formed using forward slashes (“/”regardless of the separator used on the local file system. The suffix is removed from the filename.



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

def basename( fn )
  ::File.basename(fn, '.*')
end

.dirname(fn) ⇒ Object

call-seq:

File.dirname( filename )    => dir_name

Returns all components of the filename except the last one. The filename must be formed using forward slashes (“/”) regardless of the separator used on the local file system.



78
79
80
# File 'lib/webby/resources/file.rb', line 78

def dirname( fn )
  ::File.dirname(fn).sub(%r/\A[^\/]+\/?/o, '')
end

.extname(fn) ⇒ Object

call-seq:

File.extname( filename )    => ext_name

Returns the extension (the portion of file name in path after the period). This method excludes the period from the extension name.



99
100
101
# File 'lib/webby/resources/file.rb', line 99

def extname( fn )
  ::File.extname(fn).tr('.', '')
end

.meta_data(name) ⇒ Object

call-seq:

File.( name )    => object or nil

Reads the meta-data from the file specified by name. meta_data ensures the files is closed before returning.



64
65
66
67
68
69
# File 'lib/webby/resources/file.rb', line 64

def ( name )
  fd = new name, 'r'
  fd.
ensure
  fd.close unless fd.nil?
end

.read(name, *args) ⇒ Object

call-seq:

File.read( name [, length [, offset]])    => string

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.



37
38
39
40
41
42
# File 'lib/webby/resources/file.rb', line 37

def read( name, *args )
  fd = new name, 'r'
  fd.read(*args)
ensure
  fd.close unless fd.nil?
end

.readlines(name, sep = $/) ⇒ Object

call-seq:

File.readlines( name, sep_string = $/ )    => array

Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep_string. readlines ensures the file is closed before returning.



51
52
53
54
55
56
# File 'lib/webby/resources/file.rb', line 51

def readlines( name, sep = $/ )
  fd = new name, 'r'
  fd.readlines sep
ensure
  fd.close unless fd.nil?
end

Instance Method Details

#meta_dataObject

call-seq:


Returns the meta-data defined at the top of the file. Returns nil if no meta-data is defined. The meta-data is returned as Ruby objects

Meta-data is stored in YAML format between two YAML separators “—” on their own lines.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/webby/resources/file.rb', line 134

def 
  return if @meta_end.nil?

  cur, meta_end, @meta_end = tell, @meta_end, nil
  seek 0
  return YAML.load(self)

ensure
  @meta_end = meta_end if defined? meta_end and meta_end
  seek cur if defined? cur and cur
end

#meta_data=(data) ⇒ Object

call-seq

 = object

Stores the given object as meta-data in YAML format at the top of the file. If the objectc is nil, then the meta-data section will be removed from the file.

Meta-data is stored in YAML format between two YAML separators “—” on their own lines.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/webby/resources/file.rb', line 156

def meta_data=( data )
  return if data.nil? and @meta_end.nil?

  seek 0
  lines = readlines

  truncate 0
  unless data.nil?
    write YAML.dump(data)
    write "--- #$/"
  end
  lines.each {|line| write line}
ensure
  @meta_end = 
  seek 0, IO::SEEK_END
end