Sha256: 2f65719e9e65cb3caa326088c1f92a43b4e7067ddef38e267c39a04ecc736f4a

Contents?: true

Size: 1.45 KB

Versions: 157

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/object/blank'
require 'eac_ruby_utils/templates/directory'
require 'eac_ruby_utils/templates/file'

module EacRubyUtils
  module Templates
    class Searcher
      class << self
        def default
          @default ||= new
        end
      end

      def template(subpath, required = true)
        path = template_path(subpath)
        if path.blank?
          return nil unless required

          raise_template_not_found(subpath)
        end
        return ::EacRubyUtils::Templates::File.new(path) if ::File.file?(path)
        return ::EacRubyUtils::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
      end

      private

      def raise_template_not_found(subpath)
        raise "Template not found for subpath \"#{subpath}\"" \
          " (Included paths: #{included_paths.to_a.join(::File::PATH_SEPARATOR)})"
      end

      def search_template_in_included_path(included_path, subpath)
        path = ::File.join(included_path, subpath)
        ::File.exist?(path) ? path : nil
      end
    end
  end
end

Version data entries

157 entries across 157 versions & 3 rubygems

Version Path
avm-tools-0.98.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.97.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.96.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.63.0 lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.95.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.62.1 lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.94.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.62.0 lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.61.0 lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.94.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.60.0 lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.94.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
eac_ruby_utils-0.59.0 lib/eac_ruby_utils/templates/searcher.rb
ehbrs-tools-0.20.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.94.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.93.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.92.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.91.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.90.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb
avm-tools-0.89.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/templates/searcher.rb