class Decidim::TermCustomizer::TranslationStore

Public Class Methods

new(hash) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 6
def initialize(hash)
  @values = flat_hash(hash || {})
end

Public Instance Methods

by_key(search) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 14
def by_key(search)
  @values.select do |key|
    includes_string?(key, search, case_sensitive: true)
  end
end
by_term(search, case_sensitive: false) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 20
def by_term(search, case_sensitive: false)
  @values.select do |_key, term|
    includes_string?(term, search, case_sensitive: case_sensitive)
  end
end
term(key) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 10
def term(key)
  @values[key]
end

Private Instance Methods

append_to_hash(hash, key, value) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 46
def append_to_hash(hash, key, value)
  # Specific translations have a proc value but in our context these
  # translations are not interesting. An example of such translation is
  # `i18n.plural.rule` which takes in an integer when called and returns
  # either `:one` or `:other` depending on the integer's value.
  return if value.is_a?(Proc)

  # Some translation values are symbols which are also not interesting in
  # our context. Example of such translation is `i18n.plural.rule` which
  # has a value of `[:one, :other]`.
  return if value.is_a?(Symbol)

  # Exclude the faker translations.
  return if key =~ /^faker\./

  if value.is_a?(Array)
    value.each_with_index do |v, i|
      append_to_hash(hash, "#{key}.#{i}", v)
    end
  else
    hash[key] = value.to_s
  end
end
flat_hash(hash) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 34
def flat_hash(hash)
  hash.each_with_object({}) do |(k, v), h|
    if v.is_a? Hash
      flat_hash(v).map do |h_k, h_v|
        append_to_hash(h, "#{k}.#{h_k}", h_v)
      end
    else
      append_to_hash(h, k.to_s, v)
    end
  end
end
includes_string?(source, search, case_sensitive: false) click to toggle source
# File lib/decidim/term_customizer/translation_store.rb, line 28
def includes_string?(source, search, case_sensitive: false)
  return source.include?(search) if case_sensitive

  source.downcase.include?(search.downcase)
end