Class: SimpleJsonApi::Serializer
- Inherits:
-
Object
- Object
- SimpleJsonApi::Serializer
- Extended by:
- Forwardable, DSL
- Defined in:
- lib/simple_json_api/serializer.rb
Overview
The Serializer will serialize a model
Direct Known Subclasses
Class Attribute Summary collapse
-
.registered_serializers ⇒ Object
readonly
Returns the value of attribute registered_serializers.
Instance Attribute Summary collapse
-
#_builder ⇒ Object
readonly
Returns the value of attribute _builder.
-
#_each_serializer ⇒ Object
readonly
Returns the value of attribute _each_serializer.
-
#_object ⇒ Object
readonly
Returns the value of attribute _object.
Attributes included from DSL
#_associations, #_attributes, #_root_name
Class Method Summary collapse
- .for(model, association) ⇒ Object
- .for_polymorphic(model) ⇒ Object
- .for_regular(association) ⇒ Object
- .register_serializer(serializer_hash, options) ⇒ Object
Instance Method Summary collapse
-
#initialize(object, builder, each_serializer = nil, base = nil) ⇒ Serializer
constructor
A new instance of Serializer.
Methods included from DSL
attribute, belongs_to, default_fields, has_many, has_one, inherited, serializes
Constructor Details
#initialize(object, builder, each_serializer = nil, base = nil) ⇒ Serializer
Returns a new instance of Serializer.
12 13 14 15 16 17 |
# File 'lib/simple_json_api/serializer.rb', line 12 def initialize(object, builder, each_serializer = nil, base = nil) @_object = object @_builder = builder @_each_serializer = each_serializer @_base = base end |
Class Attribute Details
.registered_serializers ⇒ Object (readonly)
Returns the value of attribute registered_serializers.
20 21 22 |
# File 'lib/simple_json_api/serializer.rb', line 20 def registered_serializers @registered_serializers end |
Instance Attribute Details
#_builder ⇒ Object (readonly)
Returns the value of attribute _builder.
8 9 10 |
# File 'lib/simple_json_api/serializer.rb', line 8 def _builder @_builder end |
#_each_serializer ⇒ Object (readonly)
Returns the value of attribute _each_serializer.
9 10 11 |
# File 'lib/simple_json_api/serializer.rb', line 9 def _each_serializer @_each_serializer end |
#_object ⇒ Object (readonly)
Returns the value of attribute _object.
10 11 12 |
# File 'lib/simple_json_api/serializer.rb', line 10 def _object @_object end |
Class Method Details
.for(model, association) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/simple_json_api/serializer.rb', line 28 def for(model, association) if association[:polymorphic] for_polymorphic(model) else for_regular(association) end end |
.for_polymorphic(model) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/simple_json_api/serializer.rb', line 44 def for_polymorphic(model) serializer = @registered_serializers.find do |entry| entry[:model] == model.class end serializer[:serializer] end |
.for_regular(association) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/simple_json_api/serializer.rb', line 36 def for_regular(association) resource = association[:name].to_s.pluralize.to_sym serializer = @registered_serializers.find do |entry| entry[:resource] == resource end serializer[:serializer] end |
.register_serializer(serializer_hash, options) ⇒ Object
22 23 24 25 26 |
# File 'lib/simple_json_api/serializer.rb', line 22 def register_serializer(serializer_hash, ) .reverse_merge!(primary_key: :id) @registered_serializers ||= [] @registered_serializers << serializer_hash.merge() end |