Sha256: 0429e9512659ecbfa5ee66fd65e144aa5c4491f8c14ac3cfdb125e32ce198531

Contents?: true

Size: 1.31 KB

Versions: 8

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module JsDependency
  module CliUtils
    class Yaml
      attr_reader :dir_path,
                  :path

      # @param [String, nil] dir_path
      # @param [String, nil] path
      def initialize(dir_path: nil, path: nil)
        @dir_path = present?(dir_path) ? dir_path : Dir.pwd
        @path = path
      end

      # @return [Hash]
      def args
        pathname = if present?(@path)
                     Pathname.new(@path)
                   else
                     config_pathname
                   end

        if pathname.nil?
          {}
        else
          symbolize_keys(YAML.safe_load(pathname.read))
        end
      end

      private

      # @param [String] str
      # @return [TrueClass, FalseClass]
      def present?(str)
        return false if str.nil?
        return false if str.empty?

        true
      end

      # @return [Pathname, nil]
      def config_pathname
        dir_pathname = Pathname.new(@dir_path)
        pathname = nil
        %w[.js_dependency.yml .js_dependency.yaml].each do |path|
          pathname = (dir_pathname + path) if (dir_pathname + path).exist?
        end
        pathname
      end

      # @param [Hash] hash
      # @return [Hash]
      def symbolize_keys(hash)
        hash.transform_keys(&:to_sym)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
js_dependency-0.4.1 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.4.0 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.15 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.14 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.13 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.12 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.11 lib/js_dependency/cli_utils/yaml.rb
js_dependency-0.3.10 lib/js_dependency/cli_utils/yaml.rb