Sha256: 13780d430fe16dc7d10d045ef53abdcacc4a5d0db6cacea9ee000ce8717e3c8d

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/require_sub'
::EacRubyUtils.require_sub(__FILE__)

module Avm
  module Templates
    class << self
      def template(subpath, required = true)
        path = template_path(subpath)
        if path.blank?
          return nil unless required

          raise "Template not found for subpath \"#{subpath}\" (Included paths: #{included_paths})"
        end
        return ::Avm::Templates::File.new(path) if ::File.file?(path)
        return ::Avm::Templates::Directory.new(path) if ::File.directory?(path)

        raise 'Invalid branching'
      end

      # @return The absolute path of template if found, +nil+ otherwise.
      def template_path(subpath)
        included_paths.each do |included_path|
          r = search_template_in_included_path(included_path, subpath)
          return r if r
        end
        nil
      end

      def included_paths
        @included_paths ||= ::Set.new([::File.expand_path('../../template', __dir__)])
      end

      private

      def search_template_in_included_path(included_path, subpath)
        path = ::File.join(included_path, subpath)
        dir = ::File.dirname(path)
        Dir.entries(dir).each do |entry|
          next if %w[. ..].include?(entry)
          return path if template_basename(entry) == ::File.basename(subpath)
        end
      end

      def template_basename(entry)
        entry.gsub(/(?:\.[a-z0-9]+)+\z/i, '')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
avm-tools-0.22.0 lib/avm/templates.rb
avm-tools-0.21.0 lib/avm/templates.rb
avm-tools-0.20.0 lib/avm/templates.rb
avm-tools-0.19.0 lib/avm/templates.rb