Sha256: da978e8c958a4a95bbfe92b15cea9b8fd2a84c3667165cc41f568417041677d6

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require "pathname"
require "dry/core/cache"

module Dry
  class View
    # @api private
    class Path
      extend Dry::Core::Cache
      include Dry::Equalizer(:dir, :root)

      attr_reader :dir, :root

      def self.[](path)
        if path.is_a?(self)
          path
        else
          new(path)
        end
      end

      def initialize(dir, root: dir)
        @dir = Pathname(dir)
        @root = Pathname(root)
      end

      def lookup(name, format, child_dirs: [], parent_dir: false)
        fetch_or_store(dir, root, name, format, child_dirs, parent_dir) do
          lookup_template(name, format) ||
            lookup_in_child_dirs(name, format, child_dirs: child_dirs) ||
            parent_dir && lookup_in_parent_dir(name, format, child_dirs: child_dirs)
        end
      end

      def chdir(dirname)
        self.class.new(dir.join(dirname), root: root)
      end

      def to_s
        dir.to_s
      end

      private

      def root?
        dir == root
      end

      # Search for a template using a wildcard for the engine extension
      def lookup_template(name, format)
        glob = dir.join("#{name}.#{format}.*")
        Dir[glob].first
      end

      def lookup_in_child_dirs(name, format, child_dirs:)
        child_dirs.reduce(nil) { |_, dir|
          template = chdir(dir).lookup(name, format)
          break template if template
        }
      end

      def lookup_in_parent_dir(name, format, child_dirs:)
        !root? && chdir("..").lookup(name, format, child_dirs: child_dirs, parent_dir: true)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-view-0.8.0 lib/dry/view/path.rb
dry-view-0.7.1 lib/dry/view/path.rb