Class: VirtFS::FileModesAndOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/virtfs/file_modes_and_options.rb

Overview

Utilty class to convert String File Modes and Options to their corresponding Ruby Constants.

*args –> mode=“r” <,permission> <,options>

mode       --> String or Integer
permission --> Integer
options    --> Hash

1 arg: <mode | options> 2 args: mode, <permissions | options> 3 args: mode, permissions, options

mode string –> file-mode[:external-encoding]

file-mode mapped to binary:

"r"  --> File::RDONLY
"r+" --> File::RDWR
"w"  --> File::WRONLY | File::TRUNC  | File::CREAT
"w+" --> File::RDWR   | File::TRUNC  | File::CREAT
"a"  --> File::WRONLY | File::APPEND | File::CREAT
"a+" --> File::RDWR   | File::APPEND | File::CREAT

Options:

:autoclose          => If false, the underlying file will not be closed
                       when this I/O object is finalized.

:binmode            => Opens the IO object in binary mode if true (same as mode: "b").

:encoding           => Specifies both external and internal encodings
                       as "external:internal" (same format used in mode parameter).

:external_encoding  => Specifies the external encoding.

:internal_encoding  => Specifies the internal encoding.

:mode               => Specifies what would have been the mode parameter.

:textmode           => Open the file in text mode (the default).

Constant Summary collapse

BINARY_ENCODING =
"ASCII-8BIT"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FileModesAndOptions

rubocop:disable AbcSize, PerceivedComplexity



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/virtfs/file_modes_and_options.rb', line 50

def initialize(*args) # rubocop:disable AbcSize, PerceivedComplexity
  @args      = args
  @options   = {}
  @mode_bits = 0
  @external_encoding_str = ""
  @internal_encoding_str = ""
  @binmode     = false
  @autoclose   = true
  @permissions = nil
  @closed      = false

  process_args(args)

  @mode_bits = VFile::RDONLY if @mode_bits == 0

  if @external_encoding_str.empty? || @external_encoding_str.empty? == "-"
    @external_encoding = Encoding.default_external
  else
    @external_encoding = Encoding.find(@external_encoding_str)
  end

  if @internal_encoding_str.empty? || @internal_encoding_str == "-"
    @internal_encoding = Encoding.default_internal
  else
    @internal_encoding = Encoding.find(@internal_encoding_str)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



46
47
48
# File 'lib/virtfs/file_modes_and_options.rb', line 46

def args
  @args
end

#external_encodingObject (readonly)

rubocop:disable ClassLength



43
44
45
# File 'lib/virtfs/file_modes_and_options.rb', line 43

def external_encoding
  @external_encoding
end

#external_encoding_strObject (readonly)

rubocop:disable ClassLength



43
44
45
# File 'lib/virtfs/file_modes_and_options.rb', line 43

def external_encoding_str
  @external_encoding_str
end

#internal_encodingObject (readonly)

Returns the value of attribute internal_encoding.



44
45
46
# File 'lib/virtfs/file_modes_and_options.rb', line 44

def internal_encoding
  @internal_encoding
end

#internal_encoding_strObject (readonly)

Returns the value of attribute internal_encoding_str.



44
45
46
# File 'lib/virtfs/file_modes_and_options.rb', line 44

def internal_encoding_str
  @internal_encoding_str
end

#mode_bitsObject (readonly)

Returns the value of attribute mode_bits.



45
46
47
# File 'lib/virtfs/file_modes_and_options.rb', line 45

def mode_bits
  @mode_bits
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/virtfs/file_modes_and_options.rb', line 45

def options
  @options
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



45
46
47
# File 'lib/virtfs/file_modes_and_options.rb', line 45

def permissions
  @permissions
end

Instance Method Details

#[](key) ⇒ Object



78
79
80
# File 'lib/virtfs/file_modes_and_options.rb', line 78

def [](key)
  @options[key]
end

#append?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/virtfs/file_modes_and_options.rb', line 82

def append?
  @mode_bits & VFile::APPEND != 0
end

#autoclose?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/virtfs/file_modes_and_options.rb', line 86

