Class: Yamljam::Jammer

Inherits:
Object
  • Object
show all
Defined in:
lib/yamljam/jammer.rb

Instance Method Summary collapse

Instance Method Details

#is_yaml?(filename) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/yamljam/jammer.rb', line 48

def is_yaml?(filename)
  %w[yml yaml].include?(filename.split(".").last)
end

#jam(input_directory_name) ⇒ Object



27
28
29
# File 'lib/yamljam/jammer.rb', line 27

def jam(input_directory_name)
  write_output(jam_recursive(input_directory_name), input_directory_name + ".yml")      
end

#jam_recursive(input_directory_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yamljam/jammer.rb', line 31

def jam_recursive(input_directory_name)
  Dir.chdir(input_directory_name) do
    directories = Dir.entries('.').reject{|entry| !File.directory?(entry) || entry == '.' || entry == '..'}
    yaml_files = Dir.entries('.').reject{|entry| File.directory?(entry) || !is_yaml?(entry)}
    merged_yaml = merge_files(yaml_files, input_directory_name)
    more_yamls = directories.map{|directory| jam_recursive(directory)}
    
    total_merged_yaml = {input_directory_name => more_yamls.reduce(merged_yaml[input_directory_name], &:merge)}
    return total_merged_yaml
  end
end

#make_hashmap(input_file) ⇒ Object



23
24
25
# File 'lib/yamljam/jammer.rb', line 23

def make_hashmap(input_file)
  base_hashmap = YAML::load_file(input_file)
end

#merge_files(input_files, namespace) ⇒ Object



19
20
21
# File 'lib/yamljam/jammer.rb', line 19

def merge_files(input_files, namespace)
  {namespace => input_files.map{|input_file| make_hashmap(input_file)}.reduce({}, &:merge)}
end

#merged_hashObject



3
4
5
# File 'lib/yamljam/jammer.rb', line 3

def merged_hash
  @hash ||= {}
end

#name_of_dotdotObject



43
44
45
46
# File 'lib/yamljam/jammer.rb', line 43

def name_of_dotdot
  # is there seriously not a better way to do this?
  Dir.chdir(".."){File.split(Dir.getwd).last}
end

#write_output(output_hashmap, output_file) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yamljam/jammer.rb', line 7

def write_output(output_hashmap, output_file)
  begin
    File.open(output_file, 'w') do |f|
      f.puts("# Generated by Yamljam. Do not directly edit this file.")
      f.write(output_hashmap.to_yaml)
    end
  rescue Exception => ex # if it's any solace I cringed when I wrote that
    puts "hit exception when writing output file! #{ex.class} #{ex.message} #{ex.backtrace}"
    raise ex
  end
end