default argument value & nil guard

suppose we have this following method, which we call up to 3 times for 3 different types of policy.

  def return_policy_link(type, country)
    policy_urls ||= Cherie::TermsAndConditions.get_policy_urls(country.upcase)
    custom_url(policy_urls[type])
  end

But what if country is “dynamic”?
As in: return_policy_link(“terms”, cookies[:country_preference])
cookies[:country_preference] is updated according to the user’s selection, browser, etc…

anyway, point is, we don’t know what country could be…

Let’s update our country argument to default to “US”, just in case country is missing:

  def return_policy_link(type, country="US")
    policy_urls ||= Cherie::TermsAndConditions.get_policy_urls(country.upcase)
    custom_url(policy_urls[type])
  end

kay, is that good enough?

Let’s run our rspec!

    describe "#return_policy_link" do
      before :each do
        us_links = {"electronicDisclosure" => "pages/electronic_communication", "terms" => "pages/terms", "privacy" => "pages/privacy"}
        Cherie::TermsAndConditions.stub(:get_policy_urls).with("US").and_return(us_links)
      end

      context "get terms policy link for US" do
        it "returns the right url" do
          controller.return_policy_link("terms", "US").should include("pages/terms")
        end
      end
      context "get terms policy link for missing country" do
        it "returns the terms link for US" do
          controller.return_policy_link("terms").should include("pages/terms")
        end
      end
    end

so far so good, but what if country is nil?

   context "get terms policy link when country is nil" do
     it "returns the terms link for US" do
       controller.return_policy_link("terms", nil).should include("pages/terms")
     end
   end

rspec fails ):

we need a nil guard and still default to “US”

  def return_policy_link(type, country = "US")
    country ||= "US"
    policy_urls ||= Cherie::TermsAndConditions.get_policy_urls(country.upcase)
    custom_url(policy_urls[type])
  end

alrighty, now all the tests pass (:

Hmm… can we refactor rspec?

    describe "#return_policy_link" do
      let(:terms_link) {{"terms" => "pages/terms"}}

      before :each do
        Cherie::TermsAndConditions.stub(:get_policy_urls).and_return(terms_link)
      end

      context "returns the expected terms link with type as terms and country" do
        it "is US" do
          controller.return_policy_link("terms", "US").should include(terms_link["terms"])
        end
        it "is missing" do
          controller.return_policy_link("terms").should include(terms_link["terms"])
        end
        it "is nil" do
          controller.return_policy_link("terms", nil).should include(terms_link["terms"])
        end
      end

not sure if I am really digging the new description, even tho it shows up nicely as:
#return_policy_link returns the expected terms link when type is terms and country is US
what do you think?

Advertisements

would you like to leave a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s