Skip to content

Dependency injection? #1906

Closed
Closed
@unleashy

Description

@unleashy

Hi! Sorry if this is a stupid question, but I've searched the documentation and it's very unclear to me how I can pass objects from outside Grape::API's scope through, say, a constructor, for dependency injection purposes?

I've tried using a bog-standard constructor, but a class derived from Grape::API doesn't seem to like being initialized:

class Foo < Grape::API
  def initialize(baz)
    @baz = baz
  end

  get :bar do
    @baz
  end
end

run Foo.new('hello') # ArgumentError

If I remove the .new call so it's just run Foo the constructor isn't even called, so @baz is obviously nil.

It seems configuration would be able to do this but it's incredibly unclear to me how to use it outside of mount, or, for that matter, when initialising the object which is what I want to do :)

There's also another, less related question. Are routes supposed to use symbols or strings? Or either? If it's either, is there a convention? That also isn't clear to me.

Thank you and I hope I'm not being too dense lol :p

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