Skip to content

Commit

Permalink
Support for alias attributes in lookup, Change params to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
rcdexta committed Oct 27, 2014
1 parent 8e9f77f commit b5950b2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 21 deletions.
2 changes: 1 addition & 1 deletion features/models/employee.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Employee
include Looksist
attr_accessor :employee_id
lookup :name, using = :employee_id
lookup :name, using: :employee_id

def initialize(id)
@employee_id = id
Expand Down
27 changes: 19 additions & 8 deletions lib/looksist/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@ module Core
include Looksist::Common

module ClassMethods
def lookup(what, using, bucket = using)
def lookup(what, opts)
unless opts.keys.all? { |k| [:using, :bucket_name, :as].include? k }
raise 'Incorrect usage: Invalid parameter specified'
end
using, bucket_name, as = opts[:using], opts[:bucket_name] || opts[:using], opts[:as]
if what.is_a? Array
setup_composite_lookup(bucket, using, what)
setup_composite_lookup(bucket_name, using, what, as)
else
self.lookup_attributes << what.to_sym
define_method(what) do
Looksist.redis_service.send("#{__entity__(bucket)}_for", self.send(using).try(:to_s))
alias_what = find_alias(as, what)
self.lookup_attributes << alias_what
define_method(alias_what) do
Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
end
end
end

private
def setup_composite_lookup(bucket, using, what)
def setup_composite_lookup(bucket, using, what, as)
what.each do |method_name|
define_method(method_name) do
alias_method_name = find_alias(as, method_name)
define_method(alias_method_name) do
JSON.parse(Looksist.redis_service.send("#{__entity__(bucket)}_for", self.send(using).try(:to_s)) || '{}')[method_name.to_s]
end
self.lookup_attributes << method_name
self.lookup_attributes << alias_method_name
end
end

def find_alias(as_map, what)
(as_map and as_map.has_key?(what)) ? as_map[what].to_sym : what
end
end


def as_json(opts)
Looksist.driver.json_opts(self, opts)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/looksist/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Lookist
VERSION = '0.2.1'
VERSION = '0.2.2'
end
1 change: 0 additions & 1 deletion looksist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency 'jsonpath', '~> 0.5.6'


end
71 changes: 61 additions & 10 deletions spec/looksist/looksist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Employee
use_api TEST_API
include Looksist

lookup :name, using = :employee_id
lookup :name, using: :employee_id

def as_json(opts)
super(opts).merge(another_attr: 'Hello World')
Expand All @@ -38,7 +38,7 @@ module ExplicitBucket
class Employee
include Looksist
attr_accessor :id
lookup :name, using = :id, bucket_name = 'employees'
lookup :name, using: :id, bucket_name: 'employees'

def initialize(id)
@id = id
Expand All @@ -51,14 +51,65 @@ def initialize(id)
end
end

context 'Alias support for lookup' do
it 'should fetch attributes and use the alias specified in the api' do
module AliasLookup
class Employee
include Her::Model
use_api TEST_API
include Looksist
attr_accessor :id
lookup :name, using: :id, bucket_name: 'employees', as: {name: 'nome'}

def initialize(id)
@id = id
end

def as_json(opts)
super(opts).merge(id: @id)
end
end
end
expect(@mock).to receive(:get).once.with('employees/1').and_return('Rajini')
e = AliasLookup::Employee.new(1)
expect(e.nome).to eq('Rajini')
expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"id\":1}")
end

it 'should fetch attributes and use the alias for specific attributes in the api' do
module AliasSpecificLookup
class Employee
include Her::Model
use_api TEST_API
include Looksist
attr_accessor :id
lookup [:name, :age], using: :id, as: {name: 'nome'}

def initialize(id)
@id = id
end

def as_json(opts)
super(opts).merge(id: @id)
end
end
end
expect(@mock).to receive(:get).once.with('ids/1').and_return({name: 'Rajini', age: 16}.to_json)
e = AliasSpecificLookup::Employee.new(1)
expect(e.nome).to eq('Rajini')
expect(e.age).to eq(16)
expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"age\":16,\"id\":1}")
end
end

context 'Lazy Evaluation' do
module LazyEval
class HerEmployee
include Her::Model
use_api TEST_API
include Looksist

lookup :name, using = :employee_id
lookup :name, using: :employee_id

def as_json(opts)
super(opts).merge(another_attr: 'Hello World')
Expand All @@ -67,7 +118,7 @@ def as_json(opts)
class Employee
include Looksist
attr_accessor :id
lookup :name, using = :id, bucket_name = 'employees'
lookup :name, using: :id, bucket_name: 'employees'

def initialize(id)
@id = id
Expand All @@ -87,8 +138,8 @@ module SimpleLookup
class Employee
include Looksist
attr_accessor :id, :employee_id
lookup :name, using= :id
lookup :unavailable, using= :employee_id
lookup :name, using: :id
lookup :unavailable, using: :employee_id

def initialize(id)
@id = @employee_id = id
Expand All @@ -109,9 +160,9 @@ class Employee
include Looksist
attr_accessor :id, :employee_id, :contact_id

lookup [:name, :location], using=:id
lookup [:age, :sex], using=:employee_id
lookup [:pager, :cell], using=:contact_id
lookup [:name, :location], using: :id
lookup [:age, :sex], using: :employee_id
lookup [:pager, :cell], using: :contact_id

def initialize(id)
@contact_id = @id = @employee_id = id
Expand Down Expand Up @@ -140,7 +191,7 @@ class Employee
include Looksist
attr_accessor :id

lookup [:name, :location], using=:id
lookup [:name, :location], using: :id

def initialize(id)
@id = id
Expand Down

0 comments on commit b5950b2

Please sign in to comment.