Sha256: 677d338aed1fedfdb54c6deb403d2c4a69c1915f24c0f3e3a9182b741222b86b

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module ConfigFiles
  class LoaderFactory
    class << self
      public
      def call(file_name, options={include_default: true})
        new(options).call(file_name)
      end
    end

    private
    attr_reader :options

    def default_loaders
      {
        Loaders::Yaml => ['yaml', 'yml'],
        Loaders::Json =>  ['json']
      }
    end

    def default_loader
      options[:default_loader]
    end

    def default_options
      {
        include_default: true,
        default_loader: Loaders::Yaml,
        loaders: default_loaders
      }
    end

    def include_default_loaders?
      options[:include_default]
    end

    def loaders
      options[:loaders]
    end

    def initialize(options) #Note the check below is necessary, because we only want to do it if it is explicity set
      @options=default_options.merge(options)
      if include_default_loaders?
        @options[:loaders]=default_loaders.merge(loaders)
      end
    end

    public
    def call(file_name)
      loaders.detect{|_, extensions| extensions.include?(::File.extname(file_name).strip.downcase[1..-1])}&.first || default_loader
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
config_files-0.1.7 lib/config_files/loader_factory.rb
config_files-0.1.6 lib/config_files/loader_factory.rb
config_files-0.1.5 lib/config_files/loader_factory.rb
config_files-0.1.3 lib/config_files/loader_factory.rb
config_files-0.1.2 lib/config_files/loader_factory.rb
config_files-0.1.1 lib/config_files/loader_factory.rb
config_files-0.1.0 lib/config_files/loader_factory.rb