Verified Commit ba74adef authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

refactor: Simplifying dir assignment

parent 4701bb3f
Loading
Loading
Loading
Loading
+7 −16
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@ module Labkit

      # @param dir [String, Pathname] Directory path containing YAML file definitions
      #   Defaults to 'config/covered_experiences' relative to the calling application's root
      def initialize(dir: nil)
        @dir = dir
      def initialize(dir: File.join("config", "covered_experiences"))
        @dir = Pathname.new(Dir.pwd).join(dir)
        @experiences = load_on_demand
        @invalid_definitions = {}
      end
@@ -45,8 +45,8 @@ module Labkit
      #
      # @return [Hash] Hash with lazy loading behavior
      def load_on_demand
        unless directory_exists?(directory_path)
          warn("Directory not readable: #{directory_path}")
        unless directory_exists?
          warn("Directory not readable: #{@dir}")
          return {}
        end

@@ -66,22 +66,13 @@ module Labkit
        read_experience(file_path, experience_id) if file_path
      end

      # Resolve the directory path, using default if @dir not provided.
      # Expands relative paths from the current working directory.
      #
      # @return [Pathname] Resolved directory path
      def directory_path
        dir = @dir || File.join("config", "covered_experiences")
        Pathname.new(Dir.pwd).join(dir)
      end

      def directory_exists?(directory_path)
        directory_path.exist? && directory_path.directory? && directory_path.readable?
      def directory_exists?
        @dir.exist? && @dir.directory? && @dir.readable?
      end

      def experience_definitions
        @experience_definitions ||=
          directory_path.glob('*.yml').each_with_object({}) do |file_path, hash| # rubocop:disable Rails/IndexBy
          @dir.glob('*.yml').each_with_object({}) do |file_path, hash| # rubocop:disable Rails/IndexBy
            hash[file_path.basename('.yml').to_s] = file_path
          end
      end