Class: Rack::I18nRoutes::AliasMappingUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/i18n_routes/alias_mapping_updater.rb

Overview

An AliasMapping that updates on every #map call.

To be used as a mapping object for Rack::I18nRoutes when the aliases needed by AliasMapping cannot be statically generated at middleware buildtime.

Instance Method Summary collapse

Constructor Details

#initialize(new_aliases_fn, opts = {}) ⇒ AliasMappingUpdater

Creates a new alias-based Mapping object that updates its aliases on every path normalization.

Examples:

Update aliases for modified user-generated content


update_fn = Proc.new do
	aliases = {}

	aliases['articles'] => {
		'ita' => 'articoli',
		'spa' => 'articulos',
	}

	if @articles.any { |article| article.changed? }
		@cached_articles_aliases = all_article_aliases()
	end

	aliases['articles'][:children] = @cached_articles_aliases
end

MAPPING = Rack::I18nRoutes::AliasMappingUpdater.new(update_fn)
use Rack::I18nRoutes, MAPPING

Delegate the aliases generation to another class


update_fn = Proc.new { @translation_mngr.updated_aliases }

MAPPING = Rack::I18nRoutes::AliasMappingUpdater.new(update_fn)
use Rack::I18nRoutes, MAPPING

Parameters:

  • new_aliases_fn (Proc)

    a parameter-less function that returns the new aliases

  • opts (Hash) (defaults to: {})

    the options to be passed to the underlying AliasMapping object



52
53
54
55
# File 'lib/rack/i18n_routes/alias_mapping_updater.rb', line 52

def initialize(new_aliases_fn, opts = {})
	@new_aliases_fn = new_aliases_fn
	@opts = opts
end

Instance Method Details

#alias_mappingObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
92
93
94
# File 'lib/rack/i18n_routes/alias_mapping_updater.rb', line 89

def alias_mapping
	aliases = @new_aliases_fn[]
	mapping = Rack::I18nRoutes::AliasMapping.new(aliases, @opts)

	return mapping
end

#map(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

  • Rack::I18nRoutes::AliasMapping#map


63
64
65
# File 'lib/rack/i18n_routes/alias_mapping_updater.rb', line 63

def map(path)
	alias_mapping.map(path)
end

#path_analysis(path, replacement_language = :default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

  • Rack::I18nRoutes::AliasMapping#analysis


83
84
85
# File 'lib/rack/i18n_routes/alias_mapping_updater.rb', line 83

def path_analysis(path, replacement_language = :default)
	alias_mapping.analysis(path, replacement_language)
end

#translate_into(path, language) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:

  • Rack::I18nRoutes::AliasMapping#translate_into


73
74
75
# File 'lib/rack/i18n_routes/alias_mapping_updater.rb', line 73

def translate_into(path, language)
	alias_mapping.translated_into(path, language)
end