From 96d9dfd0e8937a707889db121f8ca551d91182ad Mon Sep 17 00:00:00 2001 From: milobeyene Date: Thu, 8 Mar 2018 13:08:30 -0600 Subject: [PATCH] Finished homework assignment: excluding extra credit --- Gemfile.lock | 1 + lib/desugaring.rb | 37 +++++++++++++++++++++++++-------- lib/retail_transaction.rb | 5 +++++ test/retail_transaction_test.rb | 32 ++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8c1dbb3..858d89f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,6 +23,7 @@ GEM PLATFORMS ruby + x64-mingw32 DEPENDENCIES aasm! diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 8d52908..eaf1922 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -21,7 +21,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 @@ -36,7 +38,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 @@ -51,7 +55,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 @@ -70,7 +76,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 @@ -88,7 +96,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 @@ -109,8 +119,13 @@ def desugared_interpolation(recipients, event, message) # Copy the contents of the previous method here and remove this sugar. # (Think: which names are local variables, and which are not?) # + + #method parameters: recipients, event, message + #block parameters: |x| 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 @@ -131,7 +146,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 @@ -147,13 +164,15 @@ def desugared_implicit_parens(recipients, event, message) # # Copy the contents of the previous method here and remove this sugar. # - # P.S. This one gets messy! Use line breaks and indentation to help. + # P.S. This one gets messy! Use line breaks and indentation to help. -- I didn't do this because I didn't feel like it was necessary! # P.P.S. Note that whitespace is syntactic sugar too. # P.P.P.S. For full credit on this one, note that addition is left-associative: the things on the left # 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. diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index 1bfa0e3..25bed97 100644 --- a/lib/retail_transaction.rb +++ b/lib/retail_transaction.rb @@ -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, @@ -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 diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index 823cbfe..e6c41ac 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -102,6 +102,10 @@ assert_equal false, tx.processing_payment? assert_equal false, tx.settled? assert_equal true, tx.payment_declined? + end + + it "cannot refund before payment has been authorized or declined" do + assert_invalid_transition { tx.refund! } end end @@ -137,6 +141,10 @@ tx.process_payment! assert_equal false, tx.payment_declined? assert_equal true, tx.processing_payment? + end + + it "cannot refund declined payment" do + assert_invalid_transition { tx.refund! } end end @@ -151,6 +159,30 @@ it "cannot be reopened" do assert_invalid_transition { tx.reopen! } + end + + it "can be refunded" do + tx.refund! + assert_equal true, tx.refunded? + end + end + + describe "that is refunded" do + before(:each) do + tx.add_item("bobcat") + tx.check_out! + tx.payment_info = "15 cents and a nail" + tx.process_payment! + tx.payment_authorized! + tx.refund! + end + + it "cannot be refunded a second time" do + assert_invalid_transition { tx.refund! } + end + + it "cannot be reopened" do + assert_invalid_transition { tx.reopen! } end end end