Sha256: a620edd57f13e547b8aff245f576f5e873136536ca4afa6565e6f1766d221a10

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8
module SEPA
  module Converter
    def convert(*attributes, options)
      include InstanceMethods

      method_name = "convert_#{options[:to]}"
      raise ArgumentError.new("Converter '#{options[:to]}' does not exist!") unless InstanceMethods.method_defined?(method_name)

      attributes.each do |attribute|
        define_method "#{attribute}=" do |value|
          instance_variable_set("@#{attribute}", send(method_name, value))
        end
      end
    end

    module InstanceMethods
      def convert_text(value)
        return unless value

        text = value.to_s.
          # Convert german umlauts
          gsub('Ä', 'AE').
          gsub('Ü', 'UE').
          gsub('Ö', 'OE').
          gsub('ä', 'ae').
          gsub('ü', 'ue').
          gsub('ö', 'oe').
          gsub('ß', 'ss')

        I18n.transliterate(text).
          # Remove all invalid characters
          gsub(/[^a-zA-Z0-9\ \'\:\?\,\-\(\+\.\)\/]/, '').
          # Remove leading and trailing spaces
          strip
      end

      def convert_decimal(value)
        return unless value
        BigDecimal(value.to_s).round(2)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sepa_king-0.0.7 lib/sepa_king/converter.rb
sepa_king-0.0.6 lib/sepa_king/converter.rb
sepa_king-0.0.5 lib/sepa_king/converter.rb
sepa_king-0.0.4 lib/sepa_king/converter.rb