Module: Cheeba::Writer

Defined in:
lib/cheeba/writer.rb

Defined Under Namespace

Classes: EmptyFilenameError, EmptyInputError, InvalidFilenameError, InvalidInputError

Class Method Summary collapse

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, options)
  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, options)
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(options, 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(options, filename, Cheeba::Indicators.options)
  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, options)
  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, options).each {|line|
      file.print("#{line}\n")
    }
  end 
  filename
end