Module: Adminix::Helpers::Files
- Defined in:
- lib/adminix/helpers/files.rb
Class Method Summary collapse
- .file_exists?(file) ⇒ Boolean
- .mkdir_p(dirname) ⇒ Object
- .read_asset(file) ⇒ Object
- .read_erb_tpl(file, binding) ⇒ Object
- .read_json_file(file) ⇒ Object
- .read_plain_file(file) ⇒ Object
- .read_yaml_file(file) ⇒ Object
- .rm(file) ⇒ Object
- .touch(file_path) ⇒ Object
- .write_json_file(file, content) ⇒ Object
- .write_plain_file(file, content) ⇒ Object
- .write_yaml_file(file, content) ⇒ Object
Class Method Details
.file_exists?(file) ⇒ Boolean
64 65 66 |
# File 'lib/adminix/helpers/files.rb', line 64 def self.file_exists?(file) File.exist?(file) end |
.mkdir_p(dirname) ⇒ Object
56 57 58 |
# File 'lib/adminix/helpers/files.rb', line 56 def self.mkdir_p(dirname) `mkdir -p #{dirname}` end |
.read_asset(file) ⇒ Object
75 76 77 78 79 |
# File 'lib/adminix/helpers/files.rb', line 75 def self.read_asset(file) path = "#{Adminix.root}/app/assets#{file}" return unless File.exist?(path) IO.read(path) end |
.read_erb_tpl(file, binding) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/adminix/helpers/files.rb', line 68 def self.read_erb_tpl(file, binding) path = "#{Adminix.root}/app/views/web/#{file}.erb" return unless File.exist?(path) template = ERB.new File.read(path), nil, '%' template.result(binding) end |
.read_json_file(file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/adminix/helpers/files.rb', line 8 def self.read_json_file(file) content = IO.read(file) JSON.parse(content) rescue JSON::ParserError Helpers::Output.display_error_and_exit( "Error reading JSON inside file #{file}" ) {} rescue Errno::ENOENT Helpers::Output.display_error_and_exit("Error reading file #{file}") {} end |
.read_plain_file(file) ⇒ Object
34 35 36 37 38 |
# File 'lib/adminix/helpers/files.rb', line 34 def self.read_plain_file(file) IO.read(file) rescue Errno::ENOENT Helpers::Output.display_error_and_exit("Error reading file #{file}") end |
.read_yaml_file(file) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/adminix/helpers/files.rb', line 21 def self.read_yaml_file(file) content = IO.read(file) obj = YAML.safe_load(content) if obj.is_a? String Helpers::Output.display_error_and_exit("Error reading file #{file}") {} end obj rescue Errno::ENOENT Helpers::Output.display_error_and_exit("Error reading file #{file}") {} end |
.rm(file) ⇒ Object
52 53 54 |
# File 'lib/adminix/helpers/files.rb', line 52 def self.rm(file) File.delete(file) if File.exist?(file) end |
.touch(file_path) ⇒ Object
60 61 62 |
# File 'lib/adminix/helpers/files.rb', line 60 def self.touch(file_path) `touch #{file_path}` end |
.write_json_file(file, content) ⇒ Object
40 41 42 |
# File 'lib/adminix/helpers/files.rb', line 40 def self.write_json_file(file, content) open(file, 'w') { |f| f.puts(content.to_json) } end |
.write_plain_file(file, content) ⇒ Object
48 49 50 |
# File 'lib/adminix/helpers/files.rb', line 48 def self.write_plain_file(file, content) open(file, 'w') { |f| f.puts(content) } end |
.write_yaml_file(file, content) ⇒ Object
44 45 46 |
# File 'lib/adminix/helpers/files.rb', line 44 def self.write_yaml_file(file, content) open(file, 'w') { |f| f.puts(content.to_yaml) } end |