Sha256: c19e92e3fef7b0935365e21a70dcc1546c6c654095680f446cfe3c78f60d8b73

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "money"

Money.rounding_mode = BigDecimal::ROUND_HALF_UP

# Creates accessors for money values
module Sequel::Plugins::MoneyAccessors
  MoneyClassRequired = Class.new(StandardError)

  module ClassMethods
    # Setup money accessor
    #
    # @param amount_column [Symbol] amount column
    # @param currency_column [Symbol] currency column
    # @example
    #   class Order < Sequel::Model
    #     money_accessor :amount, :currency
    #   end
    #
    #   order = Order.create(amount: 200, currency: "EUR")
    #   order.amount # => #<Money fractional:20000.0 currency:RUB>
    #   order.currency # => "EUR"
    def money_accessor(amount_column, currency_column)
      money_getter(amount_column, currency_column)
      money_setter(amount_column, currency_column)
    end

    def money_getter(amount_column, currency_column)
      include_accessors_module!
      @_money_accessors_module.module_eval do
        define_method(amount_column) do
          amount, currency = super(), send(currency_column)
          return unless amount && currency
          Money.from_amount(amount.to_d, currency)
        end
      end
    end

    def money_setter(amount_column, currency_column)
      include_accessors_module!
      @_money_accessors_module.module_eval do
        define_method("#{amount_column}=") do |value|
          case value
          when Money
            amount = value.to_d
            currency = value.currency.to_s
          when nil
            amount = currency = nil
          else
            raise MoneyClassRequired, "#{amount_column} value must be either Money instance or nil"
          end

          super(amount)
          send("#{currency_column}=", currency)
        end
      end
    end

    private

    def include_accessors_module!
      return if defined?(@_money_accessors_module)
      @_money_accessors_module = Module.new
      include @_money_accessors_module
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.15.0.198 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.14.0.192 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.14.0.189 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.14.0.188 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.14.0.187 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.14.0 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.13.0.185 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.13.0.172 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.13.0 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.12.0.169 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.12.0 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.12.0.159 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.11.0.143 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.10.0.101 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.10.0.100 lib/sequel/plugins/money_accessors.rb
umbrellio-sequel-plugins-0.10.0.86 lib/sequel/plugins/money_accessors.rb