add the inc command to a repository (implementation in MongoDB + Filesystem/Memory)

did committed Jan 16, 2016
commit 350de0d086a82a82fc51f9bdba75761a4619ef06
Showing 7 changed files with 56 additions and 1 deletions
bin/steam.rb +1 -1
@@ @@ -82,7 +82,7 @@ Locomotive::Common.configure do |config|
config.notifier = Locomotive::Common::Logger.setup(options[:log_file])
end
- app = Locomotive::Steam::Server.to_app
+ app = Locomotive::Steam.to_app
# Thin rack handler
# Note: alt thin settings (Threaded)
locomotive/steam/adapters/filesystem.rb b/lib/locomotive/steam/adapters/filesystem.rb +5 -0
@@ @@ -45,6 +45,11 @@ module Locomotive::Steam
entity
end
+ def inc(mapper, entity, attribute, amount = 1)
+ entity[attribute] += amount
+ entity
+ end
+
def delete(mapper, scope, entity)
# TODO: to be implemented
end
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb +4 -0
@@ @@ -34,6 +34,10 @@ module Locomotive::Steam
command(mapper).insert(entity)
end
+ def inc(mapper, entity, attribute, amount = 1)
+ command(mapper).inc(entity, attribute, amount)
+ end
+
def delete(mapper, scope, entity)
command(mapper).delete(entity)
end
locomotive/steam/adapters/mongodb/command.rb b/lib/locomotive/steam/adapters/mongodb/command.rb +8 -0
@@ @@ -20,6 +20,14 @@ module Locomotive::Steam
entity
end
+ def inc(entity, attribute, amount = 1)
+ entity.tap do
+ @collection.find(_id: entity._id).update_one('$inc' => { attribute => amount })
+ entity[attribute] ||= 0
+ entity[attribute] += amount
+ end
+ end
+
def delete(entity)
@collection.find(_id: entity._id).delete_one if entity._id
end
locomotive/steam/models/repository.rb b/lib/locomotive/steam/models/repository.rb +4 -0
@@ @@ -32,6 +32,10 @@ module Locomotive::Steam
adapter.create(mapper, scope, entity)
end
+ def inc(entity, attribute, amount = 1)
+ adapter.inc(mapper, entity, attribute, amount)
+ end
+
def delete(entity)
adapter.delete(mapper, scope, entity)
end
spec/integration/repositories/content_entry_repository_spec.rb +16 -0
@@ @@ -76,6 +76,22 @@ describe Locomotive::Steam::ContentEntryRepository do
end
+ describe '#inc' do
+
+ let(:type) { type_repository.by_slug('songs') }
+ let(:attributes) { { title: 'Jeremy', band: 'pearl-jam', short_description: '"Jeremy" is a song by the American rock band Pearl Jam', views: 41 } }
+ let(:entry) { repository.with(type).build(attributes) }
+
+ before { repository.create(entry) }
+
+ subject { repository.inc(entry, :views) }
+
+ it { expect(subject.views).to eq 42 }
+
+ after { repository.delete(entry) }
+
+ end
+
end
context 'MongoDB' do
spec/unit/adapters/filesystem_adapter_spec.rb +18 -0
@@ @@ -50,4 +50,22 @@ describe Locomotive::Steam::FilesystemAdapter do
end
+ describe '#inc' do
+
+ let(:entity) { OpenStruct.new(name: 'My post', views: 41) }
+
+ subject { adapter.inc(mapper, entity, :views) }
+
+ it { expect(subject.views).to eq 42 }
+
+ describe 'by an amount different from 1' do
+
+ subject { adapter.inc(mapper, entity, :views, 3) }
+
+ it { expect(subject.views).to eq 44 }
+
+ end
+
+ end
+
end