Class: Hasmenu::Formatter

Inherits:
Object
  • Object
show all
Includes:
Printer
Defined in:
lib/hasmenu/formatter.rb

Instance Method Summary collapse

Methods included from Printer

#print_build_for, #print_build_start, #print_format_for, #print_format_start, #print_header, #print_invalid_build, #print_invalid_path, #print_invalid_report, #print_invalid_sequence, #print_invalid_version, #print_report, #print_warn_repeats

Constructor Details

#initialize(options) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
# File 'lib/hasmenu/formatter.rb', line 7

def initialize(options)
  @location = options[:location] || Dir.pwd
end

Instance Method Details

#format(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hasmenu/formatter.rb', line 23

def format(path)
  path = File.join(@location, path)

  unless File.exist? path
    print_invalid_path
    return
  end

  print_format_start
  if File.file?(path) && File.extname(path) == ".yml"
    format_file path
  elsif File.directory? path
    format_files path
  else
    print_invalid_path
  end
end

#format_file(path) ⇒ Object



11
12
13
14
15
# File 'lib/hasmenu/formatter.rb', line 11

def format_file(path)
  file = YAML.load_file(path)
  File.open(path, "w") { |f| f.write file.to_yaml }
  print_format_for File.basename(File.dirname(path))
end

#format_files(path) ⇒ Object



17
18
19
20
21
# File 'lib/hasmenu/formatter.rb', line 17

def format_files(path)
  Dir.glob(path + "/**/*.yml") do |file|
    format_file file
  end
end