Skip to content

Commit

Permalink
Set class attributes before running inherited
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Apr 30, 2017
1 parent 56bedad commit bd07219
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/dry/core/class_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def defines(*args)
end

define_method(:inherited) do |klass|
super(klass)
args.each { |name| klass.public_send(name, send(name)) }

super(klass)
end
end

Expand Down
24 changes: 24 additions & 0 deletions spec/dry/core/class_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,28 @@ class OtherClass < MyClass
it 'allows overwriting of inherited values' do
expect(OtherClass.two).to eq('two')
end

it 'copies values from the parent before running hooks' do
subclass_value = nil

module_with_hook = Module.new do
define_method(:inherited) do |klass|
super(klass)

subclass_value = klass.one
end
end

base_class = Class.new do
extend Dry::Core::ClassAttributes
extend module_with_hook

defines :one
one 1
end

Class.new(base_class)

expect(subclass_value).to be 1
end
end

0 comments on commit bd07219

Please sign in to comment.