Sha256: adce9a40975d41c12f4a34e47b0cb058c5b0c33bdeed599430a62d00a0565488

Contents?: true

Size: 605 Bytes

Versions: 6

Compression:

Stored size: 605 Bytes

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string/output_safety'

module HtmlFormat
  def format(**rules)
    html_wrap = rules.delete(:html_wrap)
    return html_wrap(super(**rules)) if html_wrap

    super(**rules)
  end

  def as_html(**rules)
    rules[:html_wrap] = true
    format(**rules).html_safe
  end

  private

  def html_wrap(str)
    "<span class=\"money #{direction_class}\">#{str}</span>"
  end

  def direction_class
    if cents > 0
      'positive'
    elsif cents < 0
      'negative'
    else
      'zero'
    end
  end
end

class Money
  prepend HtmlFormat
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
money_extensions-1.5.0 lib/money_extensions/html_format.rb
money_extensions-1.4.0 lib/money_extensions/html_format.rb
money_extensions-1.3.0 lib/money_extensions/html_format.rb
money_extensions-1.2.0 lib/money_extensions/html_format.rb
money_extensions-1.1.0 lib/money_extensions/html_format.rb
money_extensions-1.0.0 lib/money_extensions/html_format.rb