Module: Cheeba::Writer
- Defined in:
- lib/cheeba/writer.rb
Defined Under Namespace
Classes: EmptyFilenameError, EmptyInputError, InvalidFilenameError, InvalidInputError
Class Method Summary collapse
-
.build(object, options) ⇒ Object
array, hash, or string to array of lines of .cash file.
-
.dotfile(options, home) ⇒ Object
write a cheeba dotfile to home dir.
-
.write(object, filename, options) ⇒ Object
array, hash, or string to file.
Class Method Details
.build(object, options) ⇒ Object
array, hash, or string to array of lines of .cash file
12 13 14 15 16 |
# File 'lib/cheeba/writer.rb', line 12 def self.build(object, ) raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?) raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash)) Cheeba::Writer::Builder.build([], object, ) end |
.dotfile(options, home) ⇒ Object
write a cheeba dotfile to home dir
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cheeba/writer.rb', line 38 def self.dotfile(, home) filename = "#{home.chomp("/")}/.cheeba" new_name = nil if File.exists?(filename) new_name = "#{filename}.#{Time.now.strftime("%Y%m%d%H%M%S")}" File.rename(filename, new_name) end self.write(, filename, Cheeba::Indicators.) new_name ? new_name : filename end |
.write(object, filename, options) ⇒ Object
array, hash, or string to file
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cheeba/writer.rb', line 21 def self.write(object, filename, ) filename = "#{ENV['HOME']}/#{File.basename(filename.to_s)}" if (filename =~ /^~/) raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?) raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash)) raise Cheeba::Writer::EmptyFilenameError if File.basename(filename.to_s).strip.empty? raise Cheeba::Writer::InvalidFilenameError unless File.exists?(File.dirname(filename)) File.open(filename, "w") do |file| self.build(object, ).each {|line| file.print("#{line}\n") } end filename end |