lib/numerals/conversions/context_conversion.rb in numerals-0.2.0 vs lib/numerals/conversions/context_conversion.rb in numerals-0.2.1

- old
+ new

@@ -1,40 +1,44 @@ require 'numerals/conversions' require 'flt' -# Base class for Conversions of type with context -class Numerals::ContextConversion +module Numerals - 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" + # 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 - self.input_rounding = options[:input_rounding] - end - attr_reader :context, :type, :input_rounding + 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 + def input_rounding=(rounding) + if rounding + if rounding == :context + @input_rounding = Rounding[@context.rounding, precision: @context.precision, base: @context.radix] else - # The rounding precision is not meaningful for the destination type on input - @input_rounding = Rounding[rounding.mode, base: @context.radix] + 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 - else - @input_rounding = nil end + end end