@@ -7,49 +7,29 @@ module Blueprinter
77 # V2 hook call order:
88 #
99 # - around_hook (called around any other extension hook)
10- # - blueprint
11- # - blueprint_fields
12- # - blueprint_setup
13- # - around_serialize_object | around_serialize_collection
14- # - object_input | collection_input
15- # - blueprint_input
16- # - extract_value
17- # - field_value | object_field_value | collection_field_value
18- # - exclude_field? | exclude_object_field? | exclude_collection_field?
19- # - blueprint_fields …
20- # - field_result | object_field_result | collection_field_result
21- # - blueprint_output
22- # - object_output | collection_output
23- # - json
10+ # - around_result
11+ # - around_blueprint_init
12+ # - around_serialize_object | around_serialize_collection
13+ # - around_blueprint
14+ # - around_field_value | around_object_value | around_collection_value
15+ # - around_blueprint_init …
2416 #
2517 # V1 hook call order:
2618 # - pre_render
2719 #
2820 class Extension
21+ include V2 ::Helpers
22+
2923 HOOKS = %i[
3024 around_hook
31- blueprint
32- blueprint_fields
33- blueprint_setup
25+ around_result
26+ around_blueprint_init
3427 around_serialize_object
3528 around_serialize_collection
36- object_input
37- collection_input
38- blueprint_input
39- extract_value
40- field_value
41- exclude_field?
42- field_result
43- object_field_value
44- exclude_object_field?
45- object_field_result
46- collection_field_value
47- exclude_collection_field?
48- collection_field_result
49- blueprint_output
50- object_output
51- collection_output
52- json
29+ around_blueprint
30+ around_field_value
31+ around_object_value
32+ around_collection_value
5333 pre_render
5434 ] . freeze
5535
@@ -61,16 +41,23 @@ def self.hooks
6141 # If this returns true, around_hook will not be called when this extension's hooks are run. Used by core extensions.
6242 def hidden? = false
6343
64- # around_serialize_object: Runs around serialization of a Blueprint object. Surrounds the `prepare` through
65- # `blueprint_output` hooks. MUST yield!
44+ # around_result TODO
45+ # @param context [Blueprinter::V2::Context::Result]
46+
47+ # around_serialize_object: Runs around serialization of a Blueprint object.
6648 # @param context [Blueprinter::V2::Context::Object]
6749
68- # around_collection: Runs around serialization of a Blueprint collection. Surrounds the `prepare` through
69- # `blueprint_output` hooks. MUST yield!
50+ # around_serialize_collection: Runs around serialization of a Blueprint collection.
7051 # @param context [Blueprinter::V2::Context::Object]
7152
72- # blueprint: Returns the blueprint class to render with. The context's "fields" field will be empty.
73- # @param context [Blueprinter::V2::Context::Render]
53+ # around_blueprint: Runs around serialization of every Blueprint.
54+ # @param context [Blueprinter::V2::Context::Object]
55+
56+ # around_field_value TODO
57+
58+ # around_object_value TODO
59+
60+ # around_collection_value TODO
7461
7562 # blueprint_fields: Returns the fields that should be included in the correct order. Default is all fields in the order
7663 # in which they were defined.
@@ -83,52 +70,6 @@ def hidden? = false
8370 # and cache them in context.data, so we don't have to recalculate them for every field.
8471 # @param context [Blueprinter::V2::Context::Render]
8572
86- # blueprint_input: Modify or replace an object right before it's serialized by a Blueprint. The returned object will be
87- # used as the input to the Blueprint.
88- # @param context [Blueprinter::V2::Context::Object]
89- # @return [Object]
90-
91- # blueprint_output: Modify or replace the serialized output from any Blueprint. The returned object will be used as the
92- # output of the Blueprint.
93- # @param context [Blueprinter::V2::Context::Result]
94- # @return [Object]
95-
96- # extract_value: Extract a field, objecet, or collection value from an object. The returned value will be run through the
97- # NOTE If there are multiple extract_value hooks, only the last one is called.
98- # field_value, object_fled_value, or collection_fled_value hooks.
99-
100- # field_value: Modify or replace the value used for the field. The returned value will be run through any formatters and
101- # used as the field's value.
102- # @param context [Blueprinter::V2::Context::Field]
103- # @return [Object]
104-
105- # object_field_value: Modify or replace the value used for the object. The returned value will be used as the input for
106- # the object's Blueprint.
107- # @param context [Blueprinter::V2::Context::Field]
108- # @return [Object]
109-
110- # collection_field_value: Modify or replace the value used for the collection. The returned value will be used as the
111- # input for the collection's Blueprint.
112- # @param context [Blueprinter::V2::Context::Field]
113- # @return [Enumerable]
114-
115- # exclude_field?: Return true to exclude this field from the result.
116- # @param context [Blueprinter::V2::Context::Field]
117- # @return [Boolean]
118-
119- # exclude_object_field?: Return true to exclude this object from the result.
120- # @param context [Blueprinter::V2::Context::Field]
121- # @return [Boolean]
122-
123- # exclude_collection_field?: Return true to exclude this collection from the result.
124- # @param context [Blueprinter::V2::Context::Field]
125- # @return [Boolean]
126-
127- # json: Override the default JSON encoder. The returned string will be the JSON output.
128- # NOTE If there are multiple json hooks, only the final one is called.
129- # @param context [Blueprinter::V2::Context::Result]
130- # @return [String]
131-
13273 # around_hook: Instrument extension hook calls. MUST yield!
13374 # @param extension [Blueprinter::Extension] Instance of the extension
13475 # @param hook [Symbol] Name of hook being called
@@ -140,5 +81,13 @@ def hidden? = false
14081 # @param view [Symbol] The blueprint view
14182 # @param options [Hash] Options passed to "render"
14283 # @return [Object] The object to continue rendering
84+
85+ private
86+
87+ # Helper for around_result hooks to declare that a result is "final"
88+ def final ( val ) = V2 ::Context ::Final . new ( val )
89+
90+ # Helper for around_result hooks to check if a previous hook has declared a result "final"
91+ def final? ( val ) = val . is_a? V2 ::Context ::Final
14392 end
14493end
0 commit comments