Module: Yamlr::Writer

Defined in:
lib/yamlr/writer.rb,
lib/yamlr/writer/builder.rb

Defined Under Namespace

Modules: Builder 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 .yml file



12
13
14
15
16
# File 'lib/yamlr/writer.rb', line 12

def self.build(object, options)
  raise Yamlr::Writer::EmptyInputError if object.is_a?(String) && object.strip.empty?
  raise Yamlr::Writer::InvalidInputError unless object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash)
  Yamlr::Writer::Builder.build([], object, options)
end

.dotfile(options, home) ⇒ Object

write a yamlr dotfile to home dir



36
37
38
39
40
41
42
43
44
45
# File 'lib/yamlr/writer.rb', line 36

def self.dotfile(options, home)
  filename = "#{home.chomp("/")}/.yamlrc"
  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, Yamlr::Indicators.options)
  new_name ? new_name : filename
end

.write(object, filename, options) ⇒ Object

array, hash, or string to file



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yamlr/writer.rb', line 20

def self.write(object, filename, options)
  filename = "#{ENV['HOME']}/#{File.basename(filename.to_s)}" if (filename =~ /^~/)
  raise Yamlr::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?)
  raise Yamlr::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash))
  raise Yamlr::Writer::EmptyFilenameError if File.basename(filename.to_s).strip.empty?
  raise Yamlr::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