Module: Loader

Defined in:
lib/loader.rb,
lib/loader/meta.rb

Class Method Summary collapse

Class Method Details

.caller_folder(integer_num = 1) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/loader/meta.rb', line 111

def caller_folder integer_num= 1

  # path create from caller
  begin
    path= caller[integer_num].split(".{rb,ru}:").first.split(File::Separator)
    path= path[0..(path.count-2)]
  end

  # after formatting
  begin

    if !File.directory?(path.join(File::SEPARATOR))
      path.pop
    end
    path= File.join(path.join(File::SEPARATOR))
    if path != File.expand_path(path)
      path= File.expand_path(path)
    end

  end

  return path

end

.caller_root(*args) ⇒ Object Also known as: caller_root_folder

you can give optional file names that will be searched for



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/loader/meta.rb', line 137

def caller_root(*args)

  what_can_be_in_the_root= %w[
          gemfile Gemfile GemFile
          rakefile Rakefile RakeFile
          config.ru README.md ] + args.map{|e|e.to_s}

  folder_path= caller_folder(2).split(File::Separator)

  loop do

    Dir.glob(File.join(folder_path.join(File::Separator),"*")).map do |element|
      if !File.directory?(element)
        if what_can_be_in_the_root.include? element.split(File::Separator).last
          return folder_path.join(File::Separator)
        end
      end
    end

    if folder_path.count != 0
      folder_path.pop
    else
      return nil
    end

  end

end

.directory_path ⇒ Object Also known as: directory



4
5
6
# File 'lib/loader/meta.rb', line 4

def directory_path
  self.caller_folder
end

.meta(*args) ⇒ Object

load meta folders rb files by default it will be the caller objects root folder (app root) else you must set with hand the path from your app root example:

Loader.meta

Loader.meta 'lib','**','config_files'

Loader.meta root: "/home/...../my_app",'lib','**','meta'

Loader.meta 'lib','**','meta', root: "/home/...../my_app"

You can use the "absolute: string_path" option just alike so it wont try find your app root folder

All will return a Hash obj with the loaded meta configs based on the yaml file name as key and the folder as the category



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/loader/meta.rb', line 187

def meta( *args )

  options= args.extract_class!(Hash)[0]
  options ||= {}

  if options[:absolute].nil?

    if args.empty?
      args= ["{lib,libs,library,libraries}","**","meta"]
    end

    if !options[:root].nil?
      root_folder_path= options[:root]
    else
      root_folder_path= caller_root_folder
    end

    args.unshift(root_folder_path)

  else
    args= [options[:absolute]]
  end

  target_folder= File.join(*args)

  # load ruby files from meta
  begin
    Dir.glob( File.join(target_folder,"*.{rb,ru}") ).each do |one_rb_file|
      require one_rb_file
    end
  end

  # load yaml files elements
  begin

    target_config_hash= Hash.new
    Dir.glob(File.join(target_folder,"*.{yaml,yml}")).each do |config_object|

      # defaults
      begin
        config_name_elements= config_object.split(File::SEPARATOR)

        type=     config_name_elements.pop.split('.')[0]
        config_name_elements.pop

        category= config_name_elements.pop
        yaml_data= YAML.load(File.open(config_object))
      end

      # processing
      begin
        target_config_hash[category]       ||= {}
        target_config_hash[category][type] ||= {}
        target_config_hash[category][type]  =  yaml_data
      end

    end

  end

  # return data
  begin
    return target_config_hash
  end

end

.metaloader_framework(opts = {}) ⇒ Object

gives you a basic meta load framework for easy config use (yaml) basic system is

root folder:

  • config

-| "YAML files" #> development.yaml

  • lib -- module_name --- meta

---| "YAML files" #> rack.yaml



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
107
108
109
# File 'lib/loader/meta.rb', line 22

def metaloader_framework(opts={})

  # defaults
  begin

    root_folder         =  opts[:root]
    root_folder        ||= caller_root_folder

    target_config_hash  =  opts[:config_obj]
    target_config_hash ||= Hash.new

    lib_folder          =  opts[:lib_folder]
    lib_folder         ||= File.join(root_folder,"{lib,libs}","**","meta")

    config_folder       =  opts[:config_folder]
    config_folder      ||= File.join(root_folder,"{config,conf}","**")

    input_config_file   =  opts[:config_file]
    environment         =  opts[:environment]

  end

  target_config_hash.deep_merge! Loader.meta( absolute: lib_folder )

  # update by config
  begin

    # get config files
    begin
      config_yaml_paths= Array.new()
      Dir.glob(File.join(config_folder,"*.{yaml,yml}")).uniq.each do |one_path|

        case true

          when one_path.downcase.include?('default')
            config_yaml_paths[0]= one_path

          when one_path.downcase.include?('development')
            config_yaml_paths[1]= one_path

          when one_path.downcase.include?('test')
            config_yaml_paths[2]= one_path

          when one_path.downcase.include?('production')
            config_yaml_paths[3]= one_path

          else
            config_yaml_paths[config_yaml_paths.count]= one_path

        end

      end
      config_yaml_paths.select!{|x| x != nil }
    end

    # params config load
    unless input_config_file.nil?
      begin
        path= File.expand_path(input_config_file,"r")
        File.open(path)
        config_yaml_paths.push(path)
      rescue Exception
        config_yaml_paths.push(input_config_file)
      end
    end

    # load to last lvl environment
    begin
      config_yaml_paths.each do |one_yaml_file_path|
        begin
          yaml_data= YAML.load(File.open(one_yaml_file_path))
          target_config_hash.deep_merge!(yaml_data)

          unless environment.nil?
            if one_yaml_file_path.include?(environment.to_s)
              break
            end
          end
        rescue Exception
        end
      end
    end

  end

  return target_config_hash

end