Class: Rake::Pipeline::FileWrapper

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

Instance Method Summary collapse

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

#encodingString

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.

Returns:

  • (String)

    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

#pathString

Returns the path to the file represented by the Rake::Pipeline::FileWrapper, relative to its #root.

Returns:



19
20
21
# File 'lib/rake-pipeline/file_wrapper.rb', line 19

def path
  @path
end

#rootString

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

Returns:



15
16
17
# File 'lib/rake-pipeline/file_wrapper.rb', line 15

def root
  @root
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Make FileWrappers sortable

Parameters:

Returns:

  • (Fixnum)

    -1, 0, or 1



77
78
79
# File 'lib/rake-pipeline/file_wrapper.rb', line 77

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

#closevoid

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.

Raises:

  • (IOError)


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.

Returns:

  • (true, false)


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.

Yield Parameters:

  • file (File)

    the newly created file

Returns:

  • (File)

    if a block was not given



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

Parameters:

Returns:

  • (true, false)


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?

Returns:

  • (true, false)


84
85
86
# File 'lib/rake-pipeline/file_wrapper.rb', line 84

def exists?
  File.exists?(fullpath)
end

#fullpathString

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

Returns:



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

#hashFixnum

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

Returns:

  • (Fixnum)

    a hash code

See Also:



61
62
63
# File 'lib/rake-pipeline/file_wrapper.rb', line 61

def hash
  [root, path].hash
end

#inspectString Also known as: to_s

Returns A pretty representation of the Rake::Pipeline::FileWrapper.

Returns:



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

#readString

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.

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.



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.

Parameters:

  • encoding (String)

    the encoding for the new object

Returns:



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.

Raises:



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