Sha256: 7e61f503bfbdd2cb7d0fd6e44e3beb90b648d36d165d2bfefa349f54e4217888

Contents?: true

Size: 820 Bytes

Versions: 9

Compression:

Stored size: 820 Bytes

Contents

require "pathname"

module Dry
  module View
    class Path
      include Dry::Equalizer(:dir, :root)

      attr_reader :dir, :root

      def initialize(dir, options = {})
        @dir = Pathname(dir)
        @root = Pathname(options.fetch(:root, dir))
      end

      def lookup(name, format)
        template?(name, format) || template?("shared/#{name}", format) || !root? && chdir('..').lookup(name, format)
      end

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

      def to_s
        dir
      end

      private

      def root?
        dir == root
      end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-view-0.5.3 lib/dry/view/path.rb
dry-view-0.5.2 lib/dry/view/path.rb
dry-view-0.5.1 lib/dry/view/path.rb
dry-view-0.5.0 lib/dry/view/path.rb
dry-view-0.4.0 lib/dry/view/path.rb
dry-view-0.3.0 lib/dry/view/path.rb
dry-view-0.2.2 lib/dry/view/path.rb
dry-view-0.2.1 lib/dry/view/path.rb
dry-view-0.2.0 lib/dry/view/path.rb