Sha256: 6f881bb48bb849bfd5b78243249431866bf3694424b9efdb4c4dcef0688dc533

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

require "yaml"
require "platina_world/errors/file_path_error"
require "platina_world/errors/invalid_extension_error"

module PlatinaWorld
  class FileLoader
    DEFAULT_FILE_PATH = "./platina_world.yml".freeze
    ACCEPTABLE_EXTENSIONS = %w(.yml .yaml).freeze

    def initialize(file_path)
      @file_path = file_path
    end

    def load
      case
      when !exist_file?
        PlatinaWorld::Logger.error(file_path_error) and abort
      when !valid_extension_type?
        PlatinaWorld::Logger.error(invalid_extion_error) and abort
      else
        file_load
      end
    end

    private

    def file_path_error
      PlatinaWorld::Errors::FilePathError.new(file_path)
    end

    def invalid_extion_error
      PlatinaWorld::Errors::InvalidExtensionError.new(ACCEPTABLE_EXTENSIONS)
    end

    def file_path
      @file_path ||= DEFAULT_FILE_PATH
    end

    def file_load
      YAML.load_file(file_path)
    end

    def exist_file?
      File.exist?(file_path)
    end

    def valid_extension_type?
      ACCEPTABLE_EXTENSIONS.include?(File.extname(file_path))
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
platina_world-0.1.5 lib/platina_world/file_loader.rb
platina_world-0.1.4 lib/platina_world/file_loader.rb
platina_world-0.1.3 lib/platina_world/file_loader.rb
platina_world-0.1.2.1 lib/platina_world/file_loader.rb
platina_world-0.1.2 lib/platina_world/file_loader.rb
platina_world-0.1.1 lib/platina_world/file_loader.rb