Coder Social home page Coder Social logo

crystal-di's People

Contributors

vladislav-yashin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

crystal-di's Issues

Tagged services

Hey, first off, this is my new favorite shard. I had a feature request that would make it even better; the ability to tag services. For example

class FeedPartner
  include DI::Taggable

  getter id : String
  getter name : String

  def initialize(@id : String, @name : String); end
end

module Container
  include DI::ContainerMixin

  register FeedPartner, FeedPartner.new("GOOGLE", "Google"), tag: "feed_partner"
  register FeedPartner, FeedPartner.new("FACEBOOK", "Facebook"), tag: "feed_partner"
end

partners = Container.tagged "feed_partner" # => # => [#<FeedPartner:0x7f0f7891bd00 @id="GOOGLE", @name="Google">, #<FeedPartner:0x7f0f7891bcc0 @id="FACEBOOK", @name="Google">]

My current idea to implement this would be to define a module like DI::Taggable that can be included into types that the user wants to be able to be tagged.

Then the ContainerMixin could define a hash like @@services = Hash(String, Array(DI::Taggable)).new, where upon adding a resolve method, add that value to hash for the given tag.

\{% if type.resolve < DI::Taggable %}
  @@services[\{{tag}}] = Array(DI::Taggable).new unless @@services.has_key? \{{tag}}
  @@services[\{{tag}}] << \{{value}}.as(\{{type}})
\{% end %}

Finally, implement a method to get services based on tag:

def self.tagged(tag : String)
  @@services[tag]? || [] of DI::Taggable
end

I currently have this working on my fork, however I wanted to get your thoughts before making a PR.

Installation failed.

I added this to my shards.yml

dependencies:
  di:
    github: funk-yourself/crystal-di

and run crystal deps, but I encountered following error:

Updating https://github.com/funk-yourself/crystal-di.git
git command failed: git ls-tree -r --full-tree --name-only v0.1.0 -- shard.yml (). Maybe a commit, branch or file doesn't exist?

git version 2.14.1
Crystal 0.23.1

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.