Class: Blueprinter::Base
- Inherits:
-
Object
- Object
- Blueprinter::Base
- Extended by:
- Reflection
- Includes:
- BaseHelpers
- Defined in:
- lib/blueprinter/base.rb
Class Method Summary collapse
-
.association(method, options = {}) {|object, options| ... } ⇒ Association
Specify an associated object to be included for serialization.
-
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
-
.excludes(*field_names) ⇒ Array<Symbol>
When mixing multiple views under a single view, some fields may required to be excluded from current view.
-
.field(method, options = {}) {|object, options| ... } ⇒ Field
Specify a field or method name to be included for serialization.
-
.fields(*field_names) ⇒ Array<Symbol>
Specify one or more field/method names to be included for serialization.
-
.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new) {|object, options| ... } ⇒ Field
Specify a field or method name used as an identifier.
-
.include_view(view_name) ⇒ Array<Symbol>
Specify another view that should be mixed into the current view.
-
.include_views(*view_names) ⇒ Array<Symbol>
Specify additional views that should be mixed into the current view.
-
.prepare(object, view_name:, local_options:, root: nil, meta: nil) ⇒ Object
private
This is the magic method that converts complex objects into a simple hash ready for JSON conversion.
-
.render(object, options = {}) ⇒ String
Generates a JSON formatted String.
-
.render_as_hash(object, options = {}) ⇒ Hash
Generates a hash.
-
.render_as_json(object, options = {}) ⇒ Hash
Generates a JSONified hash.
-
.transform(transformer) ⇒ Array<Class>
Specify one transformer to be included for serialization.
-
.view(view_name) ⇒ View
Specify a view and the fields it should have.
-
.view?(view_name) ⇒ Boolean
Check whether or not a Blueprint supports the supplied view.
Methods included from Reflection
Methods included from BaseHelpers
Class Method Details
.association(method, options = {}) {|object, options| ... } ⇒ Association
Specify an associated object to be included for serialization. Takes a required method and an option.
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/blueprinter/base.rb', line 151 def self.association(method, = {}, &block) raise ArgumentError, ':blueprint must be provided when defining an association' unless [:blueprint] current_view << Association.new( method: method, name: .delete(:name) || method, extractor: .delete(:extractor) || AssociationExtractor.new, blueprint: .delete(:blueprint), view: .delete(:view) || :default, options: .merge(block: block) ) end |
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
372 373 374 |
# File 'lib/blueprinter/base.rb', line 372 def self.exclude(field_name) current_view.exclude_field(field_name) end |
.excludes(*field_names) ⇒ Array<Symbol>
When mixing multiple views under a single view, some fields may required to be excluded from current view
395 396 397 |
# File 'lib/blueprinter/base.rb', line 395 def self.excludes(*field_names) current_view.exclude_fields(field_names) end |
.field(method, options = {}) {|object, options| ... } ⇒ Field
Specify a field or method name to be included for serialization. Takes a required method and an option.
111 112 113 114 115 116 117 118 119 |
# File 'lib/blueprinter/base.rb', line 111 def self.field(method, = {}, &block) current_view << Field.new( method, .fetch(:name) { method }, .fetch(:extractor) { Blueprinter.configuration.extractor_default.new }, self, .merge(block: block) ) end |
.fields(*field_names) ⇒ Array<Symbol>
Specify one or more field/method names to be included for serialization. Takes at least one field or method names.
267 268 269 270 271 |
# File 'lib/blueprinter/base.rb', line 267 def self.fields(*field_names) field_names.each do |field_name| field(field_name) end end |
.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new) {|object, options| ... } ⇒ Field
Specify a field or method name used as an identifier. Usually, this is something like :id
Note: identifiers are always rendered and considered their own view, similar to the :default view.
46 47 48 49 50 51 52 53 54 |
# File 'lib/blueprinter/base.rb', line 46 def self.identifier(method, name: method, extractor: Blueprinter.configuration.extractor_default.new, &block) view_collection[:identifier] << Field.new( method, name, extractor, self, block: block ) end |
.include_view(view_name) ⇒ Array<Symbol>
Specify another view that should be mixed into the current view.
326 327 328 |
# File 'lib/blueprinter/base.rb', line 326 def self.include_view(view_name) current_view.include_view(view_name) end |
.include_views(*view_names) ⇒ Array<Symbol>
Specify additional views that should be mixed into the current view.
@param view_name [Array<Symbol>] the views to mix into the current view.
352 353 354 |
# File 'lib/blueprinter/base.rb', line 352 def self.include_views(*view_names) current_view.include_views(view_names) end |
.prepare(object, view_name:, local_options:, root: nil, meta: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is the magic method that converts complex objects into a simple hash ready for JSON conversion.
Note: we accept view (public interface) that is in reality a view_name, so we rename it for clarity
246 247 248 249 250 251 252 |
# File 'lib/blueprinter/base.rb', line 246 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, ) data = prepare_data(object, view_name, ) (data, root, ) end |
.render(object, options = {}) ⇒ String
Generates a JSON formatted String. Takes a required object and an optional view.
185 186 187 |
# File 'lib/blueprinter/base.rb', line 185 def self.render(object, = {}) jsonify(prepare_for_render(object, )) end |
.render_as_hash(object, options = {}) ⇒ Hash
Generates a hash. Takes a required object and an optional view.
210 211 212 |
# File 'lib/blueprinter/base.rb', line 210 def self.render_as_hash(object, = {}) prepare_for_render(object, ) end |
.render_as_json(object, options = {}) ⇒ Hash
Generates a JSONified hash. Takes a required object and an optional view.
235 236 237 |
# File 'lib/blueprinter/base.rb', line 235 def self.render_as_json(object, = {}) prepare_for_render(object, ).as_json end |
.transform(transformer) ⇒ Array<Class>
Specify one transformer to be included for serialization. Takes a class which extends Blueprinter::Transformer
304 305 306 |
# File 'lib/blueprinter/base.rb', line 304 def self.transform(transformer) current_view.add_transformer(transformer) end |
.view(view_name) ⇒ View
Specify a view and the fields it should have. It accepts a view name and a block. The block should specify the fields.
414 415 416 417 418 419 |
# File 'lib/blueprinter/base.rb', line 414 def self.view(view_name) @current_view = view_collection[view_name] view_collection[:default].track_definition_order(view_name) yield @current_view = view_collection[:default] end |
.view?(view_name) ⇒ Boolean
Check whether or not a Blueprint supports the supplied view. It accepts a view name.
class ExampleBlueprint < Blueprinter::Base
view :custom do
end
end
ExampleBlueprint.view?(:custom) => true
ExampleBlueprint.view?(:doesnt_exist) => false
supported by this Blueprint.
438 439 440 |
# File 'lib/blueprinter/base.rb', line 438 def self.view?(view_name) view_collection.view? view_name end |