Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental V2 renderer #479

Draft
wants to merge 21 commits into
base: release-2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ Rake::TestTask.new(:benchmarks) do |t|
t.verbose = false
end

Rake::TestTask.new(:speedtest) do |t|
t.libs.append('lib', 'spec')
t.pattern = 'spec/benchmarks/speedtest.rb'
t.verbose = false
end

task default: %i[spec rubocop]
3 changes: 3 additions & 0 deletions blueprinter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 3.0'
s.metadata['rubygems_mfa_required'] = 'true'

s.add_development_dependency 'multi_json', '~> 1.0'
s.add_development_dependency 'opentelemetry-sdk', '~> 1.0'
end
4 changes: 4 additions & 0 deletions lib/blueprinter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

module Blueprinter
autoload :Base, 'blueprinter/base'
autoload :Blueprint, 'blueprinter/blueprint'
autoload :BlueprinterError, 'blueprinter/blueprinter_error'
autoload :Configuration, 'blueprinter/configuration'
autoload :Errors, 'blueprinter/errors'
autoload :Extension, 'blueprinter/extension'
autoload :Extensions, 'blueprinter/extensions'
autoload :Extractor, 'blueprinter/extractor'
autoload :Hooks, 'blueprinter/hooks'
autoload :Transformer, 'blueprinter/transformer'
autoload :V2, 'blueprinter/v2'

Expand Down
12 changes: 11 additions & 1 deletion lib/blueprinter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'blueprinter/field'
require 'blueprinter/helpers/base_helpers'
require 'blueprinter/reflection'
require 'blueprinter/view_wrapper'

module Blueprinter
class Base
Expand Down Expand Up @@ -246,7 +247,7 @@ def self.render_as_json(object, options = {})
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)
object = Blueprinter.configuration.hooks.reduce(:pre_render, object) { |val| [val, self, view_name, local_options] }
data = prepare_data(object, view_name, local_options)
prepend_root_and_meta(data, root, meta)
end
Expand Down Expand Up @@ -438,5 +439,14 @@ def self.view(view_name)
def self.view?(view_name)
view_collection.view? view_name
end

# For compatibility with V2
#
# @return [Blueprinter::ViewWrapper]
def self.[](view_name)
raise Errors::UnknownView, "View '#{view_name}' could not be found in Blueprint '#{self.name}'" unless view? view_name

ViewWrapper.new(self, view_name)
end
end
end
154 changes: 154 additions & 0 deletions lib/blueprinter/blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# frozen_string_literal: true

require 'blueprinter/v2/render'
require 'blueprinter/v2/serializer'

module Blueprinter
# Base class for V2 Blueprints
class Blueprint
extend V2::DSL
extend V2::Reflection

class << self
# @return [Hash] Options set on this Blueprint
attr_accessor :options
# @return [Array<Blueprinter::Extension>] Extensions set on this Blueprint
attr_accessor :extensions
# @return [Symbol] The name of this view, e.g. :default, :"foo.bar"
attr_accessor :view_name
# @return [String] The fully-qualified name, e.g. "MyBlueprint", or "MyBlueprint.foo.bar"
attr_accessor :blueprint_name
# @api private
attr_accessor :views, :schema, :excludes, :formatters, :partials, :appended_partials, :eval_mutex
end

self.views = V2::ViewBuilder.new(self)
self.schema = {}
self.excludes = []
self.formatters = {}
self.partials = {}
self.appended_partials = []
self.extensions = []
self.options = {}
self.blueprint_name = name
self.view_name = :default
self.eval_mutex = Mutex.new

# Initialize subclass
def self.inherited(subclass)
subclass.views = V2::ViewBuilder.new(subclass)
subclass.schema = schema.transform_values(&:dup)
subclass.excludes = []
subclass.formatters = formatters.dup
subclass.partials = partials.dup
subclass.appended_partials = []
subclass.extensions = extensions.dup
subclass.options = options.dup
subclass.blueprint_name = subclass.name || blueprint_name
subclass.view_name = :default
subclass.eval_mutex = Mutex.new
end

# A descriptive name for the Blueprint view, e.g. "WidgetBlueprint.extended"
def self.inspect = blueprint_name

# A descriptive name for the Blueprint view, e.g. "WidgetBlueprint.extended"
def self.to_s = blueprint_name

# Set the view name
# @api private
def self.append_name(name)
self.blueprint_name = "#{blueprint_name}.#{name}"
self.view_name = blueprint_name.sub(/^[^.]+\./, '').to_sym
end

#
# Access a child view.
#
# MyBlueprint[:extended]
# MyBlueprint["extended.plus"] or MyBlueprint[:extended][:plus]
#
# @param name [Symbol|String] Name of the view, e.g. :extended, "extended.plus"
# @return [Class] A descendent of Blueprinter::Blueprint
#
def self.[](name)
eval! unless @evaled
child, children = name.to_s.split('.', 2)
view = views[child.to_sym] || raise(Errors::UnknownView, "View '#{child}' could not be found in Blueprint '#{self}'")
children ? view[children] : view
end

def self.render(obj, options = {})
if serializer.hooks.any?(:collection?, obj)
render_collection(obj, options)
else
render_object(obj, options)
end
end

def self.render_object(obj, options = {})
# DEPRECATED
if options[:view]
options = options.dup
return self[options.delete(:view)].render_object(obj, options)
end
V2::Render.new(obj, options, serializer: serializer, collection: false)
end

def self.render_collection(objs, options = {})
# DEPRECATED
if options[:view]
options = options.dup
return self[options.delete(:view)].render_collection(objs, options)
end
V2::Render.new(objs, options, serializer: serializer, collection: true)
end

# @api private
def self.serializer
eval! unless @evaled
@serializer
end

# Apply partials and field exclusions
# @api private
def self.eval!(lock = true)
return if @evaled

if lock
eval_mutex.synchronize { run_eval! unless @evaled }
else
run_eval!
end
end

# @api private
def self.run_eval!
appended_partials.each(&method(:apply_partial!))
excludes.each { |f| schema.delete f }
extensions.freeze
options.freeze
formatters.freeze
schema.freeze
schema.each do |_, f|
f.options&.freeze
f.freeze
end

@serializer = V2::Serializer.new(self)
@evaled = true
end

# @api private
def self.apply_partial!(name)
p = partials[name] || raise(Errors::UnknownPartial, "Partial '#{name}' could not be found in Blueprint '#{self}'")
class_eval(&p)
end

# A descriptive name for the Blueprint view, e.g. "#<WidgetBlueprint.extended>"
def inspect = self.class.to_s

# A descriptive name for the Blueprint view, e.g. "WidgetBlueprint.extended"
def to_s = self.class.to_s
end
end
2 changes: 1 addition & 1 deletion lib/blueprinter/blueprint_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def valid_blueprint?(blueprint)
return true if blueprint.is_a?(Proc)
return false unless blueprint.is_a?(Class)

blueprint <= Blueprinter::Base
blueprint <= Blueprinter::Base || blueprint <= Blueprinter::Blueprint
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'json'
require 'blueprinter/extensions'
require 'blueprinter/hooks'
require 'blueprinter/extractors/auto_extractor'

module Blueprinter
Expand All @@ -27,11 +27,16 @@ def initialize
end

def extensions
@extensions ||= Extensions.new
@extensions ||= []
end

def extensions=(list)
@extensions = Extensions.new(list)
@hooks = nil
@extensions = list
end

def hooks
@hooks ||= Blueprinter::Hooks.new(extensions)
end

def array_like_classes
Expand Down
Loading