main
 1# frozen_string_literal: true
 2
 3module Xml
 4  module Kit
 5    class Template
 6      TEMPLATES_DIR = Pathname.new(File.join(__dir__, 'templates/'))
 7
 8      attr_reader :target
 9
10      def initialize(target)
11        @target = target
12      end
13
14      # Returns the compiled template as a [String].
15      #
16      # @param options [Hash] The options hash to pass to the template engine.
17      def to_xml(options = {})
18        template.render(target, options)
19      end
20
21      private
22
23      def template_path
24        return target.template_path if target.respond_to?(:template_path)
25
26        TEMPLATES_DIR.join(template_name)
27      end
28
29      def template_name
30        "#{target.class.name.split('::').last.underscore}.builder"
31      end
32
33      def template
34        @template ||= Tilt.new(template_path.to_s)
35      end
36    end
37  end
38end