Skip to content

Commit

Permalink
Merge pull request #45 from dry-rb/set-dependencies-before-super
Browse files Browse the repository at this point in the history
Set dependency ivars before call to super in generated #initialize methods
  • Loading branch information
timriley authored Mar 26, 2018
2 parents 398fb2d + d330299 commit 733b6ff
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/dry/auto_inject/strategies/args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def define_initialize_with_params

instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def initialize(#{initialize_args})
super()
#{dependency_map.names.map { |name| "@#{name} = #{name}" }.join("\n")}
super()
end
RUBY
end
Expand All @@ -51,8 +51,8 @@ def define_initialize_with_splat(super_method)

instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def initialize(*args)
super(#{super_params})
#{dependency_map.names.map.with_index { |name, i| "@#{name} = args[#{i}]" }.join("\n")}
super(#{super_params})
end
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/auto_inject/strategies/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def define_initialize(klass)

instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def initialize(options)
super(#{super_params})
#{dependency_map.names.map { |name| "@#{name} = options[:#{name}]" }.join("\n")}
super(#{super_params})
end
RUBY
end
Expand Down
10 changes: 5 additions & 5 deletions lib/dry/auto_inject/strategies/kwargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def define_initialize_with_keywords

instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def initialize(#{initialize_params})
super()
#{dependency_map.names.map { |name| "@#{name} = #{name}" }.join("\n")}
super()
end
RUBY

Expand All @@ -55,6 +55,10 @@ def define_initialize_with_splat(super_method)

instance_mod.class_exec(dependency_map) do |dependency_map|
define_method :initialize do |*args, **kwargs|
dependency_map.names.each do |name|
instance_variable_set :"@#{name}", kwargs[name]
end

super_kwargs = kwargs.each_with_object({}) { |(key, _), hsh|
if !dependency_map.names.include?(key) || super_kwarg_names.include?(key)
hsh[key] = kwargs[key]
Expand All @@ -66,10 +70,6 @@ def define_initialize_with_splat(super_method)
else
super(*args)
end

dependency_map.names.each do |name|
instance_variable_set :"@#{name}", kwargs[name]
end
end
end

Expand Down
49 changes: 49 additions & 0 deletions spec/integration/args/super_initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

RSpec.describe "args / super #initialize method" do
before do
module Test
AutoInject = Dry::AutoInject(one: "dep 1")
end
end

describe "super #initialize using on dependencies set in the child class" do
let(:child_class) {
Class.new(parent_class) do
include Test::AutoInject.args[:one]
end
}

context "super #initialize without parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize
@excited_one = "#{one}!"
end
end
}

it "sets the dependencies in the generated #initialize before calling super" do
expect(child_class.new.excited_one).to eq "dep 1!"
end
end

context "super #initiailze with parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize(one)
@excited_one = "#{one}!"
end
end
}

it "sets the dependenceies in the generated #initialize before caling super" do
expect(child_class.new.excited_one).to eq "dep 1!"
end
end
end
end
50 changes: 50 additions & 0 deletions spec/integration/hash/super_initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

RSpec.describe "hash / super #initialize method" do
before do
module Test
AutoInject = Dry::AutoInject(one: "dep 1")
end
end

describe "super #initialize using on dependencies set in the child class" do
let(:child_class) {
Class.new(parent_class) do
include Test::AutoInject.hash[:one]
end
}

context "super #initialize without parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize
@excited_one = "#{one}!"
end
end
}

it "sets the dependencies in the generated #initialize before calling super" do
expect(child_class.new.excited_one).to eq "dep 1!"
end
end

context "super #initiailze with parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize(options = {})
@excited_one = "#{one}!"
@two = options.fetch(:two)
end
end
}

it "sets the dependenceies in the generated #initialize before caling super" do
expect(child_class.new(two: "_").excited_one).to eq "dep 1!"
end
end
end
end
49 changes: 48 additions & 1 deletion spec/integration/kwargs/super_initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,54 @@
RSpec.describe "kwargs / super #initialize method" do
before do
module Test
AutoInject = Dry::AutoInject({one: "dep 1"})
AutoInject = Dry::AutoInject(one: "dep 1")
end
end

describe "super #initialize using dependencies set in the child class" do
context "super #initialize without parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize
@excited_one = "#{one}!"
end
end
}

let(:child_class) {
Class.new(parent_class) do
include Test::AutoInject[:one]
end
}

it "sets the dependencies in the generated #initialize before calling super" do
expect(child_class.new.excited_one).to eq "dep 1!"
end
end

context "super #initialize with parameters" do
let(:parent_class) {
Class.new do
attr_reader :excited_one

def initialize(two:)
@excited_one = "#{one}!"
@two = two
end
end
}

let(:child_class) {
Class.new(parent_class) do
include Test::AutoInject[:one]
end
}

it "sets the dependenceies in the generated #initialize before caling super" do
expect(child_class.new(two: "_").excited_one).to eq "dep 1!"
end
end
end

Expand Down

0 comments on commit 733b6ff

Please sign in to comment.