Class: Ole::IOMode

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/ole/support.rb

Defined Under Namespace

Modules: Constants

Constant Summary collapse

NAMES =
%w[rdonly wronly rdwr creat trunc append binary]

Constants included from Constants

Constants::BINARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags) ⇒ IOMode

Returns a new instance of IOMode.

Raises:

  • (ArgumentError)


199
200
201
202
203
# File 'lib/ole/support.rb', line 199

def initialize flags
  flags = self.class.parse_mode flags.to_str if flags.respond_to? :to_str
  raise ArgumentError, "invalid flags - #{flags.inspect}" unless Integer === flags
  @flags = flags
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



198
199
200
# File 'lib/ole/support.rb', line 198

def flags
  @flags
end

Class Method Details

.parse_mode(mode) ⇒ Object

nabbed from rubinius, and modified



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ole/support.rb', line 177

def self.parse_mode mode
  ret = 0

  case mode[0, 1]
  when 'r'; ret |= RDONLY
  when 'w'; ret |= WRONLY | CREAT | TRUNC
  when 'a'; ret |= WRONLY | CREAT | APPEND
  else raise ArgumentError, "illegal access mode #{mode}"
  end

  (1...mode.length).each do |i|
    case mode[i, 1]
    when '+'; ret = (ret & ~(RDONLY | WRONLY)) | RDWR
    when 'b'; ret |= BINARY
    else raise ArgumentError, "illegal access mode #{mode}"
    end
  end

  ret
end

Instance Method Details

#append?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/ole/support.rb', line 218

def append?
  (@flags & APPEND) != 0
end

#binary?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/ole/support.rb', line 226

def binary?
  (@flags & BINARY) != 0
end

#create?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/ole/support.rb', line 222

def create?
  (@flags & CREAT) != 0
end

#inspectObject

# revisit this def apply io if truncate? io.truncate 0 elsif append? io.seek IO::SEEK_END, 0 end end



241
242
243
244
245
# File 'lib/ole/support.rb', line 241

def inspect
  names = NAMES.map { |name| name if (flags & IOMode.const_get(name.upcase)) != 0 }
  names.unshift 'rdonly' if (flags & 0x3) == 0
  "#<#{self.class} #{names.compact * '|'}>"
end

#readable?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/ole/support.rb', line 210

def readable?
  (@flags & WRONLY) == 0
end

#truncate?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/ole/support.rb', line 214

def truncate?
  (@flags & TRUNC) != 0
end

#writeable?Boolean

Returns:

  • (Boolean)


205
206
207
208
# File 'lib/ole/support.rb', line 205

def writeable?
  #(@flags & RDONLY) == 0
  (@flags & 0x3) != RDONLY
end