Skip to content

Commit

Permalink
Make repositories work with data in
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Uceda Pompa committed Feb 15, 2016
1 parent 23c32fd commit f773355
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/hanami/model/adapters/implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Implementation
# @api private
# @since 0.1.0
def persist(collection, entity)
if entity.id
if entity.to_hash[:id]
update(collection, entity)
else
create(collection, entity)
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/model/adapters/memory/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def update(entity)
# @api private
# @since 0.1.0
def delete(entity)
records.delete(entity.id)
records.delete(entity.to_hash[:id])
end

# Returns all the raw records
Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/model/adapters/sql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create(collection, entity)
# @since 0.1.0
def update(collection, entity)
command(
_find(collection, entity.id)
_find(collection, entity.to_hash[:id])
).update(entity)
end

Expand All @@ -89,7 +89,7 @@ def update(collection, entity)
# @since 0.1.0
def delete(collection, entity)
command(
_find(collection, entity.id)
_find(collection, entity.to_hash[:id])
).delete
end

Expand Down
7 changes: 4 additions & 3 deletions lib/hanami/model/mapping/collection_coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ def deserialize_#{ attr.mapped }(value)

instance_eval <<-EVAL, __FILE__, __LINE__
def to_record(entity)
if entity.id
Hash[#{ @collection.attributes.map{|name,attr| ":#{ attr.mapped },#{ attr.dump_coercer }(entity.#{name})"}.join(',') }]
attributes = entity.to_hash
if attributes[:id]
Hash[#{ @collection.attributes.map{|name,attr| ":#{ attr.mapped },#{ attr.dump_coercer }(attributes[:#{name}])"}.join(',') }]
else
Hash[].tap do |record|
#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,attr| "value = #{ attr.dump_coercer }(entity.#{name}); record[:#{attr.mapped}] = value unless value.nil?"}.join('; ') }
#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,attr| "value = #{ attr.dump_coercer }(attributes[:#{name}]); record[:#{attr.mapped}] = value unless value.nil?"}.join('; ') }
end
end
end
Expand Down
58 changes: 51 additions & 7 deletions test/model/adapters/file_system_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

describe Hanami::Model::Adapters::FileSystemAdapter do
before do
TestUser = Struct.new(:id, :name, :age) do
TestUser = Class.new do
include Hanami::Entity

attributes :name, :age
end

class TestUserRepository
include Hanami::Repository
end

TestDevice = Struct.new(:id) do
TestDevice = Class.new do
include Hanami::Entity
end

Expand Down Expand Up @@ -145,12 +147,19 @@ class TestDeviceRepository
describe 'when the given entity is not persisted' do
let(:entity) { TestUser.new }

it 'stores the record and assigns an id' do
it 'stores the record and assigns an id with entity' do
result = @adapter.persist(collection, entity)

result.id.wont_be_nil
@verifier.find(collection, result.id).must_equal result
end

it 'stores the record and assigns an id with hash' do
result = @adapter.persist(collection, {})

result.id.wont_be_nil
@verifier.find(collection, result.id).must_equal result
end
end

describe 'when the given entity is persisted' do
Expand All @@ -160,7 +169,7 @@ class TestDeviceRepository

let(:entity) { TestUser.new }

it 'updates the record and leaves untouched the id' do
it 'updates the record and leaves untouched the id with entity' do
id = @entity.id
id.wont_be_nil

Expand All @@ -170,18 +179,37 @@ class TestDeviceRepository
@entity.id.must_equal(id)
@verifier.find(collection, @entity.id).name.must_equal @entity.name
end

it 'updates the record and leaves untouched the id with hash' do
id = @entity.id
id.wont_be_nil

attributes = @entity.to_hash
attributes[:name] = 'L'
@adapter.persist(collection, attributes)

attributes[:id].must_equal(id)
@verifier.find(collection, @entity.id).name.must_equal attributes[:name]
end
end
end

describe '#create' do
let(:entity) { TestUser.new }

it 'stores the record and assigns an id' do
it 'stores the record and assigns an id with entity' do
result = @adapter.create(collection, entity)

result.id.wont_be_nil
@verifier.find(collection, result.id).must_equal result
end

it 'stores the record and assigns an id with hash' do
result = @adapter.create(collection, {})

result.id.wont_be_nil
@verifier.find(collection, result.id).must_equal result
end
end

describe '#update' do
Expand All @@ -191,7 +219,7 @@ class TestDeviceRepository

let(:entity) { TestUser.new(id: nil, name: 'L') }

it 'stores the changes and leave the id untouched' do
it 'stores the changes and leave the id untouched with entity' do
id = @entity.id

@entity.name = 'MG'
Expand All @@ -200,6 +228,17 @@ class TestDeviceRepository
@entity.id.must_equal id
@verifier.find(collection, @entity.id).name.must_equal @entity.name
end

it 'stores the changes and leave the id untouched with hash' do
id = @entity.id

attributes = @entity.to_hash
attributes[:name] = 'MG'
@adapter.update(collection, attributes)

@entity.id.must_equal id
@verifier.find(collection, @entity.id).name.must_equal attributes[:name]
end
end

describe '#delete' do
Expand All @@ -209,10 +248,15 @@ class TestDeviceRepository

let(:entity) { TestUser.new }

it 'removes the given identity' do
it 'removes the given identity with entity' do
@adapter.delete(collection, entity)
@verifier.find(collection, entity.id).must_be_nil
end

it 'removes the given identity with hash' do
@adapter.delete(collection, entity.to_hash)
@verifier.find(collection, entity.id).must_be_nil
end
end

describe '#all' do
Expand Down
57 changes: 50 additions & 7 deletions test/model/adapters/memory_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

describe Hanami::Model::Adapters::MemoryAdapter do
before do
TestUser = Struct.new(:id, :name, :age) do
TestUser = Class.new do
include Hanami::Entity

attributes :name, :age
end

class TestUserRepository
include Hanami::Repository
end

TestDevice = Struct.new(:id) do
TestDevice = Class.new do
include Hanami::Entity
end

Expand Down Expand Up @@ -63,12 +65,19 @@ class TestDeviceRepository
describe 'when the given entity is not persisted' do
let(:entity) { TestUser.new }

it 'stores the record and assigns an id' do
it 'stores the record and assigns an id with entity' do
result = @adapter.persist(collection, entity)

result.id.wont_be_nil
@adapter.find(collection, result.id).must_equal result
end

it 'stores the record and assigns an id with hash' do
result = @adapter.persist(collection, {})

result.id.wont_be_nil
@adapter.find(collection, result.id).must_equal result
end
end

describe 'when the given entity is persisted' do
Expand All @@ -78,7 +87,7 @@ class TestDeviceRepository

let(:entity) { TestUser.new }

it 'updates the record and leaves untouched the id' do
it 'updates the record and leaves untouched the id with entity' do
id = @entity.id
id.wont_be_nil

Expand All @@ -88,18 +97,37 @@ class TestDeviceRepository
@entity.id.must_equal(id)
@adapter.find(collection, @entity.id).name.must_equal @entity.name
end

it 'updates the record and leaves untouched the id with hash' do
id = @entity.id
id.wont_be_nil

attributes = @entity.to_hash
attributes[:name] = 'L'
@adapter.persist(collection, attributes)

@entity.id.must_equal(attributes[:id])
@adapter.find(collection, attributes[:id]).name.must_equal attributes[:name]
end
end
end

describe '#create' do
let(:entity) { TestUser.new }

it 'stores the record and assigns an id' do
it 'stores the record and assigns an id with entity' do
result = @adapter.create(collection, entity)

result.id.wont_be_nil
@adapter.find(collection, result.id).must_equal result
end

it 'stores the record and assigns an id with hash' do
result = @adapter.create(collection, {})

result.id.wont_be_nil
@adapter.find(collection, result.id).must_equal result
end
end

describe '#update' do
Expand All @@ -109,7 +137,7 @@ class TestDeviceRepository

let(:entity) { TestUser.new(id: nil, name: 'L') }

it 'stores the changes and leave the id untouched' do
it 'stores the changes and leave the id untouched with entity' do
id = @entity.id

entity.name = 'MG'
Expand All @@ -118,6 +146,15 @@ class TestDeviceRepository
@entity.id.must_equal id
@adapter.find(collection, @entity.id).name.must_equal @entity.name
end

it 'stores the changes and leave the id untouched with hash' do
attributes = @entity.to_hash
attributes[:name] = 'MG'
entity = @adapter.update(collection, attributes)

@entity.id.must_equal attributes[:id]
@adapter.find(collection, @entity.id).name.must_equal entity.name
end
end

describe '#delete' do
Expand All @@ -127,10 +164,16 @@ class TestDeviceRepository

let(:entity) { TestUser.new }

it 'removes the given identity' do
it 'removes the given identity with entity' do
@adapter.delete(collection, entity)
@adapter.find(collection, entity.id).must_be_nil
end

it 'removes the given identity with hash' do
attributes = entity.to_hash
@adapter.delete(collection, attributes)
@adapter.find(collection, attributes[:id]).must_be_nil
end
end

describe '#all' do
Expand Down

0 comments on commit f773355

Please sign in to comment.