Sha256: 19c4a32b01771472f6139d9b120c48cafbe06b1aed8f8a3db7f79c4dd53eb4cc
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
require 'yaml' require 'json' module OodAppkit # Helper methods for parsing/deserializing yml configuration files module ConfigParser # Specific version hash in the yaml file to parse YAML_VERSION = 'v1' # Identifier used to distinguish class names when deserializing CLASS_ID = 'type' # Exception raise when unable to access config file or directory class InvalidConfigPath < StandardError; end # Parse/deserialize a configuration file or a set of configuration files # @param config [#to_s] configuration file or directory # @raise [InvalidConfigPath] if config path is inaccessible # @return [Hash] hash of deserialized config file def self.parse(config:) # use 'type' to distinguish class name in yaml file JSON.create_id = CLASS_ID config = Pathname.new(config.to_s).expand_path if config.file? parse_file config elsif config.directory? config.children.each_with_object({}) do |f, h| /^(.+)\.yml$/.match(f.basename.to_s) do hsh = parse_file(f) h[$1.to_sym] = hsh unless hsh.empty? end end else raise InvalidConfigPath, "invalid config path: #{config}" end end private # Parse a single yaml file def self.parse_file(file) JSON.load(JSON.dump(YAML.load(File.read(file.to_s)).fetch(YAML_VERSION, {}))).deep_symbolize_keys end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ood_appkit-0.3.6 | lib/ood_appkit/config_parser.rb |
ood_appkit-0.3.5 | lib/ood_appkit/config_parser.rb |