Module: Scim::Kit::Templatable

Overview

Implement methods necessary to generate json from jbuilder templates.

Instance Method Summary collapse

Instance Method Details

#as_json(_options = nil) ⇒ Hash

Returns the hash representation of the JSON

Returns:

  • (Hash)

    the hash representation of the items JSON.



16
17
18
# File 'lib/scim/kit/templatable.rb', line 16

def as_json(_options = nil)
  to_h
end

#render(model, options) ⇒ String

Renders the model to JSON.

Parameters:

  • model (Object)

    the model to render.

  • options (Hash)

    the hash of options to pass to jbuilder.

Returns:

  • (String)

    the JSON.



30
31
32
# File 'lib/scim/kit/templatable.rb', line 30

def render(model, options)
  Template.new(model).to_json(options)
end

#template_nameString

Returns the file name of the jbuilder template.

Returns:

  • (String)

    name of the jbuilder template.



36
37
38
# File 'lib/scim/kit/templatable.rb', line 36

def template_name
  "#{self.class.name.split('::').last.underscore}.json.jbuilder"
end

#to_hHash

Returns the hash representation of the JSON

Returns:

  • (Hash)

    the hash representation of the items JSON.



22
23
24
# File 'lib/scim/kit/templatable.rb', line 22

def to_h
  JSON.parse(to_json, symbolize_names: true).with_indifferent_access
end

#to_json(options = {}) ⇒ Object

Returns the JSON representation of the item. return [String] the json string

Parameters:

  • options (Hash) (defaults to: {})

    the hash of options to forward to jbuilder



10
11
12
# File 'lib/scim/kit/templatable.rb', line 10

def to_json(options = {})
  render(self, options)
end