Sha256: e77f605948c6d63d40b6948f0bc582e6890d290a4cb3969768ce76f9029e6687

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8

require 'money'

module Spree
  class Money
    class <<self
      attr_accessor :default_formatting_rules
    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: :money

    def initialize(amount, options={})
      @money = Monetize.parse([amount, (options[:currency] || Spree::Config[:currency])].join)
      @options = Spree::Money.default_formatting_rules.merge(options)
    end

    def amount_in_cents
      (cents / currency.subunit_to_unit.to_f * 100).round
    end

    def to_s
      @money.format(@options)
    end

    def to_html(options = { html: true })
      output = @money.format(@options.merge(options))
      if options[:html]
        # 1) prevent blank, breaking spaces
        # 2) prevent escaping of HTML character entities
        output = output.sub(" ", "&nbsp;").html_safe
      end
      output
    end

    def as_json(*)
      to_s
    end

    def decimal_mark
      return @money.decimal_mark if @options[:decimal_mark].nil?
      @options[:decimal_mark]
    end

    def thousands_separator
      return @money.thousands_separator if @options[:thousands_separator].nil?
      @options[:thousands_separator]
    end

    def ==(obj)
      @money == obj.money
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
goca-spree-core-3.1.14.rails.5.0.1 lib/spree/money.rb
goca-spree-core-3.1.14.rails.5.0 lib/spree/money.rb
goca-spree-core-3.1.15.rails.5.0 lib/spree/money.rb
goca-spree-core-3.1.15.pre.rails.pre.5.0 lib/spree/money.rb
spree_core-3.2.9 lib/spree/money.rb
spree_core-3.1.14 lib/spree/money.rb
spree_core-3.3.6 lib/spree/money.rb
spree_core-3.2.8 lib/spree/money.rb
spree_core-3.1.13 lib/spree/money.rb