Sha256: 12107522800fce0d622113b20c0117abe6b1b82a1b705d265ce23f47c204dc0e

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'numerals/conversions'
require 'flt'

# Base class for Conversions of type with context
class Numerals::ContextConversion

  def initialize(context_or_type, options={})
    if Class === context_or_type && context_or_type.respond_to?(:context)
      @type = context_or_type
      @context = @type.context
    elsif context_or_type.respond_to?(:num_class)
      @context = context_or_type
      @type = @context.num_class
    else
      raise "Invalid Conversion definition"
    end
    self.input_rounding = options[:input_rounding]
  end

  attr_reader :context, :type, :input_rounding

  def input_rounding=(rounding)
    if rounding
      if rounding == :context
        @input_rounding = Rounding[@context.rounding, precision: @context.precision, base: @context.radix]
      else
        rounding = Rounding[base: @context.radix].set!(rounding)
        if rounding.base == @context.radix
          @input_rounding = rounding
        else
          # The rounding precision is not meaningful for the destination type on input
          @input_rounding = Rounding[rounding.mode, base: @context.radix]
        end
      end
    else
      @input_rounding = nil
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
numerals-0.2.0 lib/numerals/conversions/context_conversion.rb
numerals-0.1.0 lib/numerals/conversions/context_conversion.rb