Class: Webby::File

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

Overview

The Webby::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:

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

Opens the file named by filename according to mode (default is ‘r’) and returns a new Webby::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)


90
91
92
93
# File 'lib/webby/file.rb', line 90

def initialize( *args )
  super
  @meta_end = 
end

Class Method Details

.meta_data(name) ⇒ Object

call-seq:

Webby::File.( name )    => object or nil

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



66
67
68
69
70
71
# File 'lib/webby/file.rb', line 66

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

.read(name, *args) ⇒ Object

call-seq:

Webby::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.



39
40
41
42
43
44
# File 'lib/webby/file.rb', line 39

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

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

call-seq:

Webby::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.



53
54
55
56
57
58
# File 'lib/webby/file.rb', line 53

def readlines( name, sep = $/ )
  fd = new name, 'r'
  fd.readlines sep
ensure
  fd.close
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.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/webby/file.rb', line 104

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webby/file.rb', line 126

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