Sha256: c40fe28712aecc87a1710cb59b67db635f49eb466a3a4d2c90a6123f94185fb1

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module ArchivesSpace
  module Template
    def self.list
      Dir.glob ["*"], base: File.join(templates_path)
    end

    def self.process(template, data)
      processor = File.extname(template).delete(".").camelize
      processor = Object.const_get("ArchivesSpace::Template::#{processor}")
      processor.new(template, data).process
    end

    def self.templates_path
      ENV.fetch(
        "ARCHIVESSPACE_CLIENT_TEMPLATES_PATH",
        File.join(File.dirname(File.expand_path(__FILE__)), "templates")
      )
    end

    class Processor
      attr_reader :template, :data

      def initialize(template, data)
        @template = template
        @data = data

        validate_template
      end

      def extension
        raise "Not implemented"
      end

      def read_template
        File.read(File.join(ArchivesSpace::Template.templates_path, template))
      end

      def validate_template
        raise "Invalid template" unless File.extname(template).end_with? extension
      end
    end

    class Erb < Processor
      def extension
        ".erb"
      end

      def process
        t = ERB.new(read_template)
        r = t.result(binding).squeeze("\n")
        JSON.parse(r).to_json
      end
    end

    class Jbuilder < Processor
      def extension
        ".jbuilder"
      end

      def process
        ::Jbuilder.encode do |json|
          eval(read_template, binding) # standard:disable Security/Eval
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
archivesspace-client-0.4.1 lib/archivesspace/client/template.rb
archivesspace-client-0.4.0 lib/archivesspace/client/template.rb
archivesspace-client-0.3.0 lib/archivesspace/client/template.rb