Module: Mumukit::WithTempfile
- Included in:
- Templates::FileHook
- Defined in:
- lib/mumukit/with_tempfile.rb
Instance Method Summary collapse
- #create_tempfile ⇒ Object
- #mask_tempfile_references(string, masked_tempfile_path) ⇒ Object
- #tempfile_extension ⇒ Object
- #with_tempfile {|file| ... } ⇒ Object
- #write_tempdir!(files) ⇒ Object
- #write_tempfile!(content) ⇒ Object
Instance Method Details
#create_tempfile ⇒ Object
6 7 8 |
# File 'lib/mumukit/with_tempfile.rb', line 6 def create_tempfile Tempfile.new(['mumuki.compile', tempfile_extension]) end |
#mask_tempfile_references(string, masked_tempfile_path) ⇒ Object
34 35 36 |
# File 'lib/mumukit/with_tempfile.rb', line 34 def mask_tempfile_references(string, masked_tempfile_path) string.gsub(/\/tmp\/mumuki\.compile(.+?)#{tempfile_extension}/, masked_tempfile_path) end |
#tempfile_extension ⇒ Object
10 11 12 |
# File 'lib/mumukit/with_tempfile.rb', line 10 def tempfile_extension '' end |
#with_tempfile {|file| ... } ⇒ Object
27 28 29 30 31 32 |
# File 'lib/mumukit/with_tempfile.rb', line 27 def with_tempfile file = create_tempfile yield file file.close file end |
#write_tempdir!(files) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/mumukit/with_tempfile.rb', line 20 def write_tempdir!(files) dir = Dir.mktmpdir files.map do |filename, content| File.open("#{dir}/#{filename.sanitize_as_filename}", 'w') { |file| file.write content; file } end.try { |it| struct dir: dir, files: it } end |
#write_tempfile!(content) ⇒ Object
14 15 16 17 18 |
# File 'lib/mumukit/with_tempfile.rb', line 14 def write_tempfile!(content) with_tempfile do |file| file.write(content) end end |