Class: Mech::Producer::Default

Inherits:
Mech::Producer show all
Defined in:
lib/mech/producer/default.rb

Instance Method Summary collapse

Methods inherited from Mech::Producer

#initialize

Methods included from Configurator

#config, #init

Constructor Details

This class inherits a constructor from Mech::Producer

Instance Method Details

#load_dir_contents(path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mech/producer/default.rb', line 56

def load_dir_contents(path)
  dir = Dir.new(path)

  dirs = dir.find.to_a.select { |name|
    File.directory?(File.join(path, name)) && ! ['.', '..', '...'].include?(name)
  }.map { |name| [name, File.join(path, name)] }

  contents = dir.find.to_a.select { |name|
    name =~ /\.yml/
  }.map { |name|
    [ name.gsub(/\.yml/, ''), Psych.load(File.read(File.join(path, name))) ]
  }
  contents = Hash[contents]

  nested_contents = {}
  dirs.each { |name, dir|
    nested_contents[name] = load_dir_contents(dir)
  }

  if dirs.size.zero?
    return contents.values.first
  else
    objs = {}

    nested_contents.each do |type, scope|
      scope.each do |name, v|
        objs[name] = contents[type].deep_merge(v) if nested_contents[type][name]
      end
    end

    return objs
  end

end

#load_meta_type_objs(src_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mech/producer/default.rb', line 35

def load_meta_type_objs(src_path)
  paths = Dir.new(src_path).find.to_a.select do |path|
    path =~ /.*\.yml/
  end

  keys = paths.map { |path| path.gsub(/\.yml/, '') }

  values = paths.map do |path|
    content = File.read(File.join(src_path, path))
    Psych.load(content)
  end

  obj = {}

  keys.each_with_index do |key, index|
    obj[key] = values[index]
  end

  obj
end

#load_meta_type_titlesObject



28
29
30
31
32
33
# File 'lib/mech/producer/default.rb', line 28

def load_meta_type_titles
  paths = Dir.new(config.src_path).find.to_a.select do |path|
    path =~ /.*\.yml/
  end
  paths.map { |path| path.gsub(/\.yml/, '') }
end

#produceObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mech/producer/default.rb', line 3

def produce
  @common_obj = Psych.load(File.read(config.src_path + '/common.yml'))

  @meta_type_title = load_meta_type_titles - [ 'common' ]
  @primary_meta_type_objs  = load_meta_type_objs(config.src_path)

  src_path = config.src_path
  env = config.env
  env_src_path = File.join(src_path, env)

  @secodry_meta_type_objs  = load_meta_type_objs(env_src_path)

  contents = {}
  @meta_type_title.each do |type|
    contents[type] = load_dir_contents(File.join(env_src_path, type))
    contents[type].each do |name, val|
      meta_type_obj = @common_obj.deep_merge(@primary_meta_type_objs[type])
      meta_type_obj = meta_type_obj.deep_merge(@secodry_meta_type_objs[type])
      contents[type][name] = meta_type_obj.deep_merge(val)
    end
  end

  contents
end