Sha256: 9615bb8c14f2c41910b00f04b7b4baeb86c85c44087b759bf999c06527a286a4

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 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) { h[$1.to_sym] = parse_file(f) }
        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))[YAML_VERSION])).deep_symbolize_keys
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ood_appkit-0.3.4 lib/ood_appkit/config_parser.rb
ood_appkit-0.3.3 lib/ood_appkit/config_parser.rb
ood_appkit-0.3.2 lib/ood_appkit/config_parser.rb
ood_appkit-0.3.1 lib/ood_appkit/config_parser.rb
ood_appkit-0.3.0 lib/ood_appkit/config_parser.rb