class Numerals::ContextConversion

Base class for Conversions of type with context

Attributes

context[R]
input_rounding[R]
type[R]

Public Class Methods

new(context_or_type, options={}) click to toggle source
# File lib/numerals/conversions/context_conversion.rb, line 9
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

Public Instance Methods

input_rounding=(rounding) click to toggle source
# File lib/numerals/conversions/context_conversion.rb, line 24
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