Created
September 15, 2012 15:04
-
-
Save davybrion/3728390 to your computer and use it in GitHub Desktop.
code snippets for "Why You Don’t Need Dependency Injection In Ruby" post
This file contains hidden or 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
class Dependency | |
def do_something_with(some_object) | |
p some_object | |
end | |
end | |
class SomeClass | |
def work_your_magic_on(something) | |
Dependency.new.do_something_with something | |
end | |
end |
This file contains hidden or 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
class SomeClass | |
def initialize(dependency) | |
@dependency = dependency | |
end | |
def work_your_magic_on(something) | |
@dependency.do_something_with something | |
end | |
end |
This file contains hidden or 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
class SomeClass | |
def work_your_magic_on(something) | |
get_dependency.do_something_with something | |
end | |
private | |
def get_dependency | |
@dependency = Dependency.new if @dependency.nil? | |
@dependency | |
end | |
end |
This file contains hidden or 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
def test_magic | |
object = SomeClass.new | |
object.work_your_magic_on "some important string" | |
# TODO: assert that the string was passed to the dependency correctly | |
end |
This file contains hidden or 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
class DependencySpy | |
@@passed_in_objects = [] | |
def self.passed_in_objects | |
@@passed_in_objects | |
end | |
def do_something_with(some_object) | |
@@passed_in_objects << some_object | |
end | |
end |
This file contains hidden or 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
def test_magic | |
object = SomeClass.new | |
def object.get_dependency | |
DependencySpy.new | |
end | |
object.work_your_magic_on "some important string" | |
assert DependencySpy.passed_in_objects.include? "some important string" | |
end |
This file contains hidden or 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
class SomeClass | |
private | |
def get_dependency | |
@dependency = SomeOtherDependency.new if @dependency.nil? | |
@dependency | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment