Sha256: 8085ac578fb688df71be09806e80ab29ac5129d512e10a25c65cc5093f26e7f5
Contents?: true
Size: 1.31 KB
Versions: 221
Compression:
Stored size: 1.31 KB
Contents
# frozen_string_literal: true module EacRubyUtils # A formatter like [String.sprintf]. class CustomFormat TYPE_PATTERN = /[a-zA-Z]/.freeze SEQUENCE_PATTERN = /(?<!%)%(#{TYPE_PATTERN})/.freeze attr_reader :mapping def initialize(mapping) @mapping = mapping.map { |k, v| [k.to_sym, v] }.to_h.freeze end def format(string) ::EacRubyUtils::CustomFormat::String.new(self, string) end class String attr_reader :format, :string def initialize(format, string) @format = format @string = string end def mapping @mapping ||= format.mapping.select do |k, _v| sequences.include?(k) end end def sequences @sequences ||= string.scan(SEQUENCE_PATTERN).map(&:first).uniq.map(&:to_sym) end def source_object_value(object, method) return object.send(method) if object.respond_to?(method) return object[method] if object.respond_to?('[]') raise ::ArgumentError, "Methods \"#{method}\" or \"[]\" not found for #{object}" end def with(source_object) r = string mapping.each do |key, method| r = r.gsub(/%#{::Regexp.quote(key)}/, source_object_value(source_object, method).to_s) end r.gsub('%%', '%') end end end end
Version data entries
221 entries across 221 versions & 3 rubygems