Module: YamlToConstants
- Defined in:
- lib/yaml_to_constants.rb
Overview
1) Loads all files out of config/yaml_autoload/*/.yaml 2) Uses namespace based on file structure (overseer/blah.yaml) becomes
Configuration::Overseer::Blah
3) Only loads configuration for locale or generic configuration 4) Checks for environment names at top level for environment specific configuration
Constant Summary collapse
- @@constants_created =
Set.new
- @@default_namespace =
"Configuration"
Class Method Summary collapse
- .constant_from_file_path(file_path) ⇒ Object
- .default_namespace ⇒ Object
- .directory_load_path ⇒ Object
- .directory_load_path=(directory_path) ⇒ Object
- .initilize_all_files ⇒ Object
-
.load_configurations {|_self| ... } ⇒ Object
Configuration variables.
-
.loaded_file_paths ⇒ Object
functional methods.
- .methods_from_file(file_path) ⇒ Object
- .namespace=(name_space) ⇒ Object
-
.parse_file(file_path) ⇒ Object
defined? Constant will check to see if it exists.
- .purge! ⇒ Object
- .recursive=(bool) ⇒ Object
- .recursive? ⇒ Boolean
Class Method Details
.constant_from_file_path(file_path) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/yaml_to_constants.rb', line 106 def constant_from_file_path(file_path) cst = file_path.gsub(directory_load_path, "").split(/\/|\./).compact [default_namespace, cst].flatten.reject{|k| k =~ /yaml/i }.map(&:classify).compress end |
.default_namespace ⇒ Object
89 90 91 |
# File 'lib/yaml_to_constants.rb', line 89 def default_namespace @@default_namespace end |
.directory_load_path ⇒ Object
93 94 95 |
# File 'lib/yaml_to_constants.rb', line 93 def directory_load_path @@directory_load_path end |
.directory_load_path=(directory_path) ⇒ Object
66 67 68 |
# File 'lib/yaml_to_constants.rb', line 66 def directory_load_path=(directory_path) @@directory_load_path = directory_path end |
.initilize_all_files ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/yaml_to_constants.rb', line 20 def initilize_all_files loaded_file_paths.each do |file_path| new_constant = constant_from_file_path(file_path) new_constant_full = new_constant.join('::') full_path = [] new_constant.each do |const| full_path << const full_path_text = full_path.join("::") unless @@constants_created.include?(full_path_text) Kernel.eval("#{full_path_text} = Module.new") @@constants_created.add(full_path_text) end end to_eval = ("#{new_constant_full} = YamlToConstants.methods_from_file('#{file_path}') ") puts to_eval Kernel.eval(to_eval) @@constants_created.add(new_constant_full) end end |
.load_configurations {|_self| ... } ⇒ Object
Configuration variables
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/yaml_to_constants.rb', line 51 def load_configurations #reset defaults @@configuration_constants = nil @@loaded_file_paths = nil @@recursive = true #Clean any left over constants from previous setup purge! #Assign options yield self #Create new constants with yaml values initilize_all_files end |
.loaded_file_paths ⇒ Object
functional methods
12 13 14 15 16 17 18 |
# File 'lib/yaml_to_constants.rb', line 12 def loaded_file_paths @@loaded_file_paths ||= begin path = self.directory_load_path path += "/**/*" if self.recursive? Dir.glob(path).select { |fn| File.file?(fn) } end end |
.methods_from_file(file_path) ⇒ Object
102 103 104 |
# File 'lib/yaml_to_constants.rb', line 102 def methods_from_file(file_path) RecursiveOpenStruct.new(parse_file(file_path), :recurse_over_arrays => true) end |
.namespace=(name_space) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/yaml_to_constants.rb', line 74 def namespace=(name_space) name_space = if name_space.is_a?(String) name_space.constantize = Module.new else name_space end @@constants_created << name_space @@default_namespace = name_space end |
.parse_file(file_path) ⇒ Object
defined? Constant will check to see if it exists
98 99 100 |
# File 'lib/yaml_to_constants.rb', line 98 def parse_file(file_path) Psych.load_file(file_path) end |
.purge! ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/yaml_to_constants.rb', line 42 def purge! puts "PURGE " << @@constants_created.inspect @@constants_created = Set.new #Purge all objects created from the system for reset #Need to figure this out # Object.send(:remove_const, "#{default_namespace}".to_sym) if defined?(default_namespace.to_sym) end |
.recursive=(bool) ⇒ Object
70 71 72 |
# File 'lib/yaml_to_constants.rb', line 70 def recursive=(bool) @@recursive = bool end |
.recursive? ⇒ Boolean
85 86 87 |
# File 'lib/yaml_to_constants.rb', line 85 def recursive? @@recursive end |