Class: GrapeSerializer
- Inherits:
-
Object
- Object
- GrapeSerializer
- Defined in:
- lib/grape_serializer.rb
Class Attribute Summary collapse
-
.entity_class ⇒ Object
readonly
Returns the value of attribute entity_class.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ GrapeSerializer
constructor
A new instance of GrapeSerializer.
- #represent(resource, opts = {}) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ GrapeSerializer
Returns a new instance of GrapeSerializer.
15 16 17 |
# File 'lib/grape_serializer.rb', line 15 def initialize(params = {}) @params = params end |
Class Attribute Details
.entity_class ⇒ Object (readonly)
Returns the value of attribute entity_class.
8 9 10 |
# File 'lib/grape_serializer.rb', line 8 def entity_class @entity_class end |
Class Method Details
.entity(entity_class) ⇒ Object
10 11 12 |
# File 'lib/grape_serializer.rb', line 10 def entity(entity_class) @entity_class ||= entity_class end |
Instance Method Details
#represent(resource, opts = {}) ⇒ Object
19 20 21 22 |
# File 'lib/grape_serializer.rb', line 19 def represent(resource, opts = {}) preload_associations(resource) if defined?(::Rails::Engine) && defined?(::ActiveRecord) && (resource.is_a?(ActiveRecord::Base) || resource.is_a?(ActiveRecord::Relation)) self.class.entity_class.represent(resource, opts.merge(@params)).as_json end |