Skip to content

Commit

Permalink
#Bugfix[Priyanka] Fixing the issue where looking up a single attribut…
Browse files Browse the repository at this point in the history
…e from a json value fails and works only if it specified in an array
  • Loading branch information
priyankaiyer committed Aug 18, 2015
1 parent 3fe97dd commit 81cc063
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/looksist/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def lookup(what, opts)
alias_what = find_alias(as, what)
@lookup_attributes[alias_what] = opts[:using]
define_method(alias_what) do
Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
result = Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
begin
JSON.parse(result)[what.to_s]
rescue
result
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/looksist/hashed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ def do_populate(elt, values, using, as, populate)
end
else
alias_method = find_alias(as, populate)
elt[alias_method] = values[elt.with_indifferent_access[using]]
begin
json = JSON.parse(values[elt.with_indifferent_access[using]])
elt[alias_method] = json.with_indifferent_access[populate]
rescue
elt[alias_method] = values[elt.with_indifferent_access[using]]
end
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/looksist/hashed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,33 @@ def metrics
)
end

it 'should work for multiple attributes but looking up only one attribute' do
class HashWithMultipleAttributesLookUpOne
include Looksist

def metrics
[
{
hero_id: 1
},
{
hero_id: 2
}
]
end

inject after: :metrics, using: :hero_id, populate: :name, as: {name: 'hero_name'}
end
js1 = {name: 'Rajini', mnemonic: 'SuperStart'}.to_json
jsons = [js1, nil]
expect(@mock).to receive(:mget).once.with(*%w(heros/1 heros/2)).and_return(jsons)

expect(HashWithMultipleAttributesLookUpOne.new.metrics).to eq(
[{:hero_id => 1, :hero_name => 'Rajini'},
{:hero_id => 2, :hero_name => nil}]
)
end

it 'should work for class methods' do
class SelfHelp
include Looksist
Expand Down
18 changes: 18 additions & 0 deletions spec/looksist/looksist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def as_json(opts)
expect(e.nome).to eq('Rajini')
expect(e.age).to eq(16)
expect(e.to_json).to eq("{\"id\":1,\"nome\":\"Rajini\",\"age\":16}")
end

it 'should fetch attributes appropriately when plucking single attribute from a json key' do
module NonAliasSpecificLookup
class NoCacheEmployee
include Her::Model
use_api TEST_API
include Looksist
lookup :name, using: :id, as: {name: 'nome'}
def as_json(opts)
super(opts).merge(attributes)
end
end
end
expect(@mock).to receive(:get).twice.times.with('ids/1').and_return({name: 'Rajini', age: 16}.to_json)
e = NonAliasSpecificLookup::NoCacheEmployee.new(id:1)
expect(e.nome).to eq('Rajini')
expect(e.to_json).to eq("{\"id\":1,\"nome\":\"Rajini\"}")
end
end

Expand Down

0 comments on commit 81cc063

Please sign in to comment.