Class: JSONAPI::ResourceSerializer
- Inherits:
-
Object
- Object
- JSONAPI::ResourceSerializer
- Defined in:
- lib/jsonapi/resource_serializer.rb
Instance Attribute Summary collapse
-
#key_formatter ⇒ Object
readonly
Returns the value of attribute key_formatter.
-
#link_builder ⇒ Object
readonly
Returns the value of attribute link_builder.
-
#primary_class_name ⇒ Object
readonly
Returns the value of attribute primary_class_name.
-
#serialization_options ⇒ Object
readonly
Returns the value of attribute serialization_options.
Instance Method Summary collapse
- #format_key(key) ⇒ Object
- #format_value(value, format) ⇒ Object
-
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
constructor
initialize Options can include include: Purpose: determines which objects will be side loaded with the source objects in a linked section Example: [‘comments’,‘author’,‘comments.tags’,‘author.posts’] fields: Purpose: determines which fields are serialized for a resource type.
- #query_link(query_params) ⇒ Object
-
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure.
- #serialize_to_links_hash(source, requested_relationship) ⇒ Object
Constructor Details
#initialize(primary_resource_klass, options = {}) ⇒ ResourceSerializer
initialize Options can include include:
Purpose: determines which objects will be side loaded with the source objects in a linked section
Example: ['comments','author','comments.tags','author.posts']
fields:
Purpose: determines which fields are serialized for a resource type. This encompasses both attributes and
relationship ids in the links section for a resource. Fields are global for a resource type.
Example: { people: [:id, :email, :comments], posts: [:id, :title, :author], comments: [:id, :body, :post]}
key_formatter: KeyFormatter instance to override the default configuration serializer_options: additional options that will be passed to resource meta and links lambdas
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsonapi/resource_serializer.rb', line 18 def initialize(primary_resource_klass, = {}) @primary_resource_klass = primary_resource_klass @primary_class_name = primary_resource_klass._type @fields = .fetch(:fields, {}) @include = .fetch(:include, []) @include_directives = [:include_directives] @key_formatter = .fetch(:key_formatter, JSONAPI.configuration.key_formatter) @id_formatter = ValueFormatter.value_formatter_for(:id) @link_builder = generate_link_builder(primary_resource_klass, ) @always_include_to_one_linkage_data = .fetch(:always_include_to_one_linkage_data, JSONAPI.configuration.always_include_to_one_linkage_data) @always_include_to_many_linkage_data = .fetch(:always_include_to_many_linkage_data, JSONAPI.configuration.always_include_to_many_linkage_data) @serialization_options = .fetch(:serialization_options, {}) # Warning: This makes ResourceSerializer non-thread-safe. That's not a problem with the # request-specific way it's currently used, though. @value_formatter_type_cache = NaiveCache.new{|arg| ValueFormatter.value_formatter_for(arg) } end |
Instance Attribute Details
#key_formatter ⇒ Object (readonly)
Returns the value of attribute key_formatter.
4 5 6 |
# File 'lib/jsonapi/resource_serializer.rb', line 4 def key_formatter @key_formatter end |
#link_builder ⇒ Object (readonly)
Returns the value of attribute link_builder.
4 5 6 |
# File 'lib/jsonapi/resource_serializer.rb', line 4 def link_builder @link_builder end |
#primary_class_name ⇒ Object (readonly)
Returns the value of attribute primary_class_name.
4 5 6 |
# File 'lib/jsonapi/resource_serializer.rb', line 4 def primary_class_name @primary_class_name end |
#serialization_options ⇒ Object (readonly)
Returns the value of attribute serialization_options.
4 5 6 |
# File 'lib/jsonapi/resource_serializer.rb', line 4 def @serialization_options end |
Instance Method Details
#format_key(key) ⇒ Object
87 88 89 |
# File 'lib/jsonapi/resource_serializer.rb', line 87 def format_key(key) @key_formatter.format(key) end |
#format_value(value, format) ⇒ Object
91 92 93 |
# File 'lib/jsonapi/resource_serializer.rb', line 91 def format_value(value, format) @value_formatter_type_cache.get(format).format(value) end |
#query_link(query_params) ⇒ Object
83 84 85 |
# File 'lib/jsonapi/resource_serializer.rb', line 83 def query_link(query_params) link_builder.query_link(query_params) end |
#serialize_to_hash(source) ⇒ Object
Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jsonapi/resource_serializer.rb', line 39 def serialize_to_hash(source) @top_level_sources = Set.new([source].flatten.compact.map {|s| top_level_source_key(s) }) is_resource_collection = source.respond_to?(:to_ary) @included_objects = {} @include_directives ||= JSONAPI::IncludeDirectives.new(@primary_resource_klass, @include) process_primary(source, @include_directives.include_directives) included_objects = [] primary_objects = [] @included_objects.each_value do |objects| objects.each_value do |object| if object[:primary] primary_objects.push(object[:object_hash]) else included_objects.push(object[:object_hash]) end end end primary_hash = { data: is_resource_collection ? primary_objects : primary_objects[0] } primary_hash[:included] = included_objects if included_objects.size > 0 primary_hash end |
#serialize_to_links_hash(source, requested_relationship) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jsonapi/resource_serializer.rb', line 67 def serialize_to_links_hash(source, requested_relationship) if requested_relationship.is_a?(JSONAPI::Relationship::ToOne) data = to_one_linkage(source, requested_relationship) else data = to_many_linkage(source, requested_relationship) end { links: { self: self_link(source, requested_relationship), related: (source, requested_relationship) }, data: data } end |