Class: Extruder::FileWrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentsString

Returns contents of the file represented by the Extruder::FileWrapper.

Returns:



27
28
29
# File 'lib/extruder/file_wrapper.rb', line 27

def contents
  @contents
end

#encodingString

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.

Returns:

  • (String)

    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

#pathString

Returns the path to the file represented by the Extruder::FileWrapper relative to its #root.

Returns:



24
25
26
# File 'lib/extruder/file_wrapper.rb', line 24

def path
  @path
end

#rootString

Returns an absolute path representing this Extruder::FileWrapper‘s root directory.

Returns:



20
21
22
# File 'lib/extruder/file_wrapper.rb', line 20

def root
  @root
end

#saveBoolean

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.

Returns:

  • (Boolean)

    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

Parameters:

Returns:

  • (Fixnum)

    -1, 0, or 1



114
115
116
# File 'lib/extruder/file_wrapper.rb', line 114

def <=>(other)
  [root, path, encoding] <=> [other.root, other.path, other.encoding]
end

#createFile

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.

Returns:

  • (File)


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

Parameters:

Returns:

  • (true, false)


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?

Returns:

  • (true, false)


124
125
126
# File 'lib/extruder/file_wrapper.rb', line 124

def exists?
  File.exists?(fullpath)
end

#fullpathString

The full path of a FileWrapper is its root joined with its path

Returns:



102
103
104
105
# File 'lib/extruder/file_wrapper.rb', line 102

def fullpath
  raise "#{root}, #{path}" unless root =~ /^\//
  File.join(root, path)
end

#hashFixnum

Similar to #eql?, generate a Extruder::FileWrapper‘s #hash from its #root and #path

Returns:

  • (Fixnum)

    a hash code

See Also:



92
93
94
# File 'lib/extruder/file_wrapper.rb', line 92

def hash
  [root, path].hash
end

#inspectString Also known as: to_s

Returns A pretty representation of the Extruder::FileWrapper.

Returns:



40
41
42
# File 'lib/extruder/file_wrapper.rb', line 40

def inspect
  "#<FileWrapper root=#{root.inspect} path=#{path.inspect} encoding=#{encoding.inspect}>"
end

#readString

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.

Returns:

  • (String)

    the contents of the file

Raises:

  • (EncodingError)

    when the contents of the file are not valid in the expected encoding specified in #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.

Parameters:

  • encoding (String)

    the encoding for the new object

Returns:



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