Skip to content

Adding ability to temporarily change the rounding mode#343

Merged
semmons99 merged 6 commits intoRubyMoney:masterfrom
hadees:temporarily_change_rounding_mode
Dec 20, 2013
Merged

Adding ability to temporarily change the rounding mode#343
semmons99 merged 6 commits intoRubyMoney:masterfrom
hadees:temporarily_change_rounding_mode

Conversation

@hadees
Copy link
Contributor

@hadees hadees commented Nov 27, 2013

I added the ability to temporarily change the rounding mode in a block passed to Money.rounding_mode. I needed this feature because I'm using Stripe as my payment processor and they use a different rounding mode then I am. So this makes it easier for me to estimate their fees.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see 2 issues around this:

  1. It's not thread-safe: if two threads call Money.rounding_mode then you may get inconsistent results.
  2. If the yield fails for any reason you may be left in an inconsistent state because you haven't set the rounding mode back. Suggest you use an ensure to set the rounding mode back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair points, I was also thinking maybe rounding_mode should return yield results instead of the current rounding mode because when you are temporarily changing it getting back the current mode isn't really that useful but getting back the results of a calculation would be.

What would be the right way to make it thread safe?

@semmons99
Copy link
Member

Closing, please reopen if you're actively working on this.

@semmons99 semmons99 closed this Dec 18, 2013
@hadees
Copy link
Contributor Author

hadees commented Dec 18, 2013

I'd like to work on it but I didn't get any other feedback about how best to make it thread safe.

…ck returns the result when being passed a rounding mode
@semmons99
Copy link
Member

@ohthatjames do you have more suggestions?

@hadees
Copy link
Contributor Author

hadees commented Dec 18, 2013

@semmons99 I've gone ahead and made changes in my branch that I think will make it thread safe. I can't seem to reopen the pull request though.

@hadees
Copy link
Contributor Author

hadees commented Dec 18, 2013

Should I just create a new pull request?

@semmons99 semmons99 reopened this Dec 18, 2013
@semmons99
Copy link
Member

@hadees reopened

@hadees
Copy link
Contributor Author

hadees commented Dec 18, 2013

I also want to update the YARD docs before this gets merged in but I'm not sure the right syntax to show it returns [BigDecimal::ROUND_MODE] or the results of the block.

@semmons99
Copy link
Member

I believe you're looking for @yard, @yardparam and @yardreturn http://rubydoc.info/gems/yard/file/docs/Tags.md#yield

@hadees
Copy link
Contributor Author

hadees commented Dec 20, 2013

That was my last update to the branch unless anyone has more suggestions.

@semmons99
Copy link
Member

looks good

semmons99 added a commit that referenced this pull request Dec 20, 2013
Adding ability to temporarily change the rounding mode
@semmons99 semmons99 merged commit 25a0b92 into RubyMoney:master Dec 20, 2013
@ohthatjames
Copy link
Member

Really sorry, I've been away and I missed the notifications on this one. Two observations:

  1. this doesn't allow for nested blocks (the following example fails):
it "allows nested blocks" do
  Money.rounding_mode(BigDecimal::ROUND_UP) do
    Money.rounding_mode(BigDecimal::ROUND_DOWN) do
      Money.new(1.9).fractional
    end.should == 1

    Money.new(1.1).fractional
  end.should == 2
end

This is caused by 32929d8#diff-a6d34f32282d8d431a585cdaf0aaf730R174 - we may have to make it an array and push and pop rounding modes.

  1. I've been thinking about this, and I can't really see the use-case for doing this on a global level. Would it be safer to allow the rounding mode to be overridden on each method that uses it? It adds a certain amount of complexity and finding bugs due to changing the rounding may be a bit annoying (action at a distance). Do you have any example code of how you intend to use this?

@ohthatjames
Copy link
Member

BTW, happy to look into the nested blocks issue if others think it's worth closing off.

@semmons99
Copy link
Member

@ohthatjames it would be nice to have the nested blocks issue fixed, but I don't think it's something that's high priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants