spec/looksist/looksist_spec.rb in looksist-0.2.1 vs spec/looksist/looksist_spec.rb in looksist-0.2.2

- old
+ new

@@ -16,11 +16,11 @@ class Employee 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') end end @@ -36,11 +36,11 @@ it 'should consider bucket key when provided' do 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 end end @@ -49,27 +49,78 @@ e = ExplicitBucket::Employee.new(1) expect(e.name).to eq('Employee Name') 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') end end 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 end end @@ -85,12 +136,12 @@ it 'should generate declarative attributes on the model with simple lookup value' do 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 end end @@ -107,13 +158,13 @@ module CompositeLookup 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 end end @@ -138,10 +189,10 @@ context 'share storage between instances' do class Employee include Looksist attr_accessor :id - lookup [:name, :location], using=:id + lookup [:name, :location], using: :id def initialize(id) @id = id end end \ No newline at end of file