Sha256: 4dcf36fddbcc67da7c64a81deecba780b4ef55ffbf4346cf4fc51ed55fe500f9
Contents?: true
Size: 1.39 KB
Versions: 1
Compression:
Stored size: 1.39 KB
Contents
module RestPack::Serializer class Result attr_accessor :resources, :meta, :links def initialize @resources = {} @meta = {} @links = {} end def serialize result = {} result[:meta] = @meta unless @meta.empty? result[:links] = @links unless @links.empty? unless @resources.empty? inject_to_many_links! result[@resources.keys.first] = @resources.values.first linked = @resources.except(@resources.keys.first) result[:linked] = linked unless linked.empty? end result end private def inject_to_many_links! #find and inject to_many links from related @resources @resources.keys.each do |key| @resources[key].each do |item| if item[:links] item[:links].each do |link_key, link_value| unless link_value.is_a? Array plural_linked_key = "#{link_key}s".to_sym if @resources[plural_linked_key] linked_resource = @resources[plural_linked_key].find { |i| i[:id] == link_value } if linked_resource linked_resource[:links] ||= {} linked_resource[:links][key] ||= [] linked_resource[:links][key] << item[:id] end end end end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
restpack_serializer-0.4.10 | lib/restpack_serializer/result.rb |