main
 1# frozen_string_literal: true
 2
 3module Scim
 4  module Kit
 5    # Implement methods necessary to generate json from jbuilder templates.
 6    module Templatable
 7      # Returns the JSON representation of the item.
 8      # @param options [Hash] the hash of options to forward to jbuilder
 9      # return [String] the json string
10      def to_json(options = {})
11        render(self, options)
12      end
13
14      # Returns the hash representation of the JSON
15      # @return [Hash] the hash representation of the items JSON.
16      def as_json(_options = nil)
17        to_h
18      end
19
20      # Returns the hash representation of the JSON
21      # @return [Hash] the hash representation of the items JSON.
22      def to_h
23        JSON.parse(to_json, symbolize_names: true).with_indifferent_access
24      end
25
26      # Renders the model to JSON.
27      # @param model [Object] the model to render.
28      # @param options [Hash] the hash of options to pass to jbuilder.
29      # @return [String] the JSON.
30      def render(model, options)
31        Template.new(model).to_json(options)
32      end
33
34      # Returns the file name of the jbuilder template.
35      # @return [String] name of the jbuilder template.
36      def template_name
37        "#{self.class.name.split('::').last.underscore}.json.jbuilder"
38      end
39    end
40  end
41end