Sha256: 4623fabe97e124c793313971d384c88d57d0cf580c0a163ee4d5129b93c01e43

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

module Graphiti
  module SerializableHash
    def to_hash(fields: nil, include: {})
      {}.tap do |hash|
        _fields = fields[jsonapi_type] if fields
        attrs = requested_attributes(_fields).each_with_object({}) do |(k, v), h|
          h[k] = instance_eval(&v)
        end
        rels = @_relationships.select { |k,v| !!include[k] }
        rels.each_with_object({}) do |(k, v), h|
          serializers = v.send(:resources)
          attrs[k] = if serializers.is_a?(Array)
              serializers.map do |rr| # use private method to avoid array casting
                rr.to_hash(fields: fields, include: include[k])
              end
            elsif serializers.nil?
              nil
            else
              serializers.to_hash(fields: fields, include: include[k])
            end
        end

        hash[:id] = jsonapi_id
        hash.merge!(attrs) if attrs.any?
      end
    end
  end
  JSONAPI::Serializable::Resource.send(:include, SerializableHash)

  class HashRenderer
    def initialize(resource)
      @resource = resource
    end

    def render(options)
      serializers = options[:data]
      opts = options.slice(:fields, :include)
      to_hash(serializers, opts).tap do |hash|
        hash.merge!(options.slice(:meta)) if !options[:meta].empty?
      end
    end

    private

    def to_hash(serializers, opts)
      {}.tap do |hash|
        if serializers.is_a?(Array)
          hash[@resource.type] = serializers.map do |s|
            s.to_hash(opts)
          end
        else
          hash[@resource.type] = serializers.to_hash(opts)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
graphiti-1.0.alpha.8 lib/graphiti/hash_renderer.rb
graphiti-1.0.alpha.7 lib/graphiti/hash_renderer.rb
graphiti-1.0.alpha.6 lib/graphiti/hash_renderer.rb
graphiti-1.0.alpha.5 lib/graphiti/hash_renderer.rb
graphiti-1.0.alpha.4 lib/graphiti/hash_renderer.rb
graphiti-1.0.alpha.1 lib/graphiti/hash_renderer.rb
graphiti-rb-1.0.alpha.1 lib/graphiti/hash_renderer.rb