Sha256: db8b43e4bc4b15311782e18d2a72261ae62b3d2d7b34676671ee10eef1d9c10a

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

require 'mustache'

module OpenActive
  module DatasetSite
    class TemplateRenderer < Mustache
      attr_reader :settings

      def initialize(settings, static_assets_path_url = nil)
        @settings = settings
        if static_assets_path_url.nil? then
          @template_file =  "#{__dir__}/datasetsite.mustache"
        else
          @template_file =  "#{__dir__}/datasetsite-csp.mustache"
          @static_assets_path_url = static_assets_path_url.chomp("/")
        end
      end

      def dataset
        return settings if settings.is_a?(OpenActive::Models::Dataset)

        @dataset ||= settings.to_dataset
      end

      def jsonld
        dataset.to_json(schema: true, pretty: true)
      end

      def staticAssetsPathUrl
        @static_assets_path_url
      end

      def method_missing(orig_method_name, *args)
        method_name = orig_method_name.to_s.underscore

        return super unless dataset.respond_to?(method_name)

        val = dataset.public_send(method_name, *args)

        OpenActive::Helpers::JsonLd.serialize_value(val, nil)
      end

      def respond_to_missing?(orig_method_name, include_private = false)
        method_name = orig_method_name.to_s.underscore

        dataset.respond_to?(method_name) || super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
openactive-dataset_site-7.0.0 lib/openactive/dataset_site/template_renderer.rb
openactive-dataset_site-6.0.2 lib/openactive/dataset_site/template_renderer.rb
openactive-dataset_site-6.0.1 lib/openactive/dataset_site/template_renderer.rb
openactive-dataset_site-6.0.0 lib/openactive/dataset_site/template_renderer.rb