This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe StatelessCalculator do | |
it 'should add some numbers' do | |
expect(StatelessCalculator.add(10, StatelessCalculator.add(5, 3))).to eq(18) | |
end | |
it 'should divide some numbers' do | |
expect(StatelessCalculator.div(10, 2)).to eq(5) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_range_unsafe | |
c = StatefulCalculator.new | |
threads = (1..256).each_slice(16).map do |range| | |
Thread.new do | |
range.each { |n| c.add n } | |
end | |
end | |
threads.map(&:join) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_range_safe | |
threads = (1..256).each_slice(16).map do |range| | |
Thread.new do | |
Thread.current[:sum] = range.inject(0) do |memo, n| | |
StatelessCalculator.add memo, n | |
end | |
end | |
end | |
result = threads.map { |t| t.join[:sum] } .reduce(:+) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/services/calculation_service.rb | |
class CalculationService | |
def initialize(repository) | |
@repository = repository | |
end | |
def calculate(operation, x, y) | |
result = StatelessCalculator.send operation, x, y | |
@repository.save_calculation operation, x, y, result | |
result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumberCruncher | |
def initialize(calculator) | |
@calculator = calculator | |
end | |
def crunch(array_of_numbers) | |
@reduction ||= array_of_numbers.each do |number| | |
@calculator.add number | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/calculation.rb | |
def create | |
req = ActiveSupport::JSON.decode(request.body) | |
calculator = StatefulCalculator.new | |
# to_crunch => [[1, 2, 3], [9, 10, 11]] | |
@sums = req["to_crunch"].map do |nums| | |
NumberCruncher.new(calculator).crunch(nums) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Account = Struct.new(:id, :last_active) | |
describe 'pruning expired accounts' do | |
let(:acc1) { Account.new(1, Date.today) } | |
let(:acc2) { Account.new(2, Date.today) } | |
let(:acc3) { Account.new(3, Date.today) } | |
describe 'when the first account is expired' do | |
let(:acc1) { Account.new(1, Date.today - 45) } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Account = Struct.new(:id, :last_active) | |
describe 'pruning expired accounts' do | |
let(:f) { | |
# an account factory | |
lambda do |id, last_active=Date.today| | |
Account.new(id, last_active) | |
end | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class S3FilePruner < JobProcessor | |
def process_job | |
Images.all.select do |image| | |
image.created_at < Date.today - 30 | |
end.each do |image| | |
S3Object.delete image.s3_object_name, image.s3_bucket_name | |
image.destroy | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/util/prunable.rb | |
class Prunable | |
def self.from_assets(assets) | |
assets.select &method(:is_prunable?) | |
end | |
def self.is_prunable?(asset) | |
asset.created_at < Date.today - 30 | |
end | |
end |