Class: AbideDevUtils::Files::Writer
- Inherits:
-
Object
- Object
- AbideDevUtils::Files::Writer
show all
- Defined in:
- lib/abide_dev_utils/files.rb
Constant Summary
collapse
- MSG_EXT_APPEND =
'Appending %s extension to file'
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, **kwargs, &_block) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/abide_dev_utils/files.rb', line 14
def method_missing(m, *args, **kwargs, &_block)
if m.to_s.match?(/^write_/)
ext = m.to_s.split('_')[-1]
write(args[0], **kwargs, file_ext: ext)
else
super
end
end
|
Instance Method Details
#append_ext(file_path, ext) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/abide_dev_utils/files.rb', line 27
def append_ext(file_path, ext)
return file_path if ext.nil?
s_ext = ".#{ext}"
unless File.extname(file_path) == s_ext
puts MSG_EXT_APPEND % s_ext
file_path << s_ext
end
file_path
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
23
24
25
|
# File 'lib/abide_dev_utils/files.rb', line 23
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('write_') || super
end
|
#verify_write(file_path) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/abide_dev_utils/files.rb', line 38
def verify_write(file_path)
if File.file?(file_path)
puts "Successfully wrote to #{file_path}"
else
puts "Something went wrong! Failed writing to #{file_path}!"
end
end
|
#write(content, file: nil, add_ext: true, file_ext: nil) ⇒ Object
8
9
10
11
12
|
# File 'lib/abide_dev_utils/files.rb', line 8
def write(content, file: nil, add_ext: true, file_ext: nil)
valid_file = add_ext ? append_ext(file, file_ext) : file
File.open(valid_file, 'w') { |f| f.write(content) }
verify_write(valid_file)
end
|