Sha256: caacdb216bfb36ea134ae8e4ac48656d73e57c789ef72f12a70a5404e5c0b624

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module ActiveModel
  module Serializable
    def as_json(options={})
      instrument('!serialize') do
        if root = options.fetch(:root, json_key)
          hash = { root => serializable_object }
          hash.merge!(serializable_data)
          hash
        else
          serializable_object
        end
      end
    end

    def serializable_data
      embedded_in_root_associations.tap do |hash|
        if respond_to?(:meta) && meta
          hash[meta_key] = meta
        end
      end
    end

    def embedded_in_root_associations
      {}
    end

    private
    def instrument(action, &block)
      payload = instrumentation_keys.inject({ serializer: self.class.name }) do |payload, key|
        payload[:payload] = self.instance_variable_get(:"@#{key}")
        payload
      end
      ActiveSupport::Notifications.instrument("#{action}.active_model_serializers", payload, &block)
    end

    def instrumentation_keys
      [:object, :scope, :root, :meta_key, :meta, :wrap_in_array, :only, :except, :key_format]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_serializers-0.9.2 lib/active_model/serializable.rb
active_model_serializers-0.9.0 lib/active_model/serializable.rb