Sha256: 8363f3f35bcf9a8002057c53fd4c9cf5fef1e1fdbd0b988e32fb19341a9a0760

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    # Implement methods necessary to generate json from jbuilder templates.
    module Templatable
      # Returns the JSON representation of the item.
      # @param options [Hash] the hash of options to forward to jbuilder
      # return [String] the json string
      def to_json(options = {})
        render(self, options)
      end

      # Returns the hash representation of the JSON
      # @return [Hash] the hash representation of the items JSON.
      def as_json(_options = nil)
        to_h
      end

      # Returns the hash representation of the JSON
      # @return [Hash] the hash representation of the items JSON.
      def to_h
        JSON.parse(to_json, symbolize_names: true).with_indifferent_access
      end

      # Renders the model to JSON.
      # @param model [Object] the model to render.
      # @param options [Hash] the hash of options to pass to jbuilder.
      # @return [String] the JSON.
      def render(model, options)
        Template.new(model).to_json(options)
      end

      # Returns the file name of the jbuilder template.
      # @return [String] name of the jbuilder template.
      def template_name
        "#{self.class.name.split('::').last.underscore}.json.jbuilder"
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scim-kit-0.7.1 lib/scim/kit/templatable.rb
scim-kit-0.7.0 lib/scim/kit/templatable.rb
scim-kit-0.6.0 lib/scim/kit/templatable.rb
scim-kit-0.5.3 lib/scim/kit/templatable.rb
scim-kit-0.5.2 lib/scim/kit/templatable.rb
scim-kit-0.5.1 lib/scim/kit/templatable.rb
scim-kit-0.5.0 lib/scim/kit/templatable.rb
scim-kit-0.4.0 lib/scim/kit/templatable.rb
scim-kit-0.3.2 lib/scim/kit/templatable.rb
scim-kit-0.3.1 lib/scim/kit/templatable.rb
scim-kit-0.3.0 lib/scim/kit/templatable.rb