Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represented objects via extend are not serializable #3

Open
dblock opened this issue Jul 23, 2014 · 0 comments
Open

Represented objects via extend are not serializable #3

dblock opened this issue Jul 23, 2014 · 0 comments

Comments

@dblock
Copy link
Member

dblock commented Jul 23, 2014

If you put present instance, with: Presenter inside a cache block, it cannot be serialized. That is because its implementation looks like this:

def represent(object, options = {})
    object.extend self
    object
end

The objects gets serialized, but not its class definition with includes the extended methods.

A solution is to roll out a presenter that looks like this:

module Grape
  module Roar
    module Representer
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def represent(object, options = {})
          object.extend self
          object.as_json(options) # usually cannot be `to_json` because Grape formatter would re-encode this
        end
      end
    end
  end
end

Maybe this should just be a class called Grape::Roar::Representer::JSON?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant