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.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
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.
17 18 19 |
# File 'lib/grape_serializer.rb', line 17 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 |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/grape_serializer.rb', line 15 def @options 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
21 22 23 24 25 |
# File 'lib/grape_serializer.rb', line 21 def represent(resource, opts = {}) @options = opts.merge!(@params) 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, @options).as_json end |