Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added 1.3.2'
Empty file.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GEM

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
aasm!
Expand Down
36 changes: 26 additions & 10 deletions lib/desugaring.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module DesugaringExercises

# ’ not the same as '

def all_the_sugar(recipients, event, message)
mail message,
to: recipients.map(&:email),
subject: "You’re invited to #{event.title} on #{event.date}"
to: recipients.map(&:email),
subject: "You’re invited to #{event.title} on #{event.date}"
end

# Ruby allows you to pass arguments to a method without using parentheses. Ruby programmers lovingly
Expand All @@ -21,7 +23,9 @@ def all_the_sugar(recipients, event, message)
# Copy the contents of the previous method here and remove this sugar.
#
def desugared_poetry(recipients, event, message)
implement_me!
mail(message,
to: recipients.map(&:email),
subject: "You’re invited to #{event.title} on #{event.date}")
end

# Ruby allows you to pass arguments identified by name instead of just by position. They are really just
Expand All @@ -36,7 +40,9 @@ def desugared_poetry(recipients, event, message)
# Copy the contents of the previous method here and remove this sugar.
#
def desugared_named_args(recipients, event, message)
implement_me!
mail(message,
{to: recipients.map(&:email),
subject: "You’re invited to #{event.title} on #{event.date}"})
end

# Ruby’s general syntax for hashes is `{key => value, key => value, ...}`. Because it is so common to use
Expand All @@ -51,7 +57,9 @@ def desugared_named_args(recipients, event, message)
# Copy the contents of the previous method here and remove this sugar.
#
def desugared_symbol_keys(recipients, event, message)
implement_me!
mail(message,
{:to => recipients.map(&:email),
:subject => "You’re invited to #{event.title} on #{event.date}"})
end

# You may be wondering how `map(&:email)` works. When you precede the last argument of a method call with
Expand All @@ -70,7 +78,9 @@ def desugared_symbol_keys(recipients, event, message)
# Copy the contents of the previous method here and remove this sugar.
#
def desugared_attr_proc(recipients, event, message)
implement_me!
mail(message,
{:to => recipients.map{ |x| x.email },
:subject => "You’re invited to #{event.title} on #{event.date}"})
end

# You may recall from the Ruby koans that when you put `#{something}` in a `"`-delimited string, Ruby will
Expand All @@ -88,7 +98,9 @@ def desugared_attr_proc(recipients, event, message)
# Copy the contents of the previous method here and remove this sugar.
#
def desugared_interpolation(recipients, event, message)
implement_me!
mail(message,
{:to => recipients.map{ |x| x.email },
:subject => "You’re invited to " + event.title.to_s + " on " + event.date.to_s})
end

# Ruby tracks local variables lexically at compile time. Wherever you say `x = y`, the compiler assumes that
Expand All @@ -110,7 +122,9 @@ def desugared_interpolation(recipients, event, message)
# (Think: which names are local variables, and which are not?)
#
def desugared_implicit_self(recipients, event, message)
implement_me!
self.mail(message,
{:to => recipients.map{ |x| x.email },
:subject => "You’re invited to " + event.title.to_s + " on " + event.date.to_s})
end

# In Ruby, unlike Python, there are no properties distinct from method calls. When you say `x.y`, you are
Expand All @@ -131,7 +145,9 @@ def desugared_implicit_self(recipients, event, message)
# but structurally quite similar!
#
def desugared_implicit_parens(recipients, event, message)
implement_me!
self.mail(message,
{:to => recipients.map(){ |x| x.email() },
:subject => "You’re invited to " + event.title().to_s() + " on " + event.date().to_s()})
end

# In Ruby, every value is an object and every action is a method call. That includes operators. A binary
Expand All @@ -153,7 +169,7 @@ def desugared_implicit_parens(recipients, event, message)
# get added before the things on the right. (a + b + c) means ((a + b) + c), NOT (a + (b + c)).
#
def desugared_operators(recipients, event, message)
implement_me!
self.mail(message,{:to => recipients.map(){ |x| x.email() }, :subject => ((("You’re invited to ".+(event.title.to_s())).+(' on ')).+(event.date.to_s()))})
end

# Compare that to the version at the top.
Expand Down
5 changes: 5 additions & 0 deletions lib/retail_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def paid?
state :processing_payment
state :payment_declined
state :settled
state :refunded

event :check_out do
transitions from: :ringing_up, to: :collecting_payment,
Expand All @@ -61,5 +62,9 @@ def paid?
event :payment_declined do
transitions from: :processing_payment, to: :payment_declined
end

event :refund do
transitions from: :settled, to: :refunded
end
end
end
38 changes: 38 additions & 0 deletions test/retail_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
assert_equal false, tx.settled?
assert_equal true, tx.payment_declined?
end

# No. 2 - ensure can't be refunded
it "cannot be refunded" do
assert_invalid_transition{ tx.refund! }
end
end

describe "with declined payment" do
Expand All @@ -114,6 +119,11 @@
tx.payment_declined!
end

# No. 2 - ensure can't be refunded
it "cannot be refunded" do
assert_invalid_transition{ tx.refund! }
end

it "cannot add more items" do
assert_raises do
tx.add_item("half a slice of bologna")
Expand Down Expand Up @@ -149,8 +159,36 @@
tx.payment_authorized!
end

# Settled cannot be reopened
it "cannot be reopened" do
assert_invalid_transition { tx.reopen! }
end

# No. 1: check if can be refunded?
it "can be refunded" do
tx.refund!
assert_equal false, tx.settled?
assert_equal true, tx.refunded?
end
end

describe "refunded" do # No 3. describe group for orders already refunded/in refund state
# No. 4 Cannot be refunded for second time
it "cannot be refunded a second time" do
assert_invalid_transition {tx. refund! }
end

# No. 5 Refunded cannot be reopened, same as settled
it "cannot be reopened" do
assert_invalid_transition { tx.reopen! }
end

end

# Refund tests
# 1. Test that ensures a settled order can be refunded
# 2. Add tests to one or two other states that ensure they cannot be refunded
# 3. New describe group for orders that are already refunded
# 4. Test transactions cannot be refunded a second time
# 5. Test a refunded order cannot be reopened
end
5 changes: 3 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

# To see full test names when running tests:
#
# require "minitest/reporters"
# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
# only helpful for retail transaction tests/dedbolt
#require "minitest/reporters"
#Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

module Minitest::Assertions
def assert_invalid_transition
Expand Down