Class: Carbuncle::File
Class Method Summary collapse
- .join(*names) ⇒ Object
- .read(path, *opts) ⇒ Object
- .readlines(name, *args) ⇒ Object
- .require(file) ⇒ Object
- .required_files ⇒ Object
- .write(path, content, open_args = 'w') ⇒ Object
Instance Method Summary collapse
Class Method Details
.join(*names) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 17 def join(*names) return '' if names.empty? names.filter!(&:present?) names.map! do |name| case name when String name when Array raise ArgumentError, 'recursive array' if names == name join(*name) else raise TypeError, "no implicit conversion of #{name.class} into String" end end return names[0] if names.size == 1 s = if names[0][-1] == Carbuncle::File::SEPARATOR names[0][0..-2] else names[0].dup end (1..names.size - 2).each do |i| t = names[i] if t[0] == Carbuncle::File::SEPARATOR && t[-1] == Carbuncle::File::SEPARATOR t = t[1..-2] elsif t[0] == Carbuncle::File::SEPARATOR t = t[1..-1] elsif t[-1] == Carbuncle::File::SEPARATOR t = t[0..-2] end s += Carbuncle::File::SEPARATOR + t if t != '' end s += if names[-1][0] == Carbuncle::File::SEPARATOR Carbuncle::File::SEPARATOR + names[-1][1..-1] else Carbuncle::File::SEPARATOR + names[-1] end s end |
.read(path, *opts) ⇒ Object
95 96 97 98 99 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 95 def read(path, *opts) result = nil self.open(path, 'r') { |f| result = f.read(*opts) } result end |
.readlines(name, *args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 62 def readlines(name, *args) open_args = [name] read_args = [] case args.size when 3 open_args = [name, args[2]] read_args = [args[0], args[1]] when 2 if args[0].is_a?(String) open_args = [name, args[1]] read_args = [args[0]] else read_args = args end when 1 read_args = args when 0 # Default values else raise ArgumentError, 'Invalid number of arguments' end fp = self.open(*open_args) lines = fp.readlines(*read_args) fp.close lines end |
.require(file) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 4 def require(file) @required_files ||= {} name = file.to_s return false if @required_files[name] self.load(file) @required_files[name] = true end |
.required_files ⇒ Object
13 14 15 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 13 def required_files @required_files.keys end |
.write(path, content, open_args = 'w') ⇒ Object
89 90 91 92 93 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 89 def write(path, content, open_args = 'w') result = nil self.open(path, open_args) { |f| result = f.write(content) } result end |
Instance Method Details
#readlines(arg1 = nil, arg2 = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'gems/carbuncle-core/mrblib/file.rb', line 102 def readlines(arg1 = nil, arg2 = nil) limit, sep = -1, "\n" if arg2 limit = arg2 sep = arg1 || sep elsif arg1 if arg1.is_a?(Numeric) limit = arg1 else sep = arg1 end end [].tap do |lines| line = readline(sep) while line && (limit == -1 || limit < lines.length) lines.push(line) line = readline(sep) end end end |