-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Make Spree::Money autoloadable #6040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Spree | ||
| # Spree::Money is a relatively thin wrapper around Monetize which handles | ||
| # formatting via Spree::Config. | ||
| class Money | ||
| include Comparable | ||
| DifferentCurrencyError = Class.new(StandardError) | ||
|
|
||
| class << self | ||
| attr_accessor :default_formatting_rules | ||
|
|
||
| def parse(amount, currency = Spree::Config[:currency]) | ||
| new(parse_to_money(amount, currency)) | ||
| end | ||
|
|
||
| # @api private | ||
| def parse_to_money(amount, currency) | ||
| ::Monetize.parse(amount, currency) | ||
| end | ||
| end | ||
| self.default_formatting_rules = { | ||
| # Ruby money currently has this as false, which is wrong for the vast | ||
| # majority of locales. | ||
| sign_before_symbol: true | ||
| } | ||
|
|
||
| attr_reader :money | ||
|
|
||
| delegate :cents, :currency, :to_d, :zero?, to: :money | ||
|
|
||
| # @param amount [Money, #to_s] the value of the money object | ||
| # @param options [Hash] the default options for formatting the money object See #format | ||
| def initialize(amount, options = {}) | ||
| if amount.is_a?(::Money) | ||
| @money = amount | ||
| else | ||
| currency = options[:currency] || Spree::Config[:currency] | ||
|
|
||
| @money = Monetize.from_string(amount, currency) | ||
| end | ||
| @options = Spree::Money.default_formatting_rules.merge(options) | ||
| end | ||
|
|
||
| # @return [String] the value of this money object formatted according to | ||
| # its options | ||
| def to_s | ||
| format | ||
| end | ||
|
|
||
| # @param options [Hash, String] the options for formatting the money object | ||
| # @option options [Boolean] with_currency when true, show the currency | ||
| # @option options [Boolean] no_cents when true, round to the closest dollar | ||
| # @option options [String] decimal_mark the mark for delimiting the | ||
| # decimals | ||
| # @option options [String, false, nil] thousands_separator the character to | ||
| # delimit powers of 1000, if one is desired, otherwise false or nil | ||
| # @option options [Boolean] sign_before_symbol when true the sign of the | ||
| # value comes before the currency symbol | ||
| # @option options [:before, :after] symbol_position the position of the | ||
| # currency symbol | ||
| # @return [String] the value of this money object formatted according to | ||
| # its options | ||
| def format(options = {}) | ||
| @money.format(@options.merge(options)) | ||
| end | ||
|
|
||
| # @note If you pass in options, ensure you pass in the { html_wrap: true } as well. | ||
| # @param options [Hash] additional formatting options | ||
| # @return [String] the value of this money object formatted according to | ||
| # its options and any additional options, by default with html_wrap. | ||
| def to_html(options = { html_wrap: true }) | ||
| output = format(options) | ||
| # Maintain compatibility by checking html option renamed to html_wrap. | ||
| if options[:html_wrap] | ||
| output = output.html_safe | ||
| end | ||
| output | ||
| end | ||
|
|
||
| # (see #to_s) | ||
| def as_json(*) | ||
| to_s | ||
| end | ||
|
|
||
| def <=>(other) | ||
| if !other.respond_to?(:money) | ||
| raise TypeError, "Can't compare #{other.class} to Spree::Money" | ||
| end | ||
| if currency != other.currency | ||
| # By default, ::Money will try to run a conversion on `other.money` and | ||
| # try a comparison on that. We do not want any currency conversion to | ||
| # take place so we'll catch this here and raise an error. | ||
| raise( | ||
| DifferentCurrencyError, | ||
| "Can't compare #{currency} with #{other.currency}" | ||
| ) | ||
| end | ||
| @money <=> other.money | ||
| end | ||
|
|
||
| # Delegates comparison to the internal ruby money instance. | ||
| # | ||
| # @see http://www.rubydoc.info/gems/money/Money/Arithmetic#%3D%3D-instance_method | ||
| def ==(other) | ||
| raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| @money == other.money | ||
| end | ||
|
|
||
| def -(other) | ||
| raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| self.class.new(@money - other.money) | ||
| end | ||
|
|
||
| def +(other) | ||
| raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| self.class.new(@money + other.money) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Spree | ||
| # Spree::Money is a relatively thin wrapper around Monetize which handles | ||
| # formatting via Spree::Config. | ||
| class Money | ||
| include Comparable | ||
| DifferentCurrencyError = Class.new(StandardError) | ||
|
|
||
| class <<self | ||
| attr_accessor :default_formatting_rules | ||
|
|
||
| def parse(amount, currency = Spree::Config[:currency]) | ||
| new(parse_to_money(amount, currency)) | ||
| end | ||
|
|
||
| # @api private | ||
| def parse_to_money(amount, currency) | ||
| ::Monetize.parse(amount, currency) | ||
| end | ||
| end | ||
| self.default_formatting_rules = { | ||
| # Ruby money currently has this as false, which is wrong for the vast | ||
| # majority of locales. | ||
| sign_before_symbol: true | ||
| } | ||
|
|
||
| attr_reader :money | ||
|
|
||
| delegate :cents, :currency, :to_d, :zero?, to: :money | ||
|
|
||
| # @param amount [Money, #to_s] the value of the money object | ||
| # @param options [Hash] the default options for formatting the money object See #format | ||
| def initialize(amount, options = {}) | ||
| if amount.is_a?(::Money) | ||
| @money = amount | ||
| else | ||
| currency = options[:currency] || Spree::Config[:currency] | ||
|
|
||
| @money = Monetize.from_string(amount, currency) | ||
| end | ||
| @options = Spree::Money.default_formatting_rules.merge(options) | ||
| end | ||
|
|
||
| # @return [String] the value of this money object formatted according to | ||
| # its options | ||
| def to_s | ||
| format | ||
| end | ||
|
|
||
| # @param options [Hash, String] the options for formatting the money object | ||
| # @option options [Boolean] with_currency when true, show the currency | ||
| # @option options [Boolean] no_cents when true, round to the closest dollar | ||
| # @option options [String] decimal_mark the mark for delimiting the | ||
| # decimals | ||
| # @option options [String, false, nil] thousands_separator the character to | ||
| # delimit powers of 1000, if one is desired, otherwise false or nil | ||
| # @option options [Boolean] sign_before_symbol when true the sign of the | ||
| # value comes before the currency symbol | ||
| # @option options [:before, :after] symbol_position the position of the | ||
| # currency symbol | ||
| # @return [String] the value of this money object formatted according to | ||
| # its options | ||
| def format(options = {}) | ||
| @money.format(@options.merge(options)) | ||
| end | ||
|
|
||
| # @note If you pass in options, ensure you pass in the { html_wrap: true } as well. | ||
| # @param options [Hash] additional formatting options | ||
| # @return [String] the value of this money object formatted according to | ||
| # its options and any additional options, by default with html_wrap. | ||
| def to_html(options = { html_wrap: true }) | ||
| output = format(options) | ||
| # Maintain compatibility by checking html option renamed to html_wrap. | ||
| if options[:html_wrap] | ||
| output = output.html_safe | ||
| end | ||
| output | ||
| end | ||
|
|
||
| # (see #to_s) | ||
| def as_json(*) | ||
| to_s | ||
| end | ||
|
|
||
| def <=>(other) | ||
| if !other.respond_to?(:money) | ||
| raise TypeError, "Can't compare #{other.class} to Spree::Money" | ||
| end | ||
| if currency != other.currency | ||
| # By default, ::Money will try to run a conversion on `other.money` and | ||
| # try a comparison on that. We do not want any currency conversion to | ||
| # take place so we'll catch this here and raise an error. | ||
| raise( | ||
| DifferentCurrencyError, | ||
| "Can't compare #{currency} with #{other.currency}" | ||
| ) | ||
| end | ||
| @money <=> other.money | ||
| end | ||
|
|
||
| # Delegates comparison to the internal ruby money instance. | ||
| # | ||
| # @see http://www.rubydoc.info/gems/money/Money/Arithmetic#%3D%3D-instance_method | ||
| def ==(other) | ||
| raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| @money == other.money | ||
| end | ||
|
|
||
| def -(other) | ||
| raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| self.class.new(@money - other.money) | ||
| end | ||
|
|
||
| def +(other) | ||
| raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) | ||
| self.class.new(@money + other.money) | ||
| end | ||
| end | ||
| end | ||
| Spree.deprecator.warn( | ||
| <<~MSG | ||
| The file "#{__FILE__}" does not need to be `require`d any longer, it is now autoloaded. | ||
| MSG | ||
| ) |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.