Sha256: c7ccba0850dd23a25e23f4833bd187a4f6c4023b1bbaf826eb52681b89797e6d

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'roman-numerals'

module Smerp
  module Common

    class NumeralToStringConversionException < StandardError; end

    # expect to integrate into integer class
    module NumeralToStringConversion

      def int_to_roman
        RomanNumerals.to_roman(self.to_i)
      end
      alias_method :int_to_roman_upcase, :int_to_roman

      def int_to_roman_downcase
        int_to_roman.downcase
      end


      def int_to_alpha

        input = self.to_i
        ins = []

        input = input * -1 if input < 0
        input = 1 if input == 0
          
        if input > 26
          l = input / 26
          (0...l).each do |ll|
            ins << 26
          end

          ins << input-(26*l)
        else
          ins << input
        end

        base = 65 # 'A' 
        v = self.to_i

        res = []
        ins.each do |input|
          val = base + input - 1
          res << val.chr
        end

        res.join

      end
      alias_method :int_to_alpha_upcase, :int_to_alpha

      def int_to_alpha_downcase
        int_to_alpha.downcase 
      end

    end

    # expected to be integrated into String class
    module StringToNumeralConversion
     
      def roman_to_int
        RomanNumerals.to_decimal(self) 
      end

      def alpha_to_int

        smallAlpha = ('a'..'z')
        capAlpha = ('A'..'Z')
        smallAlphaBase = 'a'.ord
        capAlphaBase = 'A'.ord
       
        res = 0
        input = self.chars
        input.each do |c|
          if smallAlpha.include?(c)
            v = c.ord - smallAlphaBase + 1
            res += v
          elsif capAlpha.include?(c)
            v = c.ord - capAlphaBase + 1
            res += v
          else
            res += 0
          end
        end

        res

      end
    end
    
  end
end


class Integer
  include Smerp::Common::NumeralToStringConversion
end

class String
  include Smerp::Common::StringToNumeralConversion
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smerp-common-0.1.1 lib/smerp/common/numeral_conversion.rb