Module: Configer::Support

Defined in:
lib/configer/support.rb

Class Method Summary collapse

Class Method Details

.mount_config_and_lib_metaObject

> return mounted config objects



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/configer/support.rb', line 6

def self.mount_config_and_lib_meta

  return_hash = {}
  return_hash.__send__ :extend, HashExtension

  config_yaml_paths= []
  config_yaml_paths.instance_eval do
    def push_path(*paths)
      paths.each do |path|
        case true

          when path.downcase.include?('default')
            self.insert 0, path

          when path.downcase.include?('development')
            self.insert 1, path

          when path.downcase.include?('test')
            self.insert 2, path

          when path.downcase.include?('production')
            self.insert 3, path

          else
            self.push path

        end
      end
    end;alias push_paths push_path
  end

  #> load lib meta folders files
  if File.exist?(File.join(Dir.pwd,'lib'))
    config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'lib','**','{meta,META}','*.{yaml,yml,json}'))
    config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'lib','{meta,META}','**','*.{yaml,yml,json}'))
    config_yaml_paths.compact!
  end

  #> load config folder
  if File.exist?(File.join(Dir.pwd,'config'))

    config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'config','*.{yaml,yml,json}'))
    if File.exist?(File.join(Dir.pwd,'config','environments'))
      config_yaml_paths.push_paths *Dir.glob(File.join(Dir.pwd,'config','environments','*.{yaml,yml,json}'))
    end
    config_yaml_paths.compact!

  end

  config_yaml_paths.each do |path|

    path_parts = path.split(File::Separator)

    extension = path_parts.last.split('.')[-1]

    category_key = case

                     #> lib/meta/*
                     when path_parts[-2].downcase == 'meta' && path_parts[-3] == 'lib'
                       nil

                     #> lib/meta/**/*
                     when path_parts[-3].downcase == 'meta' && path_parts[-4] == 'lib'
                       path_parts[-2]

                     #> lib/**/meta/*
                     when path_parts[-2].downcase == 'meta' && path_parts[-3] != 'lib' && path_parts[-4] == 'lib'
                       path_parts[-3]

                     else
                       nil

                   end

    object = if %W[ yaml yml ].include?(extension)
               require 'yaml'
               YAML.safe_load(File.read(path))
             elsif extension == 'json'
               require 'json'
               JSON.parse(File.read(path))
             else
               {}
             end

    if path.downcase.include?('meta')
      object = {
          ( path_parts.last.split('.')[-2] || path ) => object
      }
    end

    if category_key.nil?
      return_hash.deep_merge!(object)
    else
      return_hash.deep_merge!(category_key => object)
    end

  end

  return return_hash

end