Module: Blueprinter::Rendering Private

Includes:
TypeHelpers
Included in:
Base
Defined in:
lib/blueprinter/rendering.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Encapsulates the rendering logic for Blueprinter.

Instance Method Summary collapse

Instance Method Details

#hashify(object, view_name:, local_options:) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts an object into a Hash representation based on provided view.

Parameters:

  • object (Object)

    the Object to convert into a Hash.

  • view_name (Symbol)

    the view

  • local_options (Hash)

    the options hash which requires a :view. Any additional key value pairs will be exposed during serialization.

Returns:

  • (Hash)

Raises:



92
93
94
95
96
97
# File 'lib/blueprinter/rendering.rb', line 92

def hashify(object, view_name:, local_options:)
  raise BlueprinterError, "View '#{view_name}' is not defined" unless view_collection.view?(view_name)

  object = Blueprinter.configuration.extensions.pre_render(object, self, view_name, local_options)
  prepare_data(object, view_name, local_options)
end

#prepare(object, view_name:, local_options:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

This method is no longer supported, and was not originally intended to be public. This will be removed in the next minor release. If similar functionality is needed, use ‘.render_as_hash` instead.

This is the magic method that converts complex objects into a simple hash ready for JSON conversion.

Note: we accept view (public interface) that is in reality a view_name, so we rename it for clarity



109
110
111
112
113
114
115
116
117
# File 'lib/blueprinter/rendering.rb', line 109

def prepare(object, view_name:, local_options:)
  Blueprinter::Deprecation.report(
    <<~MESSAGE
      The `prepare` method is no longer supported will be removed in the next minor release.
      If similar functionality is needed, use `.render_as_hash` instead.
    MESSAGE
  )
  render_as_hash(object, view_name:, local_options:)
end

#render(object, options = {}) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a JSON formatted String represantation of the provided object.

Examples:

Generating JSON with an extended view

post = Post.all
Blueprinter::Base.render post, view: :extended
# => "[{\"id\":1,\"title\":\"Hello\"},{\"id\":2,\"title\":\"My Day\"}]"

Parameters:

  • object (Object)

    the Object to serialize.

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

    the options hash which requires a :view. Any additional key value pairs will be exposed during serialization.

Options Hash (options):

  • :view (Symbol)

    Defaults to :default. The view name that corresponds to the group of fields to be serialized.

  • :root (Symbol|String)

    Defaults to nil. Render the json/hash with a root key if provided.

  • :meta (Any)

    Defaults to nil. Render the json/hash with a meta attribute with provided value if both root and meta keys are provided in the options hash.

Returns:

  • (String)

    JSON formatted String



31
32
33
# File 'lib/blueprinter/rendering.rb', line 31

def render(object, options = {})
  jsonify(build_result(object: object, options: options))
end

#render_as_hash(object, options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a Hash representation of the provided object. Takes a required object and an optional view.

Examples:

Generating a hash with an extended view

post = Post.all
Blueprinter::Base.render_as_hash post, view: :extended
# => [{id:1, title: Hello},{id:2, title: My Day}]

Parameters:

  • object (Object)

    the Object to serialize upon.

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

    the options hash which requires a :view. Any additional key value pairs will be exposed during serialization.

Options Hash (options):

  • :view (Symbol)

    Defaults to :default. The view name that corresponds to the group of fields to be serialized.

  • :root (Symbol|String)

    Defaults to nil. Render the json/hash with a root key if provided.

  • :meta (Any)

    Defaults to nil. Render the json/hash with a meta attribute with provided value if both root and meta keys are provided in the options hash.

Returns:

  • (Hash)


56
57
58
# File 'lib/blueprinter/rendering.rb', line 56

def render_as_hash(object, options = {})
  build_result(object: object, options: options)
end

#render_as_json(object, options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a JSONified hash. Takes a required object and an optional view.

Examples:

Generating a hash with an extended view

post = Post.all
Blueprinter::Base.render_as_json post, view: :extended
# => [{"id" => "1", "title" => "Hello"},{"id" => "2", "title" => "My Day"}]

Parameters:

  • object (Object)

    the Object to serialize upon.

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

    the options hash which requires a :view. Any additional key value pairs will be exposed during serialization.

Options Hash (options):

  • :view (Symbol)

    Defaults to :default. The view name that corresponds to the group of fields to be serialized.

  • :root (Symbol|String)

    Defaults to nil. Render the json/hash with a root key if provided.

  • :meta (Any)

    Defaults to nil. Render the json/hash with a meta attribute with provided value if both root and meta keys are provided in the options hash.

Returns:

  • (Hash)


81
82
83
# File 'lib/blueprinter/rendering.rb', line 81

def render_as_json(object, options = {})
  build_result(object: object, options: options).as_json
end