Sha256: 8be3846f23f1c7c05ee2056ba8d6a3ad454aabe4295d306b30ff380c878c7284

Contents?: true

Size: 676 Bytes

Versions: 1

Compression:

Stored size: 676 Bytes

Contents

require "galaxy_converter/constraint"
require "galaxy_converter/stretcher"

module GalaxyConverter
  class RomanNumeral
    SYMBOLS = {
      "M" => 1000,
      "D" => 500,
      "C" => 100,
      "L" => 50,
      "X" => 10,
      "V" => 5,
      "I" => 1
    }

    def initialize(value, constraint = Constraint, stretcher = Stretcher)
      @value = value.to_s.upcase
      @constraint = constraint
      @stretcher = stretcher
    end

    def to_s
      @value
    end

    def to_i
      return 0 unless valid?
      @stretcher.call(@value).chars.sum { |symbol| SYMBOLS.fetch(symbol, 0) }
    end

    private def valid?
      !@constraint.call(@value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
galaxy_converter-3.0.1 lib/galaxy_converter/roman_numeral.rb