Class: SafeFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_support/safe_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SafeFile

Returns a new instance of SafeFile.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ndr_support/safe_file.rb', line 4

def initialize(*args)
  a = self.class.get_fname_mode_prms(*args)
  fname = a[0]
  mode = a[1]
  prms = a[2]

  if prms
    @file = File.new(fname, mode, prms)
  else
    @file = File.new(fname, mode)
  end

  # Just in case better clone the object
  # Ruby object are passed by reference
  @file_name = fname.clone
end

Class Method Details

.basename(file_name, suffix = :none) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/ndr_support/safe_file.rb', line 93

def self.basename(file_name, suffix = :none)
  verify file_name
  if suffix == :none
    File.basename(file_name)
  else
    File.basename(file_name, suffix)
  end
end

.basename_fileObject



107
108
109
110
# File 'lib/ndr_support/safe_file.rb', line 107

def self.basename_file
  # SECURE: 02-08-2012 TPG Can't assign to __FILE__
  File.basename(__FILE__)
end

.delete(*list) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/ndr_support/safe_file.rb', line 119

def self.delete(*list)
  verify list, 'w'

  list.each do |file|
    File.delete(file) if File.exist?(file)
  end.length
end

.directory?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/ndr_support/safe_file.rb', line 69

def self.directory?(file_name)
  verify file_name
  File.directory?(file_name)
end

.dirname(path) ⇒ Object



112
113
114
115
116
117
# File 'lib/ndr_support/safe_file.rb', line 112

def self.dirname(path)
  verify path
  res = path.clone
  res.path = File.dirname(path)
  res
end

.exist?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ndr_support/safe_file.rb', line 74

def self.exist?(file_name)
  self.exists?(file_name)
end

.exists?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/ndr_support/safe_file.rb', line 78

def self.exists?(file_name)
  verify file_name
  File.exist?(file_name)
end

.extname(file_name) ⇒ Object



53
54
55
56
# File 'lib/ndr_support/safe_file.rb', line 53

def self.extname(file_name)
  verify file_name
  File.extname(file_name)
end

.file?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/ndr_support/safe_file.rb', line 83

def self.file?(file_name)
  verify file_name
  File.file?(file_name)
end

.open(*args) {|f| ... } ⇒ Object

Yields:

  • (f)


21
22
23
24
25
26
27
# File 'lib/ndr_support/safe_file.rb', line 21

def self.open(*args)
  return SafeFile.new(*args) unless block_given?

  f = SafeFile.new(*args)
  yield f
  f.close
end

.read(file_name) ⇒ Object



58
59
60
61
# File 'lib/ndr_support/safe_file.rb', line 58

def self.read(file_name)
  verify file_name, 'r'
  File.read(file_name)
end

.readlines(*args) ⇒ Object



63
64
65
66
67
# File 'lib/ndr_support/safe_file.rb', line 63

def self.readlines(*args)
  fail ArgumentError, "Incorrect number of arguments - #{args.length}" if args.length > 2 or args.length == 0
  verify args[0], 'r'
  File.readlines(*args)
end

.safepath_to_string(fname) ⇒ Object



102
103
104
105
# File 'lib/ndr_support/safe_file.rb', line 102

def self.safepath_to_string(fname)
  verify fname
  fname.to_s
end

.zero?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/ndr_support/safe_file.rb', line 88

def self.zero?(file_name)
  verify file_name
  File.zero?(file_name)
end

Instance Method Details

#closeObject



29
30
31
# File 'lib/ndr_support/safe_file.rb', line 29

def close
  @file.close
end

#each(*args, &block) ⇒ Object Also known as: each_line



43
44
45
46
# File 'lib/ndr_support/safe_file.rb', line 43

def each(*args, &block)
  verify @file_name, 'r'
  @file.each(*args, &block)
end

#pathObject



49
50
51
# File 'lib/ndr_support/safe_file.rb', line 49

def path
  @file_name.clone
end

#readObject



33
34
35
36
# File 'lib/ndr_support/safe_file.rb', line 33

def read
  verify @file_name, 'r'
  @file.read
end

#write(data) ⇒ Object



38
39
40
41
# File 'lib/ndr_support/safe_file.rb', line 38

def write(data)
  verify @file_name, 'w'
  @file.write(data)
end