Class: Blueprinter::Base
- Inherits:
-
Object
- Object
- Blueprinter::Base
- Includes:
- ActiveRecordHelpers
- Defined in:
- lib/blueprinter/base.rb
Class Method Summary collapse
-
.association(method, options = {}) {|Object| ... } ⇒ Field
Specify an associated object to be included for serialization.
- .associations(view_name = :default) ⇒ Object private
-
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
-
.field(method, options = {}) {|Object| ... } ⇒ 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: AutoExtractor.new) ⇒ 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.
- .inherited(subclass) ⇒ Object
-
.prepare(object, view_name:, local_options:) ⇒ 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.
-
.view(view_name) ⇒ View
Specify a view and the fields it should have.
Methods included from ActiveRecordHelpers
#active_record_relation?, included
Class Method Details
.association(method, options = {}) {|Object| ... } ⇒ Field
Specify an associated object to be included for serialization. Takes a required method and an option.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/blueprinter/base.rb', line 140 def self.association(method, = {}, &block) raise BlueprinterError, 'blueprint required' unless [:blueprint] name = .delete(:name) || method = if block_given? .merge(extractor: BlockExtractor.new, block: block) else .merge(extractor: AutoExtractor.new) end current_view << Field.new(method, name, AssociationExtractor.new, self, .merge(association: true)) end |
.associations(view_name = :default) ⇒ 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.
244 245 246 |
# File 'lib/blueprinter/base.rb', line 244 def self.associations(view_name = :default) view_collection.fields_for(view_name).select { |f| f.[:association] } end |
.exclude(field_name) ⇒ Array<Symbol>
Exclude a field that was mixed into the current view.
287 288 289 |
# File 'lib/blueprinter/base.rb', line 287 def self.exclude(field_name) current_view.exclude_field(field_name) end |
.field(method, options = {}) {|Object| ... } ⇒ Field
Specify a field or method name to be included for serialization. Takes a required method and an option.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/blueprinter/base.rb', line 98 def self.field(method, = {}, &block) = if block_given? {name: method, extractor: BlockExtractor.new, block: block} else {name: method, extractor: AutoExtractor.new} end.merge() current_view << Field.new(method, [:name], [:extractor], self, ) 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.
237 238 239 240 241 |
# File 'lib/blueprinter/base.rb', line 237 def self.fields(*field_names) field_names.each do |field_name| field(field_name) end end |
.identifier(method, name: method, extractor: AutoExtractor.new) ⇒ Field
Specify a field or method name used as an identifier. Usually, this is something like :id
Note: identifiers are always rendered and considerered their own view, similar to the :default view.
40 41 42 |
# File 'lib/blueprinter/base.rb', line 40 def self.identifier(method, name: method, extractor: AutoExtractor.new) view_collection[:identifier] << Field.new(method, name, extractor, self) end |
.include_view(view_name) ⇒ Array<Symbol>
Specify another view that should be mixed into the current view.
266 267 268 |
# File 'lib/blueprinter/base.rb', line 266 def self.include_view(view_name) current_view.include_view(view_name) end |
.inherited(subclass) ⇒ Object
44 45 46 |
# File 'lib/blueprinter/base.rb', line 44 def self.inherited(subclass) subclass.send(:view_collection).inherit(view_collection) end |
.prepare(object, view_name:, local_options:) ⇒ 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
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/blueprinter/base.rb', line 206 def self.prepare(object, view_name:, local_options:) unless view_collection.has_view? view_name raise BlueprinterError, "View '#{view_name}' is not defined" end prepared_object = include_associations(object, view_name: view_name) if array_like?(object) prepared_object.map do |obj| object_to_hash(obj, view_name: view_name, local_options: ) end else object_to_hash(prepared_object, view_name: view_name, local_options: ) end end |
.render(object, options = {}) ⇒ String
Generates a JSON formatted String. Takes a required object and an optional view.
173 174 175 176 |
# File 'lib/blueprinter/base.rb', line 173 def self.render(object, = {}) view_name = .delete(:view) || :default jsonify(prepare(object, view_name: view_name, local_options: )) end |
.render_as_hash(object, options = {}) ⇒ Hash
Generates a hash. Takes a required object and an optional view.
194 195 196 197 |
# File 'lib/blueprinter/base.rb', line 194 def self.render_as_hash(object, = {}) view_name = .delete(:view) || :default prepare(object, view_name: view_name, local_options: ) 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.
306 307 308 309 310 |
# File 'lib/blueprinter/base.rb', line 306 def self.view(view_name) @current_view = view_collection[view_name] yield @current_view = view_collection[:default] end |