Sha256: 3acfd0f4929315410269361e6df3f07097ebe4f274e051ff2316ef4d79d72ec8

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

module JsonapiCompliable
  module Util
    # Generate the options we end up passing to {{http://jsonapi-rb.org jsonapi-rb}}
    # @api private
    class RenderOptions
      def self.generate(object, query_hash, overrides = {})
        resolved = object.respond_to?(:resolve) ? object.resolve : object

        fields = query_hash[:fields].dup
        extra_fields = query_hash[:extra_fields]

        # Ensure fields doesnt clobber extra fields
        extra_fields.each do |k,v|
          fields[k] = fields[k] + v if fields[k]
        end

        options            = {}
        options[:class]    = inferrer
        options[:include]  = query_hash[:include]
        options[:jsonapi]  = resolved
        options[:fields]   = fields
        options.merge!(overrides)
        options[:meta]   ||= {}
        options[:expose] ||= {}
        options[:expose][:extra_fields] = extra_fields

        if object.respond_to?(:resolve_stats)
          stats = object.resolve_stats
          options[:meta][:stats] = stats unless stats.empty?
        end

        options
      end

      def self.inferrer
        ::Hash.new do |h, k|
          names = k.to_s.split('::')
          klass = names.pop
          serializer_name = [*names, "Serializable#{klass}"].join('::')
          serializer = serializer_name.safe_constantize
          if serializer
            h[k] = serializer
          else
            raise Errors::MissingSerializer.new(k, serializer_name)
          end
        end
      end
      private_class_method :inferrer
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jsonapi_compliable-0.11.34 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.33 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.32 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.31 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.30 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.29 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.28 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.27 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.26 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.25 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.24 lib/jsonapi_compliable/util/render_options.rb
jsonapi_compliable-0.11.23 lib/jsonapi_compliable/util/render_options.rb