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

Dependor singleton #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ require 'dependor/shorty'

class MyInjector
include Dependor::AutoInject

takes :params, :session, :request

def foo
Expand All @@ -274,6 +274,38 @@ class PostsController < ApplicationController
end
```

## Dependor::Singleton

You can include this to make your service class as a singleton.

```ruby
class FooService
include Dependor::Singleton
end

class MyInjector
include Dependor::AutoInject

takes :params, :session, :request
end

class ApplicationController
extend Dependor::Injectable

def injector
@injector ||= MyInjector.new(params, session, request)
end
end

class PostsController < ApplicationController
inject :foo_service

def get
render text: foo_service.object_id == foo_service.object_id # true
end
end
```

## Testing

Dependor doesn't add any dependencies to your classes so you can test them any way you like.
Expand Down
1 change: 1 addition & 0 deletions lib/dependor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Dependor
autoload :Injectable, 'dependor/injectable'
autoload :Instantiator, 'dependor/instantiator'
autoload :Isolate, 'dependor/isolate'
autoload :Singleton, 'dependor/singleton'
autoload :Let, 'dependor/let'
autoload :SendingInjector, 'dependor/sending_injector'
autoload :Shorty, 'dependor/shorty'
Expand Down
7 changes: 6 additions & 1 deletion lib/dependor/instantiator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ class Instantiator
def initialize(injector, dependency_names)
@injector = injector
@dependency_names = dependency_names
@singletons = {}
end

def instantiate(klass)
dependencies = dependency_names.for_class(klass).map{|name| @injector.get(name)}
return klass.new(*dependencies)
if klass.include?(Dependor::Singleton)
return @singletons[klass] ||= klass.new(*dependencies)
else
return klass.new(*dependencies)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/dependor/singleton.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Dependor
module Singleton
end
end
20 changes: 20 additions & 0 deletions spec/dependor/instantiator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ def foo

instance.foo.should == 'foo-bar-baz'
end

it "instantiates objects as a new object" do
klass = Class.new {}

first_instance = instantiator.instantiate(klass)
second_instance = instantiator.instantiate(klass)

first_instance.object_id.should_not == second_instance.object_id
end

it "instantiates objects as a singleton object" do
klass = Class.new do
include Dependor::Singleton
end

first_instance = instantiator.instantiate(klass)
second_instance = instantiator.instantiate(klass)

first_instance.object_id.should == second_instance.object_id
end
end