Class: Hasmenu::Formatter
- Inherits:
-
Object
- Object
- Hasmenu::Formatter
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_format, #print_invalid_path, #print_invalid_report, #print_invalid_sequence, #print_invalid_version, #print_report, #print_warn_repeats
Constructor Details
#initialize(type, options) ⇒ Formatter
Returns a new instance of Formatter.
7
8
9
10
|
# File 'lib/hasmenu/formatter.rb', line 7
def initialize(type, options)
@type = type.chomp("/")
@location = options[:location] || Dir.pwd
end
|
Instance Method Details
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/hasmenu/formatter.rb', line 40
def format(path)
path = File.join(@location, path)
unless valid_path? path
print_invalid_path
return
end
print_format_start
case @type
when "chains", "restaurants"
format_list path
when "menu"
if File.directory? path
format_files path
else
format_file path
end
else
print_invalid_format
end
end
|
28
29
30
31
32
|
# File 'lib/hasmenu/formatter.rb', line 28
def format_file(path)
data = YAML.load_file(path)
File.open(path, "w") { |f| f.write data.to_yaml }
print_format_for File.basename(File.dirname(path))
end
|
34
35
36
37
38
|
# File 'lib/hasmenu/formatter.rb', line 34
def format_files(path)
Dir.glob(File.join(path, "**", "*.yml")) do |file|
format_file file
end
end
|
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/hasmenu/formatter.rb', line 16
def format_list(path)
data = YAML.load_file(path)
unless data.is_a? Array
print_invalid_path
return
end
data.sort_by! { |e| [e["chain"], e["uid"]] }
File.open(path, "w") { |f| f.write data.to_yaml }
print_format_for File.basename(path)
end
|
#valid_path?(path) ⇒ Boolean
12
13
14
|
# File 'lib/hasmenu/formatter.rb', line 12
def valid_path?(path)
(File.file?(path) && File.extname(path) == ".yml") || File.directory?(path)
end
|