-
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.
- Loading branch information
1 parent
25f7964
commit 8361699
Showing
21 changed files
with
743 additions
and
6 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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'blueprinter/v2/base' | ||
|
||
module Blueprinter | ||
module V2 | ||
module Extensions | ||
autoload :ConditionalFields, 'blueprinter/v2/extensions/conditional_fields' | ||
autoload :DefaultValues, 'blueprinter/v2/extensions/default_values' | ||
autoload :ExcludeIfNil, 'blueprinter/v2/extensions/exclude_if_nil' | ||
|
||
DEFAULT = [ | ||
ConditionalFields, | ||
DefaultValues, | ||
ExcludeIfNil | ||
].freeze | ||
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ module V2 | |
:legacy_view, | ||
:from, | ||
:value_proc, | ||
:extractor, | ||
:options, | ||
keyword_init: true | ||
) | ||
|
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,85 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
module V2 | ||
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 | ||
|
||
# Returning true will exclude this field and value from the results | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
def exclude_field?(_context) | ||
false | ||
end | ||
|
||
# Returning true will exclude this association and value from the results | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
def exclude_object?(_context) | ||
false | ||
end | ||
|
||
# Returning true will exclude this association and value from the results | ||
# @param _context [Blueprinter::V2::Serializer::Context] | ||
# @return [Boolean] | ||
def exclude_collection?(_context) | ||
false | ||
end | ||
|
||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
def field_value(context) | ||
context.value | ||
end | ||
|
||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
def object_value(context) | ||
context.value | ||
end | ||
|
||
# @param context [Blueprinter::V2::Serializer::Context] | ||
# @return [Object] | ||
def collection_value(context) | ||
context.value | ||
end | ||
|
||
# Modify or replace the object passed to render/render_object/render_collection | ||
def input(_blueprint, object, _options) | ||
object | ||
end | ||
|
||
# Modify or replace the result before final render (e.g. to JSON) | ||
def output(_blueprint, result, _options) | ||
result | ||
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'blueprinter/v2/extension' | ||
|
||
module Blueprinter | ||
module V2 | ||
module Extensions | ||
class ConditionalFields < ::Blueprinter::V2::Extension | ||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_field?(ctx) | ||
if (cond = ctx.options[:field_if] || ctx.field.options[:if] || ctx.blueprint.class.options[:field_if]) | ||
ctx.blueprint.instance_exec(ctx, &cond) | ||
elsif (cond = ctx.options[:field_unless] || ctx.field.options[:unless] || ctx.blueprint.class.options[:field_unless]) | ||
!ctx.blueprint.instance_exec(ctx, &cond) | ||
else | ||
false | ||
end | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_object?(ctx) | ||
if (cond = ctx.options[:object_if] || ctx.field.options[:if] || ctx.blueprint.class.options[:object_if]) | ||
ctx.blueprint.instance_exec(ctx, &cond) | ||
elsif (cond = ctx.options[:object_unless] || ctx.field.options[:unless] || ctx.blueprint.class.options[:object_unless]) | ||
!ctx.blueprint.instance_exec(ctx, &cond) | ||
else | ||
false | ||
end | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_collection?(ctx) | ||
if (cond = ctx.options[:collection_if] || ctx.field.options[:if] || ctx.blueprint.class.options[:collection_if]) | ||
ctx.blueprint.instance_exec(ctx, &cond) | ||
elsif (cond = ctx.options[:collection_unless] || ctx.field.options[:unless] || ctx.blueprint.class.options[:collection_unless]) | ||
!ctx.blueprint.instance_exec(ctx, &cond) | ||
else | ||
false | ||
end | ||
end | ||
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'blueprinter/v2/extension' | ||
|
||
module Blueprinter | ||
module V2 | ||
module Extensions | ||
class DefaultValues < ::Blueprinter::V2::Extension | ||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def field_value(ctx) | ||
default_if = ctx.options[:field_default_if] || ctx.field.options[:default_if] || ctx.blueprint.class.options[:field_default_if] | ||
return ctx.value unless ctx.value.nil? || default_if&.call(ctx.value) | ||
|
||
ctx.options[:field_default] || ctx.field.options[:default] || ctx.blueprint.class.options[:field_default] | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def object_value(ctx) | ||
default_if = ctx.options[:object_default_if] || ctx.field.options[:default_if] || ctx.blueprint.class.options[:object_default_if] | ||
return ctx.value unless ctx.value.nil? || default_if&.call(ctx.value) | ||
|
||
ctx.options[:object_default] || ctx.field.options[:default] || ctx.blueprint.class.options[:object_default] | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def collection_value(ctx) | ||
default_if = ctx.options[:collection_default_if] || ctx.field.options[:default_if] || ctx.blueprint.class.options[:collection_default_if] | ||
return ctx.value unless ctx.value.nil? || default_if&.call(ctx.value) | ||
|
||
ctx.options[:collection_default] || ctx.field.options[:default] || ctx.blueprint.class.options[:collection_default] | ||
end | ||
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'blueprinter/v2/extension' | ||
|
||
module Blueprinter | ||
module V2 | ||
module Extensions | ||
class ExcludeIfNil < Extension | ||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_field?(ctx) | ||
ctx.value.nil? && (ctx.options[:exclude_if_nil] || ctx.field.options[:exclude_if_nil] || ctx.blueprint.class.options[:exclude_if_nil]) | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_object?(...) | ||
exclude_field?(...) | ||
end | ||
|
||
# @param ctx [Blueprinter::V2::Serializer::Context] | ||
def exclude_collection?(...) | ||
exclude_field?(...) | ||
end | ||
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blueprinter | ||
module V2 | ||
# The default extractor, and the suggested base class for custom extractors | ||
class Extractor | ||
def field(blueprint_instance, field, object, options) | ||
if field.value_proc | ||
blueprint_instance.instance_exec(object, options, &field.value_proc) | ||
elsif object.is_a? Hash | ||
object[field.from] | ||
else | ||
object.public_send(field.from) | ||
end | ||
end | ||
|
||
def object(blueprint_instance, field, object, options) | ||
field(blueprint_instance, field, object, options) | ||
end | ||
|
||
def collection(blueprint_instance, field, object, options) | ||
field(blueprint_instance, field, object, options) | ||
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ module V2 | |
:name, | ||
:from, | ||
:value_proc, | ||
:extractor, | ||
:options, | ||
keyword_init: true | ||
) | ||
|
Oops, something went wrong.