From 398b56ecd72d5cde243c06e7217882bdd341695f Mon Sep 17 00:00:00 2001 From: Tara Date: Wed, 7 Mar 2018 01:29:51 -0600 Subject: [PATCH 1/4] Part 1 --- lib/retail_transaction.rb | 5 ++++ test/retail_transaction_test.rb | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index 1bfa0e3..5bb48d6 100644 --- a/lib/retail_transaction.rb +++ b/lib/retail_transaction.rb @@ -39,6 +39,11 @@ def paid? state :processing_payment state :payment_declined state :settled + state :refunded + + event :refund do + transitions from: :settled, to: :refunded + end event :check_out do transitions from: :ringing_up, to: :collecting_payment, diff --git a/test/retail_transaction_test.rb b/test/retail_transaction_test.rb index 823cbfe..a1aa84c 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -103,6 +103,12 @@ assert_equal false, tx.settled? assert_equal true, tx.payment_declined? end + + it "cannot be refunded yet" do + assert_raises do + tx.refund! + end + end end describe "with declined payment" do @@ -138,6 +144,12 @@ assert_equal false, tx.payment_declined? assert_equal true, tx.processing_payment? end + + it "cannot be refunded yet" do + assert_raises do + tx.refund! + end + end end describe "that is settled" do @@ -152,5 +164,40 @@ it "cannot be reopened" do assert_invalid_transition { tx.reopen! } end + + it "can be refunded" do + tx.refund! + assert_equal false, tx.settled? + assert_equal true, tx.refunded? + end + end + + describe "after refund" do + before(:each) do + tx.add_item("12lb bag of gummy bears") + tx.check_out! + tx.payment_info = "Laundry fund" + tx.process_payment! + tx.payment_authorized! + tx.refund! + end + + it "doesn't accept new payment info" do + assert_raises do + tx.payment_info = "Snax Account" + end + end + + it "can't be resubmitted" do + assert_raises do + tx.process_payment! + end + end + + it "can't be refunded again" do + assert_raises do + tx.refund! + end + end end end From e5e4dd1608d1216ee0977bbef5a9dbc2134b81cb Mon Sep 17 00:00:00 2001 From: Tara Date: Wed, 7 Mar 2018 01:30:10 -0600 Subject: [PATCH 2/4] Part 2 I think --- lib/desugaring.rb | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 8d52908..284eab5 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 + " on " + event.date}) end # Ruby tracks local variables lexically at compile time. Wherever you say `x = y`, the compiler assumes that @@ -110,7 +120,10 @@ 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! + # I tried this.mail() but it was an error so idk what that's about + mail(message, + {:to => recipients.map{|x| x.email}, + :subject => "You’re invited to " + event.title + " on " + event.date}) end # In Ruby, unlike Python, there are no properties distinct from method calls. When you say `x.y`, you are @@ -131,7 +144,9 @@ def desugared_implicit_self(recipients, event, message) # but structurally quite similar! # def desugared_implicit_parens(recipients, event, message) - implement_me! + mail(message, + {:to => recipients.map{|x| x.email()}, + :subject => "You’re invited to " + event.title() + " on " + event.date()}) end # In Ruby, every value is an object and every action is a method call. That includes operators. A binary @@ -153,7 +168,9 @@ 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! + mail(message, + {:to => recipients.map{|x| x.email()}, + :subject => "You’re invited to ".+(event.title()).+(" on ").+(event.date())}) end # Compare that to the version at the top. From e98d877c72323c396cdf9056ccb457e98bc04bdb Mon Sep 17 00:00:00 2001 From: Tara Date: Wed, 7 Mar 2018 01:30:16 -0600 Subject: [PATCH 3/4] Part 3 --- lib/door.rb | 61 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/lib/door.rb b/lib/door.rb index 0f99a74..0ade1f4 100644 --- a/lib/door.rb +++ b/lib/door.rb @@ -16,29 +16,46 @@ class Door end end - aasm :deadbolt_lock, namespace: :deadbolt do - state :unlocked, initial: true - state :locked - - event :lock_deadbolt do - transitions to: :locked - end - - event :unlock_deadbolt do - transitions to: :unlocked + # I tried adding a method to do this to Class without success + # But this works too + ["deadbolt", "knob"].each do |name| + aasm "#{name}_lock".to_sym, namespace: name.to_sym do + state :unlocked, initial: true + state :locked + + event "lock_#{name}".to_sym do + transitions to: :locked + end + + event "unlock_#{name}".to_sym do + transitions to: :unlocked + end end end - aasm :knob_lock, namespace: :knob do - state :unlocked, initial: true - state :locked - - event :lock_knob do - transitions to: :locked - end - - event :unlock_knob do - transitions to: :unlocked - end - end + # aasm :deadbolt_lock, namespace: :deadbolt do + # state :unlocked, initial: true + # state :locked + # + # event :lock_deadbolt do + # transitions to: :locked + # end + # + # event :unlock_deadbolt do + # transitions to: :unlocked + # end + # end + # + # aasm :knob_lock, namespace: :knob do + # state :unlocked, initial: true + # state :locked + # + # event :lock_knob do + # transitions to: :locked + # end + # + # event :unlock_knob do + # transitions to: :unlocked + # end + # end end From 5d8ed7cc016d2b98904111042b43dd45cba984fc Mon Sep 17 00:00:00 2001 From: Tara Date: Thu, 8 Mar 2018 23:37:28 -0600 Subject: [PATCH 4/4] Fixed Part 2 --- lib/desugaring.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 284eab5..8965330 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -40,7 +40,7 @@ def desugared_poetry(recipients, event, message) def desugared_named_args(recipients, event, message) mail(message, {to: recipients.map(&:email), - subject: "You’re invited to #{event.title} on #{event.date}"}) + 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 @@ -120,8 +120,8 @@ def desugared_interpolation(recipients, event, message) # (Think: which names are local variables, and which are not?) # def desugared_implicit_self(recipients, event, message) - # I tried this.mail() but it was an error so idk what that's about - mail(message, + # I had some trouble here because I instinctively kept writing this.mail() and wasn't sure why it wasn't working + self.mail(message, {:to => recipients.map{|x| x.email}, :subject => "You’re invited to " + event.title + " on " + event.date}) end @@ -144,7 +144,7 @@ def desugared_implicit_self(recipients, event, message) # but structurally quite similar! # def desugared_implicit_parens(recipients, event, message) - mail(message, + self.mail(message, {:to => recipients.map{|x| x.email()}, :subject => "You’re invited to " + event.title() + " on " + event.date()}) end @@ -168,7 +168,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) - mail(message, + self.mail(message, {:to => recipients.map{|x| x.email()}, :subject => "You’re invited to ".+(event.title()).+(" on ").+(event.date())}) end