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/utils/calculator.rb | |
module StatelessCalculator | |
def add(x, y) | |
x + y | |
end | |
module_function :add | |
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 RssImporter | |
def self.import(articles) | |
articles.map do |article| | |
RssContent.create { url: article.url, text: article.body } | |
end | |
end | |
end | |
# somewhere in your codebase... | |
imported = RssImporter.import a_bunch_of_rss_content |
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
# our first import | |
importer = RssImporter.new | |
importer.articles = a_bunch_of_rss_content | |
importer.import | |
imported = importer.result | |
# second import, same instance of RssImporter | |
importer.articles = other_rss_content | |
importer.result # same value as "imported" on line 5 |
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 RssImporter | |
attr_writer :articles | |
attr_reader :result | |
def import | |
@result = articles.map do |article| | |
RssContent.create { url: article.url, text: article.body } | |
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/utils/calculator.rb | |
class StatelessCalculator | |
def self.add(x, y) | |
x + y | |
end | |
# ...subtract, divide, multiply, etc. | |
end | |
# app/services/calculation_isolate.rb |
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 |
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
# 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
# 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
# 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) |