class PlaceholderPath
Public Class Methods
new(path_definition)
click to toggle source
# File lib/toptranslation_cli/placeholder_path.rb, line 4 def initialize(path_definition) @path_definition = path_definition end
Public Instance Methods
for_path(path, locale_code)
click to toggle source
Returns a filepath with {locale_code} placeholder e.g. for the parameters path: “/locales/de/admin/index.de.po” path_definition: “/locales/{locale_code}/*/{locale_code}.po” locale_code: “de” it will return: “/locales/{locale_code}/admin/index.{locale_code}.po”
# File lib/toptranslation_cli/placeholder_path.rb, line 13 def for_path(path, locale_code) regex = regex(locale_code) path.match(regex).captures.join('{locale_code}') end
Private Instance Methods
regex(locale_code)
click to toggle source
Replaces UNIX wildcards in a path definition and returns a regular expression of the path definition e.g. “/config/*/{locale_code}.po” => //config/.*de.po/
(1) - Replaces ** and * wildcards with .* (2) - Replaces duplicate wildcards like ./. with one .* (3) - splits path_definition at {locale_code} (4) - Puts each part of splits in parantesis and joins them with locale_code
# File lib/toptranslation_cli/placeholder_path.rb, line 28 def regex(locale_code) string = @path_definition string = string.gsub(/\./, '\.') string = string.gsub(/((?<!\*)\*(?!\*))|(\*\*)/, '.*') # (1) string = string.gsub('.*/.*', '.*') # (2) splits = string.split('{locale_code}') # (3) path = splits.map { |segment| "(#{segment})" }.join(locale_code) # (4) Regexp.new(path, Regexp::IGNORECASE) end