Giter Club home page Giter Club logo

Comments (2)

sunny avatar sunny commented on July 3, 2024 2

@fidalgo I have simplified this a bit, so that in v3.3.0 you can now use input :client, default: ServiceClient 🎉

from actor.

sunny avatar sunny commented on July 3, 2024

Hi there! I haven’t used actors for dependency injection, so I hadn’t encountered this use-case before. It looks very versatile though, I can think of a few places where this would be very handy in my code!

The issue here is that in order to handle dynamic defaults, the default: keyword tries to trigger .call on whatever is given to it (if it responds to that method). This works well for lambdas, but not so much for actors themselves.

Therefore, one way to fix this is to define the default inside a lambda, as so:

input :client, default: -> { ServiceClient }

The full solution for your use-case would be:

class ServiceClient < Actor
  input :token, allow_nil: false
  input :term, allow_nil: false

  output :outcome

  def call
    self.outcome = Faraday.new('http://example.com/service').get("", query: term)
  end
end

class Persistor < Actor
  input :client, default: -> { ServiceClient }
  
  def call
    outcome = client.result(token: 'token', term: 'actor').outcome
    puts outcome.status if outcome.success?
  end
end

from actor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.