Sha256: 0021a7f17a597972a4b30c4b559e016c546fa6f11325282659dc95bc4adbdc1a

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

require 'numerals/conversions'
require 'flt'

module Numerals

  # Base class for Conversions of type with context
  class 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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
numerals-0.3.1 lib/numerals/conversions/context_conversion.rb
numerals-0.3.0 lib/numerals/conversions/context_conversion.rb
numerals-0.2.1 lib/numerals/conversions/context_conversion.rb