Skip to content

Commit cb92e2e

Browse files
committed
Extension hook exception
Signed-off-by: Jordan Hollinger <[email protected]>
1 parent df3c425 commit cb92e2e

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

lib/blueprinter/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Blueprinter
44
module Errors
5+
autoload :ExtensionHook, 'blueprinter/errors/extension_hook'
56
autoload :InvalidBlueprint, 'blueprinter/errors/invalid_blueprint'
67
autoload :UnknownPartial, 'blueprinter/errors/unknown_partial'
78
autoload :UnknownView, 'blueprinter/errors/unknown_view'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module Blueprinter
4+
module Errors
5+
class ExtensionHook < StandardError
6+
attr_reader :extension, :hook, :message
7+
8+
def initialize(extension, hook, message)
9+
@extension = extension
10+
@hook = hook
11+
@message = message
12+
end
13+
end
14+
end
15+
end

lib/blueprinter/hooks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize(extensions)
1010
ext.class.hooks.each { |hook| @hooks[hook] << ext }
1111
end
1212
@hooks.freeze
13+
@reversed_hooks ||= @hooks.transform_values(&:reverse).freeze
1314
@around_hook_registered = registered? :around_hook
1415
end
1516

@@ -120,7 +121,6 @@ def reduce_into(hook, ctx, attr)
120121
#
121122
def around(hook, ctx)
122123
result = nil
123-
@reversed_hooks ||= @hooks.transform_values(&:reverse).freeze
124124
@reversed_hooks.fetch(hook).reduce(-> { result = yield }) do |f, ext|
125125
proc do
126126
yields = 0
@@ -130,7 +130,7 @@ def around(hook, ctx)
130130
end
131131
if yields != 1
132132
msg = "Extension hook '#{ext.class.name}##{hook}' should have yielded 1 time, but yielded #{yields} times"
133-
raise BlueprinterError, msg
133+
raise Errors::ExtensionHook.new(ext, hook, msg)
134134
end
135135
end
136136
end.call

lib/blueprinter/v2/serializer.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def serialize_collection(collection, depth:)
9191
@hooks.reduce_into(:collection_output, ctx, :result)
9292
end
9393

94-
# rubocop:disable Metrics/MethodLength
9594
def serialize(object, depth:)
9695
if @run_blueprint_input
9796
ctx = Context::Object.new(@blueprint, @fields, @options, object, depth)
@@ -105,15 +104,11 @@ def serialize(object, depth:)
105104
ctx.value = ctx.field.value_proc ? proc_value(ctx) : @hooks.call(field_conf.extractor, :extract_value, ctx)
106105
field_conf.serialize(ctx, acc)
107106
end
107+
return result unless @run_blueprint_output
108108

109-
if @run_blueprint_output
110-
ctx = Context::Result.new(@blueprint, @fields, @options, object, result, depth)
111-
@hooks.reduce_into(:blueprint_output, ctx, :result)
112-
else
113-
result
114-
end
109+
ctx = Context::Result.new(@blueprint, @fields, @options, object, result, depth)
110+
@hooks.reduce_into(:blueprint_output, ctx, :result)
115111
end
116-
# rubocop:enable Metrics/MethodLength
117112

118113
# @param ctx [Blueprinter::V2::Context::Field]
119114
def proc_value(ctx)

spec/extensions/hooks_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def around_serialize_object(_ctx); end
299299
end
300300
ctx = object_ctx.new(serializer.blueprint, serializer.fields, {}, object)
301301
hooks = described_class.new [ext.new]
302-
expect { hooks.around(:around_serialize_object, ctx) { 42 } }.to raise_error Blueprinter::BlueprinterError
302+
expect { hooks.around(:around_serialize_object, ctx) { 42 } }.to raise_error Blueprinter::Errors::ExtensionHook
303303
end
304304

305305
it 'raises if a hook yields more than once' do
@@ -311,7 +311,7 @@ def around_serialize_object(_ctx)
311311
end
312312
ctx = object_ctx.new(serializer.blueprint, serializer.fields, {}, object)
313313
hooks = described_class.new [ext.new]
314-
expect { hooks.around(:around_serialize_object, ctx) { 42 } }.to raise_error Blueprinter::BlueprinterError
314+
expect { hooks.around(:around_serialize_object, ctx) { 42 } }.to raise_error Blueprinter::Errors::ExtensionHook
315315
end
316316
end
317317
end

spec/v2/serializer_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
let(:name_of_extractor) do
5353
test = self
5454
Class.new(Blueprinter::V2::Extensions::Core::Extractor) do
55-
def initialize(prefix: 'Name') = @prefix = prefix
55+
def initialize(prefix: 'Name') = @tmp_prefix = prefix
56+
57+
def blueprint_setup(_ctx) = @prefix = @tmp_prefix
5658

5759
def extract_value(ctx)
5860
case ctx.field

0 commit comments

Comments
 (0)