Sha256: cac77e7c40730c900a9e0fc9863a3ccfe248f56cb66d0b3be280405dfe7fd937

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

# typed: strict
# frozen_string_literal: true

module Bhook
  #
  # Valid keys:
  # - website
  #   - link to place in the footer
  # - exclude
  #   - list of .md file paths relative to config files
  class Config
    extend T::Sig
    BHOOK_CONFIG_FILE = T.let('.bhook.yml', String)
    WEBSITE_KEY = T.let('website', String)
    EXCLUDE_KEY = T.let('exclude', String)

    sig { params(root_dir_path: Pathname, additional_options: T::Hash[String, String]).void }
    def initialize(root_dir_path, additional_options = {})
      config_file_path = root_dir_path.join(BHOOK_CONFIG_FILE)
      config = load_config(config_file_path).merge(additional_options)
      @root_dir_path = root_dir_path
      @website_url = T.let(config[WEBSITE_KEY], T.nilable(String))
      @excluded_files = T.let(parse_excluded_files(config[EXCLUDE_KEY] || []), T::Array[Pathname])
    end

    sig { params(src_file_path: Pathname, src_file_sha: T.nilable(String)).returns(T.nilable(String)) }
    def website_url_for(src_file_path, src_file_sha)
      return unless @website_url && src_file_sha

      relative_file_path = src_file_path.relative_path_from(@root_dir_path)
      File.join(@website_url, src_file_sha, relative_file_path)
    end

    sig { params(pathname: Pathname).returns(T::Boolean) }
    def excluded?(pathname)
      @excluded_files.include?(pathname)
    end

    private

    sig { params(excluded_files: T::Array[String]).returns(T::Array[Pathname]) }
    def parse_excluded_files(excluded_files)
      excluded_files.map { |file_path| @root_dir_path.join(file_path) }
    end

    sig do
      params(config_file_path: Pathname).returns(T::Hash[String, T.untyped])
    end
    def load_config(config_file_path)
      if File.exist?(config_file_path)
        L.debug("Config found: #{config_file_path}")
        YAML.safe_load(File.read(config_file_path))
      else
        L.debug("Config not found: #{config_file_path}")
        {}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bhook-0.3.0 lib/bhook/config.rb
bhook-0.2.2 lib/bhook/config.rb
bhook-0.2.1 lib/bhook/config.rb
bhook-0.2.0 lib/bhook/config.rb