def autoclose?
  @autoclose
end

#binmodeObject



94
95
96
97
# File 'lib/virtfs/file_modes_and_options.rb', line 94

def binmode
  set_encoding(BINARY_ENCODING, nil)
  @binmode = true
end

#binmode?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/virtfs/file_modes_and_options.rb', line 90

def binmode?
  @binmode
end

#closeObject



153
154
155
# File 'lib/virtfs/file_modes_and_options.rb', line 153

def close
  @closed = true
end

#close_readObject



143
144
145
146
# File 'lib/virtfs/file_modes_and_options.rb', line 143

def close_read
  return unless rdonly?
  close
end

#close_writeObject



148
149
150
151
# File 'lib/virtfs/file_modes_and_options.rb', line 148

def close_write
  return unless wronly?
  close
end

#closed?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/virtfs/file_modes_and_options.rb', line 99

def closed?
  @closed
end

#create?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/virtfs/file_modes_and_options.rb', line 103

def create?
  @mode_bits & VFile::CREAT != 0
end

#excl?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/virtfs/file_modes_and_options.rb', line 107

def excl?
  @mode_bits & VFile::EXCL != 0
end

#noctty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/virtfs/file_modes_and_options.rb', line 111

def noctty?
  @mode_bits & VFile::NOCTTY != 0
end

#nonblock?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/virtfs/file_modes_and_options.rb', line 115

def nonblock?
  @mode_bits & VFile::NONBLOCK != 0
end

#rdonly?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/virtfs/file_modes_and_options.rb', line 119

def rdonly?
  @mode_bits == VFile::RDONLY # VFile::RDONLY = 0
end

#rdwr?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/virtfs/file_modes_and_options.rb', line 123

def rdwr?
  @mode_bits & VFile::RDWR != 0
end

#read?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/virtfs/file_modes_and_options.rb', line 135

def read?
  rdonly? || rdwr?
end

#set_encoding(*args) ⇒ Object

rubocop:disable AccessorMethodName, AbcSize, PerceivedComplexity, CyclomaticComplexity, MethodLength, LineLength

Raises:

  • (ArgumentError)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/virtfs/file_modes_and_options.rb', line 157

def set_encoding(*args) # rubocop:disable AccessorMethodName, AbcSize, PerceivedComplexity, CyclomaticComplexity, MethodLength, LineLength
  raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)" if args.length < 1 || args.length > 2
  unless args[0].is_a?(Encoding) || args[0].respond_to?(:to_str)
    raise TypeError, "no implicit conversion of #{args[0].class.name} into String"
  end

  if args.length == 2
    unless args[1].is_a?(Encoding) || args[1].respond_to?(:to_str) || args[1].nil?
      raise TypeError, "no implicit conversion of #{args[1].class.name} into String"
    end
    @external_encoding     = args[0].is_a?(Encoding) ? args[0] : Encoding.find(args[0].to_str)
    @internal_encoding     = args[1].nil? || args[1].is_a?(Encoding) ? args[1] : Encoding.find(args[1].to_str)
    return nil
  end

  if args[0].is_a?(Encoding)
    @external_encoding = args[0]
    return nil
  end

  if args[0].to_str.include?(":")
    @external_encoding_str, @internal_encoding_str = args[0].split(":")
    @external_encoding_str = @external_encoding_str.to_str
    @internal_encoding_str = @internal_encoding_str.to_str
  else
    @external_encoding_str = @internal_encoding_str = args[0].to_str
  end

  @external_encoding = Encoding.find(@external_encoding_str) unless @external_encoding_str.empty?
  @internal_encoding = Encoding.find(@internal_encoding_str) unless @internal_encoding_str.empty?
  nil
end

#trunc?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/virtfs/file_modes_and_options.rb', line 127

def trunc?
  @mode_bits & VFile::TRUNC != 0
end

#write?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/virtfs/file_modes_and_options.rb', line 139

def write?
  wronly? || rdwr?
end

#wronly?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/virtfs/file_modes_and_options.rb', line 131

def wronly?
  @mode_bits & VFile::WRONLY != 0
end