diff --git a/lib/desugaring.rb b/lib/desugaring.rb index 8d52908..bd9f05e 100644 --- a/lib/desugaring.rb +++ b/lib/desugaring.rb @@ -21,7 +21,10 @@ 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 +39,10 @@ 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 +57,10 @@ 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 +79,10 @@ 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,9 +100,14 @@ 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 + # :subject => "You’re invited to #{event.title} on #{event.date}"} + # Ruby tracks local variables lexically at compile time. Wherever you say `x = y`, the compiler assumes that # `x` is a local variable, and implicitly declares it if it’s not already declared. You also create a local # variable when you make a method parameter or block parameter. @@ -110,7 +127,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! + 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 +151,10 @@ 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 @@ -153,9 +176,27 @@ 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 + # =>, block {}, space in string, |x| + # Compare that to the version at the top. # # Languages designers argue a lot -- excessively, perhaps -- about how much sugar is too much. diff --git a/lib/door.rb b/lib/door.rb index 0f99a74..8d9c534 100644 --- a/lib/door.rb +++ b/lib/door.rb @@ -16,29 +16,18 @@ 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 - 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 + for i in ["deadbolt", "knob"] + aasm (i + "_lock").to_sym, namespace: i.to_sym do + state :unlocked, initial: true + state :locked + + event "lock_".concat(i).to_sym do + transitions to: :locked + end + + event "unlock_".concat(i).to_sym do + transitions to: :unlocked + end end end end diff --git a/lib/retail_transaction.rb b/lib/retail_transaction.rb index 1bfa0e3..545e1f0 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..61eb9bb 100644 --- a/test/retail_transaction_test.rb +++ b/test/retail_transaction_test.rb @@ -103,6 +103,10 @@ assert_equal false, tx.settled? assert_equal true, tx.payment_declined? end + + it "cannot be refunded" do + assert_invalid_transition{tx.refund!} + end end describe "with declined payment" do @@ -138,6 +142,11 @@ assert_equal false, tx.payment_declined? assert_equal true, tx.processing_payment? end + + it "cannot be refunded" do + assert_invalid_transition{tx.refund!} + end + end describe "that is settled" do @@ -152,5 +161,31 @@ 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 "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 diff --git a/test/test_helper.rb b/test/test_helper.rb index a343433..383ebf7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,8 +8,8 @@ # To see full test names when running tests: # -# require "minitest/reporters" -# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require "minitest/reporters" +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new module Minitest::Assertions def assert_invalid_transition