Class: Blueprinter::Base
- Inherits:
-
Object
- Object
- Blueprinter::Base
- Extended by:
- Reflection, Rendering
- 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.default_extractor) {|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.
-
.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.
- .view_collection ⇒ Object
Methods included from Reflection
Methods included from Rendering
hashify, prepare, render, render_as_hash, render_as_json
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.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/blueprinter/base.rb', line 168 def association(method, = {}, &block) raise ArgumentError, ':blueprint must be provided when defining an association' unless [:blueprint] method = method.to_sym current_view << Association.new( method:, name: .fetch(:name) { method }, extractor: .fetch(:extractor) { association_extractor }, blueprint: .fetch(:blueprint), parent_blueprint: self, view: .fetch(:view, :default), options: .except(:name, :extractor, :blueprint, :view).merge(block:) ) end |
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
300 301 302 |
# File 'lib/blueprinter/base.rb', line 300 def 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
322 323 324 |
# File 'lib/blueprinter/base.rb', line 322 def 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.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/blueprinter/base.rb', line 113 def field(method, = {}, &block) method = method.to_sym current_view << Field.new( method, .fetch(:name) { method }, .fetch(:extractor) { Blueprinter.configuration.default_extractor }, self, .merge(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.
196 197 198 199 200 |
# File 'lib/blueprinter/base.rb', line 196 def fields(*field_names) field_names.each do |field_name| field(field_name) end end |
.identifier(method, name: method, extractor: Blueprinter.configuration.default_extractor) {|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.
48 49 50 51 52 53 54 55 56 |
# File 'lib/blueprinter/base.rb', line 48 def identifier(method, name: method, extractor: Blueprinter.configuration.default_extractor, &block) view_collection[:identifier] << Field.new( method, name, extractor, self, block: ) end |
.include_view(view_name) ⇒ Array<Symbol>
Specify another view that should be mixed into the current view.
255 256 257 |
# File 'lib/blueprinter/base.rb', line 255 def 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.
280 281 282 |
# File 'lib/blueprinter/base.rb', line 280 def include_views(*view_names) current_view.include_views(view_names) end |
.transform(transformer) ⇒ Array<Class>
Specify one transformer to be included for serialization. Takes a class which extends Blueprinter::Transformer
233 234 235 |
# File 'lib/blueprinter/base.rb', line 233 def 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.
341 342 343 344 345 346 347 |
# File 'lib/blueprinter/base.rb', line 341 def view(view_name) self.view_scope = view_collection[view_name] view_collection[:default].track_definition_order(view_name) yield view_collection.invalidate_cache! self.view_scope = 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.
366 367 368 |
# File 'lib/blueprinter/base.rb', line 366 def view?(view_name) view_collection.view?(view_name) end |
.view_collection ⇒ Object
370 371 372 |
# File 'lib/blueprinter/base.rb', line 370 def view_collection @_view_collection ||= ViewCollection.new end |