Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding resource preload functions, improving number formatting perfor…

…mance
  • Loading branch information...
commit 4d6275f1e63804230e8468abc934086957dd6900 1 parent b713d9f
Cameron Dutro authored
View
10 lib/twitter_cldr/formatters/numbers/number_formatter.rb
@@ -11,7 +11,11 @@ class NumberFormatter < Base
DEFAULT_SYMBOLS = { :group => ',', :decimal => '.', :plus_sign => '+', :minus_sign => '-' }
def initialize(options = {})
- @tokenizer = TwitterCldr::Tokenizers::NumberTokenizer.new(:locale => extract_locale(options))
+ locale = extract_locale(options)
+ cache_key = TwitterCldr::Utils.compute_cache_key(locale)
+ @tokenizer = tokenizer_cache[cache_key] ||= TwitterCldr::Tokenizers::NumberTokenizer.new(
+ :locale => locale
+ )
@symbols = DEFAULT_SYMBOLS.merge(tokenizer.symbols)
end
@@ -28,6 +32,10 @@ def format(number, opts = {})
protected
+ def tokenizer_cache
+ @@tokenizer_cache ||= {}
+ end
+
def transform_number(number)
number # noop for base class
end
View
49 lib/twitter_cldr/resources/loader.rb
@@ -13,11 +13,58 @@ def get_resource(*path)
end
def get_locale_resource(locale, resource_name)
- get_resource(:locales, TwitterCldr.convert_locale(locale), resource_name)
+ get_resource(*locale_resource_path(locale, resource_name))
+ end
+
+ def resource_loaded?(*path)
+ resources_cache.include?(resource_file_path(path))
+ end
+
+ def locale_resource_loaded?(locale, resource_name)
+ resource_loaded?(*locale_resource_path(locale, resource_name))
+ end
+
+ def resource_types
+ @resource_types ||= Dir.glob(File.join(RESOURCES_DIR, 'locales/en', '*')).map do |file|
+ File.basename(file).chomp(File.extname(file)).to_sym
+ end
+ end
+
+ def preload_resources_for_locale(locale, *resources)
+ if resources.size > 0
+ resources = resource_types if resources.first == :all
+ resources.each { |res| get_locale_resource(locale, res) }
+ end
+ nil
+ end
+
+ def preload_resource_for_locales(resource, *locales)
+ locales.each do |locale|
+ preload_resources_for_locale(locale, resource)
+ end
+ nil
+ end
+
+ def preload_resources_for_all_locales(*resources)
+ TwitterCldr.supported_locales.each do |locale|
+ preload_resources_for_locale(locale, *resources)
+ end
+ nil
+ end
+
+ def preload_all_resources
+ TwitterCldr.supported_locales.each do |locale|
+ preload_resources_for_locale(locale, :all)
+ end
+ nil
end
private
+ def locale_resource_path(locale, resource_name)
+ [:locales, TwitterCldr.convert_locale(locale), resource_name]
+ end
+
def resources_cache
@resources_cache ||= Hash.new { |hash, path| hash[path] = load_resource(path) }
end
View
4 lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb
@@ -41,8 +41,8 @@ def initialize(options = {})
end
def tokens(options = {})
- @type = options[:type] || @type || :default
- @format = options[:format] || @format || :default
+ @type = options[:type] || :default
+ @format = options[:format] || :default
path = full_path
positive, negative = traverse(path).to_s.split(/;/)
View
9 spec/formatters/numbers/abbreviated/long_decimal_formatter_spec.rb
@@ -4,6 +4,8 @@
# http://www.apache.org/licenses/LICENSE-2.0
require 'spec_helper'
+require 'pry'
+require 'pry-nav'
include TwitterCldr::Formatters
@@ -32,6 +34,11 @@
end
it "formats the number as if it were a straight decimal if it's less than 1000" do
- @formatter.format(500).should == "500"
+ begin
+ @formatter.format(500).should == "500"
+ rescue
+ binding.pry
+ @formatter.format(500).should == "500"
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.