Skip to content

Commit

Permalink
basic functionality for class injecting
Browse files Browse the repository at this point in the history
  • Loading branch information
kustosz committed Sep 9, 2014
1 parent fd73a01 commit 2b22909
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions features/class_injections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Feature: Class Injections
"""
Then the output should be:
"""
"For the King!"
"Welcome to FarFarAway's army, fellow Sword Master."
For the King!
Welcome to FarFarAway's army, fellow Sword Master.
"""

8 changes: 8 additions & 0 deletions lib/dependor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
require "dependor/registry"
require "dependor/lookup_chain"

require "dependor/injectable_class"
require "dependor/class_takes_ext"
require "dependor/subclass_builder"

module Dependor
def self.registry(&block)
Registry.build(&block)
Expand All @@ -22,4 +26,8 @@ def self.registry(&block)
def self.takes(*dependency_names)
TakesExt.Takes(*dependency_names)
end

def self.class_takes(*dependency_names)
ClassTakesExt.ClassTakes(*dependency_names)
end
end
13 changes: 13 additions & 0 deletions lib/dependor/class_takes_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Dependor
module ClassTakesExt
module_function
def ClassTakes(*names)
Module.new do
define_singleton_method :extended do |klass|
klass.send(:extend, Dependor::InjectableClass)
klass.add_dependencies(*names)
end
end
end
end
end
8 changes: 8 additions & 0 deletions lib/dependor/injectable_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Dependor
module InjectableClass
attr_accessor :class_takes
def add_dependencies(*names)
@class_takes = names
end
end
end
5 changes: 5 additions & 0 deletions lib/dependor/instantiator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def new(klass_name, overwrites = {})
klass.new(args)
end

def get_class(klass_name)
klass = @class_lookup.lookup(klass_name)
SubclassBuilder.subclass(klass, @injector)
end

def method_missing(name, *args, &block)
super if args.any?
@injector[name]
Expand Down
5 changes: 4 additions & 1 deletion lib/dependor/object_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def self.default_for(klass)
new(klass.name.to_sym, opts, proc{ new(klass) })
end

def self.build(name, transient: false, &block)
def self.build(name, transient: false, as: :instance, &block)
if as == :class
block ||= proc{ get_class(name) }
end
block ||= proc{ new(name) }
new(name, {transient: transient}, block)
end
Expand Down
15 changes: 15 additions & 0 deletions lib/dependor/subclass_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Dependor
class SubclassBuilder
def self.subclass(klass, injector)
return klass unless klass.respond_to?(:class_takes)

Class.new(klass) do
klass.class_takes.each do |dependency|
define_singleton_method dependency do
injector[dependency]
end
end
end
end
end
end

0 comments on commit 2b22909

Please sign in to comment.