Sha256: 4a1e78f27a1ce4c3bd1c7dd2333668aa81b67e30d5da91678e39f28dd1018302

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

module Appfuel
  module Configuration
    # Handle loading files and parsing them correctly based on their type.
    # The file loader used for loading configuration data into a definition
    module FileLoader
      attr_writer :file_module, :json_module, :yaml_module

      def file_module
        @file_module ||= ::File
      end

      def json_module
        @json_module || ::JSON
      end

      def yaml_module
        @yaml_module ||= YAML
      end

      # @param path [String]
      # @return [Hash]
      def parse_json(path)
        file = file_module.read(path)
        json_module.parse(file)
      end

      # @param path [String]
      # @return [Hash]
      def parse_yaml(path)
        yaml_module.load_file(path)
      end

      # Load file will search through a configuration's definition file
      # paths and use the first on that exists. It parse it based on
      # the file type.
      #
      # @raises [RuntimeException] when no files are found
      #
      # @param definition [DefinitionDsl]
      # @return [Hash]
      def load_file(definition)
        paths = definition.file
        key   = definition.key

        paths.each do |path|
          ext = file_module.extname(path).strip.downcase[1..-1]
          parse_method = "parse_#{ext}"
          unless respond_to?(parse_method)
            fail "extension (#{ext}), for (#{key}: #{path}) is not valid, " +
                 "only yaml and json are supported"
          end

          return public_send(parse_method, path) if file_module.exists?(path)
        end

        list = paths.join(',')
        fail "none of :#{key} config files exist at (#{list})"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
appfuel-0.2.8 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.7 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.6 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.5 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.4 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.3 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.2.pre.alpha.pre.140 lib/appfuel/configuration/file_loader.rb
appfuel-0.2.0 lib/appfuel/configuration/file_loader.rb