Class: OpenActive::DatasetSite::TemplateRenderer

Inherits:
Mustache
  • Object
show all
Defined in:
lib/openactive/dataset_site/template_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, static_assets_path_url = nil) ⇒ TemplateRenderer

Returns a new instance of TemplateRenderer.



8
9
10
11
12
13
14
15
16
# File 'lib/openactive/dataset_site/template_renderer.rb', line 8

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(orig_method_name, *args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/openactive/dataset_site/template_renderer.rb', line 32

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

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



6
7
8
# File 'lib/openactive/dataset_site/template_renderer.rb', line 6

def settings
  @settings
end

Instance Method Details

#datasetObject



18
19
20
21
22
# File 'lib/openactive/dataset_site/template_renderer.rb', line 18

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

  @dataset ||= settings.to_dataset
end

#jsonldObject



24
25
26
# File 'lib/openactive/dataset_site/template_renderer.rb', line 24

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

#respond_to_missing?(orig_method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/openactive/dataset_site/template_renderer.rb', line 42

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

#staticAssetsPathUrlObject



28
29
30
# File 'lib/openactive/dataset_site/template_renderer.rb', line 28

def staticAssetsPathUrl
  @static_assets_path_url
end