Sha256: cd7119575dabc0cc9232454a2ec6d4ee11a3038c31e03887755a6c2945d8c985

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

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

module Hanami
  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

6 entries across 6 versions & 1 rubygems

Version Path
hanami-view-2.0.0.alpha8 lib/hanami/view/path.rb
hanami-view-2.0.0.alpha7 lib/hanami/view/path.rb
hanami-view-2.0.0.alpha6 lib/hanami/view/path.rb
hanami-view-2.0.0.alpha5 lib/hanami/view/path.rb
hanami-view-2.0.0.alpha3 lib/hanami/view/path.rb
hanami-view-2.0.0.alpha2 lib/hanami/view/path.rb