Sha256: 722eb06cbdf2e8511a2120d57aa82ea83769341bf1ae1a3dfccc7b4b86842982

Contents?: true

Size: 826 Bytes

Versions: 7

Compression:

Stored size: 826 Bytes

Contents

# frozen_string_literal: true

module Xml
  module Kit
    class Template
      TEMPLATES_DIR = Pathname.new(File.join(__dir__, 'templates/'))

      attr_reader :target

      def initialize(target)
        @target = target
      end

      # Returns the compiled template as a [String].
      #
      # @param options [Hash] The options hash to pass to the template engine.
      def to_xml(options = {})
        template.render(target, options)
      end

      private

      def template_path
        return target.template_path if target.respond_to?(:template_path)

        TEMPLATES_DIR.join(template_name)
      end

      def template_name
        "#{target.class.name.split('::').last.underscore}.builder"
      end

      def template
        @template ||= Tilt.new(template_path.to_s)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
xml-kit-0.6.0 lib/xml/kit/template.rb
xml-kit-0.5.0 lib/xml/kit/template.rb
xml-kit-0.4.0 lib/xml/kit/template.rb
xml-kit-0.3.1 lib/xml/kit/template.rb
xml-kit-0.3.0 lib/xml/kit/template.rb
xml-kit-0.2.0 lib/xml/kit/template.rb
xml-kit-0.1.14 lib/xml/kit/template.rb