Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented simple dependency injection based only on dependency name.
- Loading branch information
Adam Pohorecki
committed
Apr 14, 2011
1 parent
66e4db3
commit 0ffff63
Showing
13 changed files
with
196 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pkg/* | |
*.gem | ||
.bundle | ||
.rvmrc | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
require 'dependor/injectable.rb' | ||
require 'dependor/meta_data.rb' | ||
require 'dependor/injector.rb' | ||
require 'dependor/dependency_to_class_name_converter.rb' | ||
|
||
module Fake | ||
module Dependor | ||
def self.dependency_to_class_name_converter | ||
@dependency_to_class_name_converter ||= DependencyToClassNameConverter.new | ||
end | ||
|
||
def self.injector | ||
@injector ||= Injector.new(dependency_to_class_name_converter) | ||
end | ||
end | ||
|
||
module Dependor | ||
module Fake | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Dependor | ||
class DependencyToClassNameConverter | ||
|
||
def convert(dependency_name) | ||
class_name = dashes_to_colons(dependency_name.to_s) | ||
return camelize(class_name) | ||
end | ||
|
||
private | ||
|
||
def camelize(string) | ||
string.gsub(/^[a-z]/){|s| s.upcase}.gsub(/_+/, '_').gsub(/_[a-z]/){|s| s[1].upcase} | ||
end | ||
|
||
def dashes_to_colons(string) | ||
string.gsub('-', '::_') | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Dependor | ||
class Injector | ||
|
||
attr_reader :dependency_to_class_name_converter | ||
|
||
def initialize(dependency_to_class_name_converter) | ||
@dependency_to_class_name_converter = dependency_to_class_name_converter | ||
end | ||
|
||
def inject(instance) | ||
meta_data = Dependor::MetaData.for(instance) | ||
|
||
meta_data.dependencies.each do |dependency_name| | ||
klass = class_for_name(dependency_name) | ||
dependency = inject(klass.new) | ||
instance.send("#{dependency_name}=", dependency) | ||
end | ||
|
||
return instance | ||
end | ||
|
||
private | ||
|
||
def class_for_name(dependency_name) | ||
class_name = dependency_to_class_name_converter.convert(dependency_name) | ||
parts = class_name.split('::') | ||
context = Object | ||
parts.each do |part| | ||
context = context.const_get(part) | ||
end | ||
return context | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require File.expand_path('../../spec_helper.rb', __FILE__) | ||
|
||
describe Dependor::DependencyToClassNameConverter do | ||
|
||
before(:each) do | ||
@converter = Dependor::DependencyToClassNameConverter.new | ||
end | ||
|
||
it "should convert simple names to camel case" do | ||
@converter.convert(:foo).should == "Foo" | ||
end | ||
|
||
it "should convert multi part underscore_names to CamelCase" do | ||
@converter.convert(:some_component).should == "SomeComponent" | ||
end | ||
|
||
it "should convert - to :: in names" do | ||
@converter.convert("some_module-some_class").should == "SomeModule::SomeClass" | ||
end | ||
|
||
it "should leave camelcase names unchanged" do | ||
@converter.convert("SomeClass").should == "SomeClass" | ||
end | ||
|
||
it "should leave camelcase names with :: in them unchanged" do | ||
@converter.convert("SomeModule::SomeClass").should == "SomeModule::SomeClass" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require File.expand_path('../../spec_helper.rb', __FILE__) | ||
|
||
describe Dependor::Injector do | ||
|
||
before(:each) do | ||
@injector = Dependor::Injector.new(Dependor::DependencyToClassNameConverter.new) | ||
end | ||
|
||
describe "for an instance of a class with no dependencies" do | ||
class NoDependenciesSample | ||
attr_accessor :foo | ||
end | ||
|
||
it "should do nothing" do | ||
instance = NoDependenciesSample.new | ||
|
||
@injector.inject(instance) | ||
|
||
instance.foo.should be_nil | ||
end | ||
end | ||
|
||
describe "for an instance of a class with dependencies" do | ||
it "should inject those dependencies" do | ||
bar = Bar.new | ||
|
||
@injector.inject(bar) | ||
|
||
bar.foo.should be_an_instance_of(Foo) | ||
end | ||
end | ||
|
||
describe "for a multi-level dependency hierarchy" do | ||
it "should inject dependencies of dependencies too" do | ||
baz = Baz.new | ||
|
||
@injector.inject(baz) | ||
|
||
baz.bar.foo.should be_an_instance_of(Foo) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Foo | ||
def name | ||
"world" | ||
end | ||
end | ||
|
||
class Fake::Foo | ||
def name | ||
"fake" | ||
end | ||
end | ||
|
||
class Bar | ||
include Dependor::Injectable | ||
|
||
depends_on :foo | ||
|
||
def hello | ||
return "Hello #{foo.name}!" | ||
end | ||
end | ||
|
||
class Fake::Bar | ||
def hello | ||
"Hello?" | ||
end | ||
end | ||
|
||
class Baz | ||
include Dependor::Injectable | ||
|
||
depends_on :bar | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters