Skip to content

Lookup Representers Dynamically #1

Closed

Description

@johnallen3d

Would you consider adding support for automatically extending object with the appropriate representer and releasing this as a gem?

Say something along these lines of:

def call(object, env)
  representer_name = "#{object.class}Representer"

  if Object.const_definied?(representer_name)
    representer_class = Object.const_get()
    object.extend(representer_class)
  end

  Grape::Formatter::Json.call object, env
end

Activity

  1. dblock commented on Aug 26, 2013

    @dblock
    Member

    That could work, yes.

  2. johnallen3d commented on Aug 26, 2013

    @johnallen3d
    Author

    Cool, I was thinking about it a little more over the weekend and it possible you'd want to have pluralized versions of representers as well. I'll work something up and submit a pull request. Thanks.

  3. changed the title [-]Lookup Representers Dynamically?[/-] [+]Lookup Representers Dynamically[/+] on Jul 23, 2014
  4. thisiscab commented on Jul 22, 2015

    @thisiscab

    Is there any progress on this?

  5. johnallen3d commented on Jul 22, 2015

    @johnallen3d
    Author

    Sorry, I've not been working roar of late and haven't gotten back to this.

  6. dblock commented on Oct 27, 2026

    @dblock
    Member

    PR #46 documents and verifies Grape’s built-in lookup: assign a representer module or decorator to Product::Entity, then call present product without with:. Decorator classes also support API-local represent Product, with: ProductRepresenter mappings without modifying the model. An explicit with: overrides inference.

    This avoids global class-name guessing and singular/plural ambiguity. It does not silently mutate arbitrary endpoint return values; call present to request inference. Request regressions cover both module/decorator inference, overrides, and API-local mapping.

    Posted by Copilot, an AI, on behalf of @dblock.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions