Class: Extruder::FileWrapper
- Inherits:
-
Object
- Object
- Extruder::FileWrapper
- Defined in:
- lib/extruder/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 Extruder 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
-
#contents ⇒ String
Contents of the file represented by the FileWrapper.
-
#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.
-
#save ⇒ Boolean
True if created by a filter, otherwise false this flag determines if a file will be written when its container FileSet is saved.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Make FileWrappers sortable.
-
#create ⇒ File
Create a new file at the FileWrapper‘s #fullpath and fill it with the value of @contents.
-
#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
Append a string to the cached contents of the file this wrapper represents.
Constructor Details
#initialize(root = nil, path = nil, encoding = "UTF-8") ⇒ void
Create a new Extruder::FileWrapper, passing in optional root, path, and encoding. Any of the parameters can be ommitted and supplied later.
52 53 54 55 56 |
# File 'lib/extruder/file_wrapper.rb', line 52 def initialize(root=nil, path=nil, encoding="UTF-8") @root, @path, @encoding = root, path, encoding @created_file = nil @save = false end |
Instance Attribute Details
#contents ⇒ String
Returns contents of the file represented by the Extruder::FileWrapper.
27 28 29 |
# File 'lib/extruder/file_wrapper.rb', line 27 def contents @contents end |
#encoding ⇒ String
Returns the encoding that the file represented by this Extruder::FileWrapper is encoded in. Filters set the #encoding to BINARY
if they are declared as processing binary data.
32 33 34 |
# File 'lib/extruder/file_wrapper.rb', line 32 def encoding @encoding end |
#path ⇒ String
Returns the path to the file represented by the Extruder::FileWrapper relative to its #root.
24 25 26 |
# File 'lib/extruder/file_wrapper.rb', line 24 def path @path end |
#root ⇒ String
Returns an absolute path representing this Extruder::FileWrapper‘s root directory.
20 21 22 |
# File 'lib/extruder/file_wrapper.rb', line 20 def root @root end |
#save ⇒ Boolean
Returns true if created by a filter, otherwise false this flag determines if a file will be written when its container Extruder::FileSet is saved.
37 38 39 |
# File 'lib/extruder/file_wrapper.rb', line 37 def save @save end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Make FileWrappers sortable
114 115 116 |
# File 'lib/extruder/file_wrapper.rb', line 114 def <=>(other) [root, path, encoding] <=> [other.root, other.path, other.encoding] end |
#create ⇒ File
Create a new file at the Extruder::FileWrapper‘s #fullpath and fill it with the value of @contents. If the file already exists, it will be overwritten.
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/extruder/file_wrapper.rb', line 175 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 @created_file.write(@contents) @created_file.close @created_file end |
#eql?(other) ⇒ true, false Also known as: ==
A Extruder::FileWrapper is equal to another Extruder::FileWrapper for hashing purposes if they have the same #root and #path
78 79 80 81 |
# File 'lib/extruder/file_wrapper.rb', line 78 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 Extruder::FileWrapper exist in the file system?
124 125 126 |
# File 'lib/extruder/file_wrapper.rb', line 124 def exists? File.exists?(fullpath) end |
#fullpath ⇒ String
The full path of a FileWrapper is its root joined with its path
102 103 104 105 |
# File 'lib/extruder/file_wrapper.rb', line 102 def fullpath raise "#{root}, #{path}" unless root =~ /^\// File.join(root, path) end |
#hash ⇒ Fixnum
Similar to #eql?, generate a Extruder::FileWrapper‘s #hash from its #root and #path
92 93 94 |
# File 'lib/extruder/file_wrapper.rb', line 92 def hash [root, path].hash end |
#inspect ⇒ String Also known as: to_s
Returns A pretty representation of the Extruder::FileWrapper.
40 41 42 |
# File 'lib/extruder/file_wrapper.rb', line 40 def inspect "#<FileWrapper root=#{root.inspect} path=#{path.inspect} encoding=#{encoding.inspect}>" end |
#read ⇒ String
Read the contents of the file represented by the Extruder::FileWrapper.
Read the file using the Extruder::FileWrapper‘s encoding, which will result in this method returning a String
tagged with the Extruder::FileWrapper’s encoding.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/extruder/file_wrapper.rb', line 139 def read if @contents.nil? @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 end @contents end |
#with_encoding(encoding) ⇒ FileWrapper
Create a new Extruder::FileWrapper with the same root and path as this Extruder::FileWrapper, but with a specified encoding.
66 67 68 |
# File 'lib/extruder/file_wrapper.rb', line 66 def with_encoding(encoding) self.class.new(@root, @path, encoding) end |
#write(string) ⇒ Object
Append a string to the cached contents of the file this wrapper represents. This method is called repeatedly by a Extruder::Filter‘s #generate_output
method.
162 163 164 165 |
# File 'lib/extruder/file_wrapper.rb', line 162 def write(string) @contents = "" if contents.nil? @contents << string end |