Class: Rake::Pipeline::FileWrapper
- Inherits:
-
Object
- Object
- Rake::Pipeline::FileWrapper
- Defined in:
- lib/rake-pipeline/file_wrapper.rb
Overview
This class wraps a file for consumption inside of filters. It is initialized with a root and path, and filters usually use the #read and #write methods to work with these files.
The #root and path
parameters are provided by the Filter class’ internal implementation. Individual filters do not need to worry about them.
The root of a FileWrapper is always an absolute path.
Instance Attribute Summary collapse
-
#encoding ⇒ String
The encoding that the file represented by this FileWrapper is encoded in.
-
#path ⇒ String
The path to the file represented by the FileWrapper, relative to its #root.
-
#root ⇒ String
An absolute path representing this FileWrapper‘s root directory.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Make FileWrappers sortable.
-
#close ⇒ void
private
Close the file represented by the FileWrapper if it was previously opened.
-
#closed? ⇒ true, false
private
Check to see whether the file represented by the FileWrapper is open.
-
#create {|file| ... } ⇒ File
private
Create a new file at the FileWrapper‘s #fullpath.
-
#eql?(other) ⇒ true, false
(also: #==)
A FileWrapper is equal to another FileWrapper for hashing purposes if they have the same #root and #path.
-
#exists? ⇒ true, false
Does the file represented by the FileWrapper exist in the file system?.
-
#fullpath ⇒ String
The full path of a FileWrapper is its root joined with its path.
-
#hash ⇒ Fixnum
Similar to #eql?, generate a FileWrapper‘s #hash from its #root and #path.
-
#initialize(root = nil, path = nil, encoding = "UTF-8") ⇒ void
constructor
Create a new FileWrapper, passing in optional root, path, and encoding.
-
#inspect ⇒ String
(also: #to_s)
A pretty representation of the FileWrapper.
-
#read ⇒ String
Read the contents of the file represented by the FileWrapper.
-
#with_encoding(encoding) ⇒ FileWrapper
Create a new FileWrapper with the same root and path as this FileWrapper, but with a specified encoding.
-
#write(string) ⇒ Object
Write a String to a previously opened file.
Constructor Details
#initialize(root = nil, path = nil, encoding = "UTF-8") ⇒ void
Create a new Rake::Pipeline::FileWrapper, passing in optional root, path, and encoding. Any of the parameters can be ommitted and supplied later.
30 31 32 33 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 30 def initialize(root=nil, path=nil, encoding="UTF-8") @root, @path, @encoding = root, path, encoding @created_file = nil end |
Instance Attribute Details
#encoding ⇒ String
Returns the encoding that the file represented by this Rake::Pipeline::FileWrapper is encoded in. Filters set the #encoding to BINARY
if they are declared as processing binary data.
24 25 26 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 24 def encoding @encoding end |
#path ⇒ String
Returns the path to the file represented by the Rake::Pipeline::FileWrapper, relative to its #root.
19 20 21 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 19 def path @path end |
#root ⇒ String
Returns an absolute path representing this Rake::Pipeline::FileWrapper‘s root directory.
15 16 17 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 15 def root @root end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Make FileWrappers sortable
77 78 79 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 77 def <=>(other) [root, path, encoding] <=> [other.root, other.path, other.encoding] end |
#close ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Close the file represented by the Rake::Pipeline::FileWrapper if it was previously opened.
141 142 143 144 145 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 141 def close raise IOError, "closed stream" unless @created_file @created_file.close @created_file = nil end |
#closed? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check to see whether the file represented by the Rake::Pipeline::FileWrapper is open.
151 152 153 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 151 def closed? @created_file.nil? end |
#create {|file| ... } ⇒ File
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new file at the Rake::Pipeline::FileWrapper‘s #fullpath. If the file already exists, it will be overwritten.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 116 def create FileUtils.mkdir_p(File.dirname(fullpath)) @created_file = if "".respond_to?(:encode) File.open(fullpath, "w:#{encoding}") else File.open(fullpath, "w") end if block_given? yield @created_file end @created_file ensure if block_given? @created_file.close @created_file = nil end end |
#eql?(other) ⇒ true, false Also known as: ==
A Rake::Pipeline::FileWrapper is equal to another Rake::Pipeline::FileWrapper for hashing purposes if they have the same #root and #path
50 51 52 53 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 50 def eql?(other) return false unless other.is_a?(self.class) root == other.root && path == other.path end |
#exists? ⇒ true, false
Does the file represented by the Rake::Pipeline::FileWrapper exist in the file system?
84 85 86 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 84 def exists? File.exists?(fullpath) end |
#fullpath ⇒ String
The full path of a FileWrapper is its root joined with its path
68 69 70 71 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 68 def fullpath raise "#{root}, #{path}" unless root =~ /^(\/|[a-zA-Z]:[\\\/])/ File.join(root, path) end |
#hash ⇒ Fixnum
Similar to #eql?, generate a Rake::Pipeline::FileWrapper‘s #hash from its #root and #path
61 62 63 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 61 def hash [root, path].hash end |
#inspect ⇒ String Also known as: to_s
Returns A pretty representation of the Rake::Pipeline::FileWrapper.
166 167 168 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 166 def inspect "#<FileWrapper root=#{root.inspect} path=#{path.inspect} encoding=#{encoding.inspect}>" end |
#read ⇒ String
Read the contents of the file represented by the Rake::Pipeline::FileWrapper.
Read the file using the Rake::Pipeline::FileWrapper‘s encoding, which will result in this method returning a String
tagged with the Rake::Pipeline::FileWrapper’s encoding.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 96 def read contents = if "".respond_to?(:encode) File.read(fullpath, :encoding => encoding) else File.read(fullpath) end if "".respond_to?(:encode) && !contents.valid_encoding? raise EncodingError, "The file at the path #{fullpath} is not valid UTF-8. Please save it again as UTF-8." end contents end |
#with_encoding(encoding) ⇒ FileWrapper
Create a new FileWrapper with the same root and path as this FileWrapper, but with a specified encoding.
41 42 43 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 41 def with_encoding(encoding) self.class.new(@root, @path, encoding) end |
#write(string) ⇒ Object
Write a String to a previously opened file. This method is called repeatedly by a Rake::Pipeline::Filter‘s #generate_output
method and does not create a brand new file for each invocation.
160 161 162 163 |
# File 'lib/rake-pipeline/file_wrapper.rb', line 160 def write(string) raise UnopenedFile unless @created_file @created_file.write(string) end |