Skip to content

Commit

Permalink
Bug: When module name and class name are identical, associations do n…
Browse files Browse the repository at this point in the history
…ot resolve correctly to class name
  • Loading branch information
rcdexta committed Jan 31, 2015
1 parent 0f5285e commit 6671a98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/hash19/resolvers.rb
Expand Up @@ -50,7 +50,7 @@ def resolve_injections(hash)

def resolve_class(assoc_name)
full_class_name = self.class.name
new_class = full_class_name.gsub(full_class_name.demodulize, assoc_name)
new_class = construct_association_class full_class_name, assoc_name
new_class.split('::').inject(Object) do |mod, class_name|
begin
mod.const_get(class_name)
Expand All @@ -62,6 +62,11 @@ def resolve_class(assoc_name)

private

def construct_association_class(parent, association)
*modules, class_name = parent.split('::')
(modules + [association]).join('::')
end

def async_injections
self.class.injections.select { |e| e[:async].nil? }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hash19/version.rb
@@ -1,3 +1,3 @@
module Hash19
VERSION = '0.0.9'
VERSION = '0.0.10'
end
22 changes: 19 additions & 3 deletions spec/hash19/associations_spec.rb
Expand Up @@ -30,7 +30,7 @@ class Laptop < Testable
end

expect { Computer::Laptop.new(model: 'MacBook Pro', year: 2013, keyboard: {keys: 101}) }.
to raise_error('Class:<Computer::Keyboard> not defined! Unable to resolve association:<keyboard>')
to raise_error('Class:<Computer::Keyboard> not defined! Unable to resolve association:<keyboard>')
end

it 'should support alternate key in payload for has_one' do
Expand Down Expand Up @@ -144,7 +144,7 @@ class UDPPacket < Testable
it 'should be able to call the trigger on has_one association' do
packet = UDPPacket.new(code: 500, error_id: 500)
expect(packet.to_h).to eq('code' => 500, 'error_id' => 500, 'all_errors' => [{'error_id' => 500, 'desc' => 'fatal error'},
{'error_id' => 404, 'desc' => 'not found'}])
{'error_id' => 404, 'desc' => 'not found'}])
end

end
Expand All @@ -157,7 +157,23 @@ class Jedi < Testable
end

jedi = Jedi.new(name: 'Obi Wan Kenobi')
expect(jedi.to_h).to eq({"name" => 'Obi Wan Kenobi'})
expect(jedi.to_h).to eq({'name' => 'Obi Wan Kenobi'})
end
end

context 'module and class with same name' do
module SecretAgentService
class SecretAgent < Testable
attributes :code
has_one :agency
end
class Agency < Testable
attributes :name
end
end
it 'should resolve class when association class and containing module have same name' do
mi6 = SecretAgentService::SecretAgent.new({code: '007', agency: {name: 'MI6'}})
expect(mi6.to_h).to eq({'code' => '007', 'agency' => {'name' => 'MI6'}})
end
end

Expand Down

0 comments on commit 6671a98

Please sign in to comment.