Module: Configer::Helpers
- Included in:
- Configer
- Defined in:
- lib/configer/logic.rb
Instance Method Summary collapse
- #mount_config(get_pwd = Configer.pwd) ⇒ Object
- #mount_config_environments(get_pwd = Configer.pwd) ⇒ Object
- #mount_lib_meta(get_pwd = Configer.pwd) ⇒ Object
- #mount_lib_module_meta(get_pwd = Configer.pwd) ⇒ Object
-
#mount_process(*paths, break_if: -> e { e.size > 2 }, namespace_less: false) ⇒ Object
> opts:.
- #name_parser(name_str) ⇒ Object
- #parser(file_path) ⇒ Object
- #supported_formats ⇒ Object
Instance Method Details
#mount_config(get_pwd = Configer.pwd) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/configer/logic.rb', line 128 def mount_config(get_pwd=Configer.pwd) mount_process( *Dir.glob( File.join( get_pwd, 'config', "*.{#{supported_formats.join(',')}}" ) ), break_if: -> e { e.size == 1 }, namespace_less: true ) end |
#mount_config_environments(get_pwd = Configer.pwd) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/configer/logic.rb', line 144 def mount_config_environments(get_pwd=Configer.pwd) mount_process( *Dir.glob( File.join( get_pwd, 'config', '{environments,environment,env,envs}', "#{Configer.env}.{#{supported_formats.join(',')}}" ) ), break_if: -> e { e.size >= 1 }, namespace_less: true ) end |
#mount_lib_meta(get_pwd = Configer.pwd) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/configer/logic.rb', line 96 def (get_pwd=Configer.pwd) mount_process( *Dir.glob( File.join( get_pwd, '{lib,libs}', '{meta,META}', '**', "*.{#{supported_formats.join(',')}}" ) ) ) end |
#mount_lib_module_meta(get_pwd = Configer.pwd) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/configer/logic.rb', line 112 def (get_pwd=Configer.pwd) mount_process( *Dir.glob( File.join( get_pwd, '{lib,libs}', '*', '{meta,META}', '**', "*.{#{supported_formats.join(',')}}" ) ) ) end |
#mount_process(*paths, break_if: -> e { e.size > 2 }, namespace_less: false) ⇒ Object
> opts:
category:
namespace where the gathered file datas will be merged
split_at:
string or regexp where the folder located
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 |
# File 'lib/configer/logic.rb', line 53 def mount_process( *paths,break_if: -> e { e.size > 2 }, namespace_less: false ) return paths.reduce(Object.new) do |m,file_path| if namespace_less #> if some non matching type wanted to be merged m.deep_merge!(parser(file_path)) rescue nil else #> keys will be downcase anyways elements = [] file_path.split(File::Separator).reverse.each do |element| next if element =~ /^(META|meta)$/ break if element =~ /^(config|libs?)$/ elements.unshift element break if break_if.call(elements) end key_cains = [] m.deep_merge!( elements.reduce(Object.new) do |mem,element| target_obj = mem key_cains.each{|k| target_obj = target_obj[k] } key_cains.push(element) if elements[-1] == element target_obj[name_parser(element)] = parser(file_path) else target_obj[name_parser(element)] ||= Object.new end;mem end ) end;m end end |
#name_parser(name_str) ⇒ Object
42 43 44 |
# File 'lib/configer/logic.rb', line 42 def name_parser(name_str) name_str.gsub(/(\.json|\.yaml|\.yml|\.erb)/,'') end |
#parser(file_path) ⇒ Object
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 |
# File 'lib/configer/logic.rb', line 9 def parser(file_path) file_string = if file_path.downcase =~ /\.erb\.?/ require 'erb' ERB.new(File.read(file_path)).result else File.read(file_path) end object = if file_path =~ /\.ya?ml(\.erb)?$/ require 'yaml' #> back compatibility for old yaml parse logic if YAML.respond_to?(:save_load) YAML.safe_load(file_string) else YAML.load(file_string) end elsif file_path =~ /\.json(\.erb)?$/ require 'json' JSON.parse(file_string) else file_string end return Object.parse(object) rescue;nil end |
#supported_formats ⇒ Object
5 6 7 |
# File 'lib/configer/logic.rb', line 5 def supported_formats @formats ||= %W[ erb json yaml yml ] end |