class EacRubyUtils::CustomFormat::String

Attributes

format[R]
string[R]

Public Class Methods

new(format, string) click to toggle source
# File lib/eac_ruby_utils/custom_format.rb, line 22
def initialize(format, string)
  @format = format
  @string = string
end

Public Instance Methods

mapping() click to toggle source
# File lib/eac_ruby_utils/custom_format.rb, line 27
def mapping
  @mapping ||= format.mapping.select do |k, _v|
    sequences.include?(k)
  end
end
sequences() click to toggle source
# File lib/eac_ruby_utils/custom_format.rb, line 33
def sequences
  @sequences ||= string.scan(SEQUENCE_PATTERN).map(&:first).uniq.map(&:to_sym)
end
source_object_value(object, method) click to toggle source
# File lib/eac_ruby_utils/custom_format.rb, line 37
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
with(source_object) click to toggle source
# File lib/eac_ruby_utils/custom_format.rb, line 44
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