4. –Alan Kay
“The key in making great and growable
systems is much more to design how its
modules communicate rather than what
their internal properties and behaviors
should be.”
5.
6. class Paperboy
attr_reader :wallet
def initialize(wallet)
@wallet = wallet
end
def receive_from(customer, amount)
if customer.wallet.try(:amount).to_f > amount
@wallet.amount = amount
customer.wallet = customer.wallet.amount - amount
else
nil
end
end
end
7. class Paperboy
attr_reader :wallet
def initialize(wallet)
@wallet = wallet
end
def receive_from(customer, amount)
if customer.wallet.try(:amount).to_f > amount
@wallet.amount = amount
customer.wallet = customer.wallet.amount - amount
else
nil
end
end
end
8. class Paperboy
attr_reader :wallet
def initialize(wallet)
@wallet = wallet
end
def receive_from(customer, amount)
if customer.wallet.try(:amount).to_f > amount
@wallet.amount = amount
customer.wallet = customer.wallet.amount - amount
else
nil
end
end
end
9. –Ruby Science
“Referencing another object’s state
directly couples two objects together
based on what they are, rather than on
what they do.”
11. describe Paperboy do
describe '#receive_from' do
let(:paperboy) { described_class.new(Wallet.new(0)) }
context 'when customer has enough money to pay' do
it 'add due amount to wallet' do
customer = double
allow(customer).to receive(:wallet).and_return(Wallet.new(10))
paperboy.receive_from(user, 5)
expect(paperboy.wallet.amount).to eq(5)
end
end
end
end
12. describe Paperboy do
describe '#receive_from' do
let(:paperboy) { described_class.new(Wallet.new(0)) }
context 'when customer has enough money to pay' do
it 'add due amount to wallet' do
customer = double
allow(customer).to receive(:wallet).and_return(Wallet.new(10))
paperboy.receive_from(user, 5)
expect(paperboy.wallet.amount).to eq(5)
end
end
end
end
13. describe Paperboy do
describe '#receive_from' do
let(:paperboy) { described_class.new(Wallet.new(0)) }
context 'when customer has enough money to pay' do
it 'add due amount to wallet' do
customer = double
allow(customer).to receive(:wallet).and_return(Wallet.new(10))
paperboy.receive_from(user, 5)
expect(paperboy.wallet.amount).to eq(5)
end
end
end
end
15. –Reek doumentation
“Feature Envy occurs when a code fragment
references another object more often than it
references itself, or when several clients do the
same series of manipulations on a particular type of
object.”
18. def associate_user
@order ||= current_order
if try_spree_current_user && @order
if @order.user.blank? || @order.email.blank?
@order.associate_user!(try_spree_current_user)
end
end
end
25. –Ruby Science
“The law restricts how deeply a method
can reach into another object’s dependency
graph, preventing any one method from
becoming tightly coupled to another
object’s structure.”
26. class Paperboy
attr_reader :wallet
def initialize(wallet)
@wallet = wallet
end
def receive_from(customer, amount)
if customer.wallet.try(:amount).to_f > amount
@wallet.amount = amount
customer.wallet = customer.wallet.amount - amount
else
nil
end
end
end
27. class Paperboy
attr_reader :wallet
def initialize(wallet)
@wallet = wallet
end
def receive_from(customer, amount)
self.wallet.add(customer.pay(amount))
end
end
29. –Martin Fowler
“Instead of returning null, or some
odd value, return a Special Case that
has the same interface as what the
caller expects.”
30. def associate_user
@order ||= current_order
if try_spree_current_user && @order
if @order.user.blank? || @order.email.blank?
@order.associate_user!(try_spree_current_user)
end
end
end