class Blueprinter::Base

def self.association(method, options = {}, &block)

Returns:
  • (Field) - A Field object

Other tags:
    Example: Passing a block to be evaluated as the value. -
    Example: Specifying an association -

Other tags:
    Yield: - The object and the options passed to render are

Options Hash: (**options)
  • :view (Symbol) -- Specify the view to use or fall back to
  • :name (Symbol) -- Use this to rename the association in the
  • :blueprint (Symbol) -- Required. Use this to specify the

Parameters:
  • options (Hash) -- options to overide defaults.
  • method (Symbol) -- the association name
def self.association(method, options = {}, &block)
  validate_blueprint!(options[:blueprint], method)
  field(
    method,
    options.merge(
      association: true,
      extractor: options.fetch(:extractor) { AssociationExtractor.new }
    ),
    &block
  )
end

def self.exclude(field_name)

Returns:
  • (Array) - an array of field names

Other tags:
    Example: Excluding a field from being included into the current view. -

Parameters:
  • field_name (Symbol) -- the field to exclude from the current view.
def self.exclude(field_name)
  current_view.exclude_field(field_name)
end

def self.excludes(*field_names)

def self.excludes(*field_names)
  current_view.exclude_fields(field_names)
end

def self.field(method, options = {}, &block)

Returns:
  • (Field) - A Field object

Other tags:
    Example: Passing an if proc and unless method. -
    Example: Passing a block to be evaluated as the value. -
    Example: Specifying a user's first_name to be serialized. -

Other tags:
    Yield: - The object and the options passed to render are

Options Hash: (**options)
  • :unless (Symbol, Proc) -- Specifies a method, proc or string
  • :if (Symbol, Proc) -- Specifies a method, proc or string to
  • :datetime_format (String, Proc) -- Format Date or DateTime object
  • :name (Symbol) -- Use this to rename the method. Useful if
  • :extractor (AssociationExtractor, BlockExtractor, HashExtractor, PublicSendExtractor) --

Parameters:
  • options (Hash) -- options to overide defaults.
  • method (Symbol) -- the field or method name you want to include for
def self.field(method, options = {}, &block)
  current_view << Field.new(
    method,
    options.fetch(:name) { method },
    options.fetch(:extractor) { Blueprinter.configuration.extractor_default.new },
    self,
    options.merge(block: block)
  )
end

def self.fields(*field_names)

Returns:
  • (Array) - an array of field names

Other tags:
    Example: Specifying a user's first_name and last_name to be serialized. -

Parameters:
  • method (Symbol) -- the field or method name you want to include for
def self.fields(*field_names)
  field_names.each do |field_name|
    field(field_name)
  end
end

def self.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new, &block)

Returns:
  • (Field) - A Field object

Other tags:
    Example: Passing a block to be evaluated as the value. -
    Example: Specifying a uuid as an identifier. -

Other tags:
    Yield: - The object and the options passed to render are

Parameters:
  • extractor (AssociationExtractor, AutoExtractor, BlockExtractor, HashExtractor, PublicSendExtractor) --
  • name (Symbol) -- to rename the identifier key in the JSON
  • method (Symbol) -- the method or field used as an identifier that you
def self.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new, &block)
  view_collection[:identifier] << Field.new(
    method,
    name,
    extractor,
    self,
    block: block
  )
end

def self.include_view(view_name)

Returns:
  • (Array) - an array of view names.

Other tags:
    Example: Including a normal view into an extended view. -

Parameters:
  • view_name (Symbol) -- the view to mix into the current view.
def self.include_view(view_name)
  current_view.include_view(view_name)
end

def self.include_views(*view_names)

def self.include_views(*view_names)
  current_view.include_views(view_names)
end

def self.prepare(object, view_name:, local_options:, root: nil, meta: nil)

Other tags:
    Api: - private
def self.prepare(object, view_name:, local_options:, root: nil, meta: nil)
  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)
  data = prepare_data(object, view_name, local_options)
  prepend_root_and_meta(data, root, meta)
end

def self.render(object, options = {})

Returns:
  • (String) - JSON formatted String

Other tags:
    Example: Generating JSON with an extended view -

Options Hash: (**options)
  • :meta (Any) -- Defaults to nil.
  • :root (Symbol|String) -- Defaults to nil.
  • :view (Symbol) -- Defaults to :default.

Parameters:
  • options (Hash) -- the options hash which requires a :view. Any
  • object (Object) -- the Object to serialize upon.
def self.render(object, options = {})
  jsonify(prepare_for_render(object, options))
end

def self.render_as_hash(object, options = {})

Returns:
  • (Hash) -

Other tags:
    Example: Generating a hash with an extended view -

Options Hash: (**options)
  • :meta (Any) -- Defaults to nil.
  • :root (Symbol|String) -- Defaults to nil.
  • :view (Symbol) -- Defaults to :default.

Parameters:
  • options (Hash) -- the options hash which requires a :view. Any
  • object (Object) -- the Object to serialize upon.
def self.render_as_hash(object, options = {})
  prepare_for_render(object, options)
end

def self.render_as_json(object, options = {})

Returns:
  • (Hash) -

Other tags:
    Example: Generating a hash with an extended view -

Options Hash: (**options)
  • :meta (Any) -- Defaults to nil.
  • :root (Symbol|String) -- Defaults to nil.
  • :view (Symbol) -- Defaults to :default.

Parameters:
  • options (Hash) -- the options hash which requires a :view. Any
  • object (Object) -- the Object to serialize upon.
def self.render_as_json(object, options = {})
  prepare_for_render(object, options).as_json
end

def self.transform(transformer)

Returns:
  • (Array) - an array of transformers

Other tags:
    Example: Specifying a DynamicFieldTransformer transformer for including dynamic fields to be serialized. -

Parameters:
  • class () -- name [Class] which implements the method transform to include for
def self.transform(transformer)
  current_view.add_transformer(transformer)
end

def self.view(view_name)

Returns:
  • (View) - a Blueprinter::View object

Other tags:
    Example: Using views -

Other tags:
    Yieldreturn: - Use this block to

Parameters:
  • view_name (Symbol) -- the view name
def self.view(view_name)
  @current_view = view_collection[view_name]
  view_collection[:default].track_definition_order(view_name)
  yield
  @current_view = view_collection[:default]
end

def self.view?(view_name)

Returns:
  • (Boolean) - a boolean value indicating if the view is

Other tags:
    Example: With the following Blueprint -

Parameters:
  • view_name (Symbol) -- the view name
def self.view?(view_name)
  view_collection.view? view_name
end