Class: SvelteOnRails::Lib::ToSvelteTranslations

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/lib/to_svelte_translations.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeToSvelteTranslations

Returns a new instance of ToSvelteTranslations.



13
14
15
16
17
# File 'lib/svelte_on_rails/lib/to_svelte_translations.rb', line 13

def initialize
  @config = SvelteOnRails::Configuration.instance
  @cached_translations = {}
  @all_cached_translations = {}
end

Class Method Details

.instanceObject



4
5
6
7
8
9
10
11
# File 'lib/svelte_on_rails/lib/to_svelte_translations.rb', line 4

def self.instance
  @instance ||= new
  if SvelteOnRails::Configuration.instance.watch_changes?
    @cached_translations = {}
    @all_cached_translations = {}
  end
  @instance
end

Instance Method Details

#all_cached_translations(language_key, config_key, models, own_model) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/svelte_on_rails/lib/to_svelte_translations.rb', line 19

def all_cached_translations(language_key, config_key, models, own_model)
  k = "#{language_key}-#{config_key}-#{models}".hash
  @all_cached_translations[k] ||=
    models.each_with_object({}) do |model, memo|
      k = own_model == model ? "_translations" : "_#{model.name.first.downcase + model.name[1..-1]}Translations"
      memo[k] = cached_translations(language_key, config_key, model)
    end
end

#cached_translations(language_key, config_key, model) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/svelte_on_rails/lib/to_svelte_translations.rb', line 28

def cached_translations(language_key, config_key, model)
  k = "#{language_key}-#{config_key}-#{model.class.name}".hash
  @cached_translations[k] ||= begin
                                config = @config.configs[config_key]
                                if config
                                  source = config[:translations]
                                  if source
                                    calc(source, language_key, model)
                                  end
                                end
                              end
end