-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jordan Hollinger <[email protected]>
- Loading branch information
1 parent
25f7964
commit 0b5e538
Showing
42 changed files
with
2,823 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'blueprinter/v2/extensions' | ||
require 'blueprinter/v2/base' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
module V2 | ||
# | ||
# Base class for extensions. | ||
# | ||
# Hook call order: | ||
# - collection? (skipped with render_object/render_collection) | ||
# - input_object | input_collection | ||
# - each_input | ||
# - field_value | ||
# - formatters | ||
# - exclude_field? | ||
# - object_value | ||
# - exclude_object? | ||
# - collection_value | ||
# - exclude_collection? | ||
# - each_output | ||
# - output_object | output_collection | ||
# | ||
class Extension | ||
class << self | ||
attr_accessor :formatters | ||
end | ||
|
||
def self.inherited(ext) | ||
ext.formatters = {} | ||
end | ||
|
||
# | ||
# Add a formatter for instances of the given class. | ||
# | ||
# Example: | ||
# class MyExtension < Blueprinter::V2::Extension | ||
# format(Time) { |context| context.value.iso8601 } | ||
# format Date, :date_str | ||
# | ||
# def date_str(context) | ||
# context.value.iso8601 | ||
# end | ||
# end | ||
# | ||
# @param klass [Class] The class of objects to format | ||
# @param formatter_method [Symbol] Name of a public instance method to call for formatting | ||
# @yield Do formatting in the block instead | ||
# | ||
def self.format(klass, formatter_method = nil, &formatter_block) | ||
formatters[klass] = formatter_method || formatter_block | ||
end | ||
|
||
# | ||
# Returns true if the given object should be considered a collection. | ||
# | ||
# @param [Object] | ||
# @return [Boolean] | ||
# | ||
def collection?(_object) | ||
false | ||
end | ||
|
||
# | ||
# Modify or replace the object passed to render/render_object. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def input_object(context) | ||
context.object | ||
end | ||
|
||
|
||
# | ||
# Modify or replace the collection passed to render/render_collection. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def input_collection(context) | ||
context.object | ||
end | ||
|
||
# | ||
# Modify or replace the object result before final render (e.g. to JSON). | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def output_object(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Modify or replace the collection result before final render (e.g. to JSON). | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def output_collection(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Modify or replace the object that's passed into each Blueprint. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def each_input(context) | ||
context.object | ||
end | ||
|
||
# | ||
# Modify or replace the output from each Blueprint. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def each_output(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Modify or replace the value used for the field. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def field_value(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Modify or replace the value used for the object. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def object_value(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Modify or replace the value used for the collection. | ||
# | ||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
# | ||
def collection_value(context) | ||
context.value | ||
end | ||
|
||
# | ||
# Return true to exclude this field from the result. | ||
# | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
# | ||
def exclude_field?(_context) | ||
false | ||
end | ||
|
||
# | ||
# Return true to exclude this object from the result. | ||
# | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
# | ||
def exclude_object?(_context) | ||
false | ||
end | ||
|
||
# | ||
# Return true to exclude this collection from the result. | ||
# | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
# | ||
def exclude_collection?(_context) | ||
false | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
module V2 | ||
module Extensions | ||
autoload :DefaultValues, 'blueprinter/v2/extensions/default_values' | ||
autoload :ExcludeIfEmpty, 'blueprinter/v2/extensions/exclude_if_empty' | ||
autoload :ExcludeIfNil, 'blueprinter/v2/extensions/exclude_if_nil' | ||
autoload :Extraction, 'blueprinter/v2/extensions/extraction' | ||
autoload :FieldSorter, 'blueprinter/v2/extensions/field_sorter' | ||
autoload :IfConditionals, 'blueprinter/v2/extensions/if_conditionals' | ||
autoload :NestedBlueprints, 'blueprinter/v2/extensions/nested_blueprints' | ||
autoload :RootElement, 'blueprinter/v2/extensions/root_element' | ||
autoload :UnlessConditionals, 'blueprinter/v2/extensions/unless_conditionals' | ||
end | ||
end | ||
end |
Oops, something went wrong.