Sha256: fbb66a26cfe37350799ac02427addaa7b0ef313f022c03e92a283c88eea3010b

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

require_relative 'literal_asis'
require_relative 'composite_o_expression'

module Zenlish
  module Inflect
    class Substitution < CompositeOExpression
      def initialize(theOriginal, aPattern, theReplacement)
        valid_original = validated_argument(theOriginal)
        valid_pattern = validated_pattern(aPattern)
        valid_replac = validated_argument(theReplacement)
        super([valid_original, valid_pattern, valid_replac])
      end

      def generate(headings, lexeme, values)
        original = children[0].generate(headings, lexeme, values)
        replacement = children[-1].generate(headings, lexeme, values)

        original.sub(pattern, replacement)
      end

      def pattern
        children[1]
      end

      private

      def validated_original(theOriginal)
        arg = nil
        if theOriginal.kind_of?(String)
          arg = LiteralAsIs.new(theOriginal)
        elsif theOriginal.kind_of?(OutputExpression)
          arg = theOriginal
        else
          err_msg = "Expected an OutputExpression, found '#{theOriginal.inspect}' instead."
          raise StandardError, err_msg
        end

        arg
      end

      def validated_pattern(aPattern)
        unless aPattern.kind_of?(Regexp)
          err_msg = "Expected a Regexp, found #{aPattern.inspect}."
          raise StandardError, err_msg
        end

        aPattern
      end

      def validated_argument(anArgument)
        arg = nil
        if anArgument.kind_of?(String)
          arg = LiteralAsIs.new(anArgument)
        elsif anArgument.kind_of?(OutputExpression)
          arg = anArgument
        else
          err_msg = "Expected an OutputExpression, found #{anArgument.inspect}."
          raise StandardError, err_msg
        end

        arg
      end
    end # class
  end # module
end # module

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zenlish-0.2.05 lib/zenlish/inflect/substitution.rb
zenlish-0.2.04 lib/zenlish/inflect/substitution.rb
zenlish-0.2.03 lib/zenlish/inflect/substitution.rb
zenlish-0.2.02 lib/zenlish/inflect/substitution.rb
zenlish-0.2.01 lib/zenlish/inflect/substitution.rb
zenlish-0.2.00 lib/zenlish/inflect/substitution.rb