Skip to content

Commit

Permalink
Merge pull request #23 from insales/frozen_strings
Browse files Browse the repository at this point in the history
Уменьшаем алокацию объектов за счет замороженных строк
  • Loading branch information
hbda authored Feb 5, 2024
2 parents c83cb2d + ede8038 commit 6ce474a
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 35 deletions.
3 changes: 2 additions & 1 deletion lib/globalize.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'globalize/load_path'
require 'globalize/backend/static'
require 'globalize/model/active_record'
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/backend/chain.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module I18n
class << self
def chain_backends(*args)
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/backend/pluralizing.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'i18n/backend/simple'

module Globalize
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/backend/static.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'globalize/backend/pluralizing'
require 'globalize/locale/fallbacks'
require 'globalize/translation'
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/i18n/missing_translations_log_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

# A simple exception handler that behaves like the default exception handler
# but additionally logs missing translations to a given log.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/i18n/missing_translations_raise_handler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

# A simple exception handler that behaves like the default exception handler
# but also raises on missing translations.
#
Expand Down
21 changes: 11 additions & 10 deletions lib/globalize/load_path.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

# Locale load_path and Locale loading support.
#
# To use this include the Globalize::LoadPath::I18n module to I18n like this:
Expand All @@ -14,16 +15,16 @@
#
# I18n.load_locales 'en-US', 'de-DE'
# I18n.load_locale 'en-US'
#
#
# This will lookup all files named like:
#
# 'path/to/dir/all.yml'
# 'path/to/dir/en-US.yml'
# 'path/to/dir/en-US/*.yml'
#
# The filenames will be passed to I18n.load_translations which delegates to
# The filenames will be passed to I18n.load_translations which delegates to
# the backend. So the actual behaviour depends on the implementation of the
# backend. I18n::Backend::Simple will be able to read YAML and plain Ruby
# backend. I18n::Backend::Simple will be able to read YAML and plain Ruby
# files. See the documentation for I18n.load_translations for details.

module Globalize
Expand All @@ -32,27 +33,27 @@ def extensions
@extensions ||= ['rb', 'yml']
end
attr_writer :extensions

def locales
@locales ||= ['*']
end
attr_writer :locales

def <<(path)
push path
end

def push(*paths)
super(*paths.map{|path| filenames(path) }.flatten.uniq.sort)
end

protected

def filenames(path)
return [path] if File.file? path
patterns(path).map{|pattern| Dir[pattern] }
end

def patterns(path)
locales.map do |locale|
extensions.map do |extension|
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/locale/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module I18n
@@ar_fallbacks = nil
@@ar_locale = nil
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/locale/fallbacks.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'globalize/locale/language_tag'

module I18n
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/locale/language_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

# for specifications see http://en.wikipedia.org/wiki/IETF_language_tag
#
# SimpleParser does not implement advanced usages such as grandfathered tags
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/model/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

require 'active_support/core_ext/object/deep_dup'
require 'globalize/translation'
require 'globalize/locale/fallbacks'
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/model/active_record/languages.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module I18n
@@languages = nil
@@languages_array = nil
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/model/active_record/postgres_array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module Globalize
module Model
module ActiveRecord
Expand Down
3 changes: 2 additions & 1 deletion lib/globalize/model/active_record/translated.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module Globalize
module Model

Expand Down
13 changes: 7 additions & 6 deletions lib/globalize/translation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module Globalize
# Translations are simple value objects that carry some context information
# alongside the actual translation string.
Expand All @@ -7,25 +8,25 @@ class Translation < String
class Attribute < Translation
attr_accessor :requested_locale, :locale, :key
end

class Static < Translation
attr_accessor :requested_locale, :locale, :key, :options, :plural_key, :original

def initialize(string, meta = nil)
self.original = string
super
end
end

def initialize(string, meta = nil)
set_meta meta
super string
end

def fallback?
locale.to_sym != requested_locale.to_sym
end

def set_meta(meta)
meta.each {|name, value| send :"#{name}=", value } if meta
end
Expand Down
13 changes: 7 additions & 6 deletions lib/rails_edge_load_path_patch.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- encoding : utf-8 -*-
# frozen_string_literal: true

module I18n
@@load_path = nil
@@default_locale = :'en-US'

class << self
def load_path
@@load_path ||= []
end

def load_path=(load_path)
@@load_path = load_path
end
Expand All @@ -18,14 +19,14 @@ def load_path=(load_path)
def initialized?
@initialized ||= false
end

protected

def init_translations
load_translations(*I18n.load_path)
@initialized = true
end

def lookup(locale, key, scope = [])
return unless key
init_translations unless initialized?
Expand All @@ -35,7 +36,7 @@ def lookup(locale, key, scope = [])
end

rails_dir = File.expand_path "#{File.dirname(__FILE__)}/../../../rails/"
paths = %w(actionpack/lib/action_view/locale/en-US.yml
paths = %w(actionpack/lib/action_view/locale/en-US.yml
activerecord/lib/active_record/locale/en-US.yml
activesupport/lib/active_support/locale/en-US.yml)
paths.each{|path| I18n.load_path << "#{rails_dir}/#{path}" }

0 comments on commit 6ce474a

Please sign in to comment.