What is Blueprinter
mainviews, which allow you to predefine different output formats for the same data depending on the context.repository·main·Indexed 23 days ago
https://github.com/procore-oss/blueprinterA high-performance JSON Object Presenter for Ruby that serializes business objects into JSON using a view-based approach. Designed as a performant alternative to JBuilder and ActiveModelSerializers, Blueprinter allows developers to define blueprints with fields, associations, and multiple views to transform Ruby objects into hashes or JSON strings.
views, which allow you to predefine different output formats for the same data depending on the context.Transformers allow you to process the resulting hash of a view before it is serialized. Create a class inheriting from Blueprinter::Transformer and implement the transform(hash, object, options) method. Transformers can be applied globally, per-blueprint, or per-view.
class DynamicFieldTransformer < Blueprinter::Transformer
def transform(hash, object, _options)
hash.merge!(object.dynamic_fields)
end
end
class UserBlueprint < Blueprinter::Base
fields :first_name, :last_name
transform DynamicFieldTransformer
endThe extension system allows you to intercept the rendering process. Currently, the pre_render hook is available, which allows you to modify or replace the object before serialization begins. Extensions are executed in the order they are added to Blueprinter.configure.extensions.
class ObfuscateNameExtension < Blueprinter::Extension
def pre_render(object, blueprint, view, options)
return object unless object.respond_to?(:name)
modified_object = object.dup
modified_object.name = ObsfuscateName.call(modified_object.name)
modified_object
end
end
Blueprinter.configure do |config|
config.extensions << ObfuscateNameExtension.new
endViews allow you to define different output shapes for the same blueprint. You can use view :name do ... end to define a view and include_view :other_view to compose views by inheriting fields from another.
class UserBlueprint < Blueprinter::Base
identifier :uuid
field :email, name: :login
view :normal do
fields :first_name, :last_name
end
view :extended do
include_view :normal
field :address
association :projects
end
end
# Usage
puts UserBlueprint.render(user, view: :extended)Associations allow you to include related objects. You can specify the blueprint to use for the association and pass static options or a Proc to derive options from the parent object at runtime.
class DriverBlueprint < Blueprinter::Base
identifier :uuid
view :normal do
fields :first_name, :last_name
# Passing options via a Proc to derive them from the parent object
association :vehicles,
blueprint: VehicleBlueprint,
options: ->(driver) { { trim: driver.preferred_trim } }
end
endAn identifier specifies the field or method used as the object's unique ID. Identifiers have two unique properties:
:identifier).If you do not want this behavior, define the ID as a regular field instead.
class UserBlueprint < Blueprinter::Base
identifier :uuid
endTo serialize an object, create a class inheriting from Blueprinter::Base. Use identifier to specify the unique ID field and fields to list the attributes to be included. Call .render(object) on the blueprint class to get a JSON string.
class UserBlueprint < Blueprinter::Base
identifier :uuid
fields :first_name, :last_name, :email
end
puts UserBlueprint.render(user) # Output is a JSON stringTo install Blueprinter, add it to your application's Gemfile:
gem 'blueprinter'Then run bundle in your terminal. Alternatively, you can install it directly using gem install blueprinter.
Note: If you are not using Rails or the Oj gem, ensure you have require 'json' in your project.
AssociationExtractor supports providing fallback values for associations. You can use the :default or :default_if keys within the association options. If the extracted value meets the condition specified by :default_if, the extractor will return the value specified in :default. If :default is not explicitly provided, it falls back to the global Blueprinter.configuration.association_default.You can control how Blueprinter reports deprecated functionality using the deprecations configuration key. Options are:
:stderr (Default): Writes to stderr.:raise: Raises a Blueprinter::BlueprinterError.:silence: Silences all deprecation notices.Blueprinter.configure do |config|
config.deprecations = :raise
endBy default, Blueprinter sorts JSON keys alphabetically. To preserve the order in which fields are defined in your blueprint, configure sort_fields_by = :definition.
Blueprinter.configure do |config|
config.sort_fields_by = :definition
endTo use yajl-ruby instead of the default JSON or Oj generators, configure the generator and the method used for encoding.
_Note: If you are using yajl-ruby via its JSON compatibility API (require 'yajl/json_gem'), JSON.generate is already patched to use Yajl::Encoder.encode, so manual configuration may not be necessary.
require 'yajl' # you can skip this if yajl has already been required.
Blueprinter.configure do |config|
config.generator = Yajl::Encoder # default is JSON
config.method = :encode # default is generate
end