Sha256: a49932dbfdae88b86c1b41e9a21f97501e5e8984e366c6feecbb2be3cf7d7ea1

Contents?: true

Size: 773 Bytes

Versions: 2

Compression:

Stored size: 773 Bytes

Contents

require "galaxy_converter/roman_constraint"
require "galaxy_converter/roman_rule"

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

      def initialize(value, constraint = Constraint, rule = Rule)
        @value = value.to_s.upcase
        @constraint = constraint
        @rule = rule
      end

      def to_s
        @value
      end

      def to_i
        @rule.call(@value).chars.reduce(0) do |total, symbol|
          total += SYMBOLS.fetch(symbol, 0)
        end
      end

      def valid?
        return false if @value.empty?
        !@constraint.violated?(@value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
galaxy_converter-2.1.2 lib/galaxy_converter/roman_numeral.rb
galaxy_converter-2.1.1 lib/galaxy_converter/roman_numeral.rb