Class: Blueprinter::Base
- Inherits:
-
Object
- Object
- Blueprinter::Base
- Includes:
- BaseHelpers
- Defined in:
- lib/blueprinter/base.rb
Class Method Summary collapse
-
.association(method, options = {}) {|object, options| ... } ⇒ Field
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.
-
.has_view?(view_name) ⇒ Boolean
Check whether or not a Blueprint supports the supplied view.
-
.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) ⇒ Object
-
.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.
Methods included from BaseHelpers
Class Method Details
.association(method, options = {}) {|object, options| ... } ⇒ Field
Specify an associated object to be included for serialization. Takes a required method and an option.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/blueprinter/base.rb', line 161 def self.association(method, = {}, &block) validate_blueprint!([:blueprint], method) field( method, .merge( association: true, extractor: .fetch(:extractor) { AssociationExtractor.new }, ), &block ) end |
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
388 389 390 |
# File 'lib/blueprinter/base.rb', line 388 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
411 412 413 |
# File 'lib/blueprinter/base.rb', line 411 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/blueprinter/base.rb', line 122 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.
278 279 280 281 282 |
# File 'lib/blueprinter/base.rb', line 278 def self.fields(*field_names) field_names.each do |field_name| field(field_name) end end |
.has_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.has_view?(:custom) => true
ExampleBlueprint.has_view?(:doesnt_exist) => false
supported by this Blueprint.
454 455 456 |
# File 'lib/blueprinter/base.rb', line 454 def self.has_view?(view_name) view_collection.has_view? view_name 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.
57 58 59 60 61 62 63 64 65 |
# File 'lib/blueprinter/base.rb', line 57 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.
339 340 341 |
# File 'lib/blueprinter/base.rb', line 339 def self.include_view(view_name) current_view.include_view(view_name) end |
.include_views(*view_names) ⇒ Object
367 368 369 |
# File 'lib/blueprinter/base.rb', line 367 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
256 257 258 259 260 261 262 263 |
# File 'lib/blueprinter/base.rb', line 256 def self.prepare(object, view_name:, local_options:, root: nil, meta: nil) unless view_collection.has_view? view_name raise BlueprinterError, "View '#{view_name}' is not defined" end 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.
195 196 197 |
# File 'lib/blueprinter/base.rb', line 195 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.
220 221 222 |
# File 'lib/blueprinter/base.rb', line 220 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.
245 246 247 |
# File 'lib/blueprinter/base.rb', line 245 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
316 317 318 |
# File 'lib/blueprinter/base.rb', line 316 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.
430 431 432 433 434 435 |
# File 'lib/blueprinter/base.rb', line 430 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 |