Permalink
Browse files

Merge pull request #29 from KL-7/cleanup

Cleaning up LocalizedDateTime and LocalizedNumber.
  • Loading branch information...
2 parents 070d08a + 2290610 commit 9c0a3fccb0992c88651382a164f32c04114b5714 @camertron camertron committed May 7, 2012
View
@@ -13,8 +13,6 @@
require 'forwardable'
require 'twitter_cldr/version'
-require 'twitter_cldr/core_ext'
-
module TwitterCldr
@@ -83,4 +81,6 @@ def supported_locale?(locale)
end
-end
+end
+
+require 'twitter_cldr/core_ext'
@@ -13,22 +13,19 @@ module TwitterCldr
class LocalizedDateTime < LocalizedObject
attr_reader :calendar_type
- def initialize(obj, locale, options={})
- super(obj, locale, options)
+ def initialize(obj, locale, options = {})
+ super
@calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
end
- def method_missing(method, *args, &block)
- type = method.to_s.match(/to_(\w+)_s/)[1]
- if type && !type.empty? && TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.include?(type.to_sym)
- @formatter.format(@base_obj, :type => type.to_sym)
- else
- raise "Method not supported"
+ TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.each do |format_type|
+ define_method "to_#{format_type}_s" do
+ @formatter.format(@base_obj, :type => format_type.to_sym)
end
end
def to_s
- self.to_default_s
+ to_default_s
end
def to_date
@@ -7,12 +7,14 @@ module TwitterCldr
class LocalizedObject
attr_reader :locale, :base_obj, :formatter
- def initialize(obj, locale, options={})
+ def initialize(obj, locale, options = {})
@base_obj = obj
@locale = locale
+ options = options.dup
options[:locale] ||= @locale
- @formatter = self.formatter_const.new(options) if self.formatter_const
+
+ @formatter = formatter_const.new(options) if formatter_const
end
def formatter_const
@@ -4,35 +4,37 @@
# http://www.apache.org/licenses/LICENSE-2.0
module TwitterCldr
+
module LocalizedNumberMixin
def localize(locale = TwitterCldr.get_locale)
TwitterCldr::LocalizedNumber.new(self, locale)
end
end
class LocalizedNumber < LocalizedObject
+ TYPES = [:decimal, :currency, :percent]
DEFAULT_TYPE = :decimal
+
attr_reader :type
- def to_decimal(options = {})
- self.setup_for(:decimal)
- self
- end
+ def initialize(obj, locale, options = {})
+ @options = options.dup
- def to_currency(options = {})
- self.setup_for(:currency)
- self
+ @type = @options.delete(:type) || DEFAULT_TYPE
+ raise ArgumentError.new("type #{@type} is not supported") unless @type && TYPES.include?(@type.to_sym)
+
+ super(obj, locale, @options)
end
- def to_percent(options = {})
- self.setup_for(:percent)
- self
+ TYPES.each do |type|
+ define_method "to_#{type}" do
+ to_type(type)
+ end
end
def to_s(options = {})
- opts = options
- opts = { :precision => 0 }.merge(opts) if @base_obj.is_a?(Fixnum)
- @formatter.format(@base_obj, opts)
+ options = { :precision => 0 }.merge(options) if @base_obj.is_a?(Fixnum)
+ @formatter.format(@base_obj, options)
end
def plural_rule
@@ -42,13 +44,12 @@ def plural_rule
protected
def formatter_const
- TwitterCldr::Formatters::DecimalFormatter
+ TwitterCldr::Formatters.const_get("#{@type.to_s.capitalize}Formatter")
end
- def setup_for(type)
- @type = type
- fmt_class = TwitterCldr::Formatters.const_get("#{(@type || DEFAULT_TYPE).to_s.capitalize}Formatter".to_sym)
- @formatter = fmt_class.new(:locale => locale.to_sym)
+ def to_type(target_type)
+ self.class.new(@base_obj, @locale, @options.merge(:type => target_type))
end
end
+
end
@@ -56,13 +56,6 @@
end
describe LocalizedDate do
- context "with an unsupported type" do
- it "raise an error because 'albatross' is not a supported type" do
- date = Date.today.localize(:it)
- lambda { date.to_albatross_s }.should raise_error("Method not supported")
- end
- end
-
describe "#to_datetime" do
it "should combine a date and a time object into a datetime" do
date = Date.new(1987, 9, 20)
@@ -57,13 +57,6 @@
end
describe LocalizedDateTime do
- context "with an unsupported type" do
- it "raise an error because 'albatross' is not a supported type" do
- date_time = DateTime.now.localize(:it)
- lambda { date_time.to_albatross_s }.should raise_error("Method not supported")
- end
- end
-
describe "#to_date" do
it "should convert to a date" do
date = DateTime.new(1987, 9, 20, 22, 5).localize.to_date
@@ -56,13 +56,6 @@
end
describe LocalizedTime do
- context "with an unsupported type" do
- it "raise an error because 'albatross' is not a supported type" do
- time = Time.now.localize(:it)
- lambda { time.to_albatross_s }.should raise_error("Method not supported")
- end
- end
-
describe "#to_datetime" do
it "should combine a date and a time object into a datetime" do
date = Date.new(1987, 9, 20)
@@ -8,48 +8,85 @@
include TwitterCldr
describe LocalizedNumber do
- before(:each) do
- @number = 10.localize
- end
+ describe '#initialize' do
+ let(:decimal) { LocalizedNumber.new(10, :en) }
+ let(:currency) { LocalizedNumber.new(10, :en, :type => :currency) }
- describe "#to_decimal" do
- it "should internally create a decimal formatter and return itself" do
- mock.proxy(TwitterCldr::Formatters).const_get(:DecimalFormatter)
- @number.to_decimal.should == @number
+ it 'uses :decimal type by default' do
+ decimal.type.should == :decimal
end
- end
- describe "#to_currency" do
- it "should internally create a currency formatter and return itself" do
- mock.proxy(TwitterCldr::Formatters).const_get(:CurrencyFormatter)
- @number.to_currency.should == @number
+ it 'setups DecimalFormatter by default' do
+ decimal.formatter.should be_an_instance_of(Formatters::DecimalFormatter)
+ end
+
+ it 'uses provided type if there is one' do
+ currency.type.should == :currency
+ end
+
+ it 'setups formatter corresponding to the specified type' do
+ currency.formatter.should be_an_instance_of(Formatters::CurrencyFormatter)
+ end
+
+ it 'setups formatter with correct locale' do
+ mock.proxy(TwitterCldr::Formatters::DecimalFormatter).new(:locale => :es)
+ LocalizedNumber.new(10, :es)
+ end
+
+ it 'raises ArguemntError if unsupported type is passed' do
+ lambda { LocalizedNumber.new(10, :en, :type => :foo) }.should raise_error(ArgumentError, 'type foo is not supported')
end
end
- describe "#to_percent" do
- it "should internally create a percentage formatter and return itself" do
- mock.proxy(TwitterCldr::Formatters).const_get(:PercentFormatter)
- @number.to_percent.should == @number
+ describe 'type conversion methods' do
+ let(:number) { LocalizedNumber.new(10, :en) }
+ let(:currency) { LocalizedNumber.new(10, :en, :type => :currency) }
+
+ LocalizedNumber::TYPES.each do |type|
+ describe "#to_#{type}" do
+ let(:method) { "to_#{type}" }
+
+ it 'creates a new object' do
+ number.send(method).object_id.should_not == number.object_id
+ end
+
+ it 'creates an object of appropriate type, but do not change type of the original object' do
+ new_number = currency.send(method)
+ new_number.type.should == type
+ currency.type.should == :currency
+ end
+
+ it 'creates a new object with the same base object and locale' do
+ percent = LocalizedNumber.new(42, :fr, :type => :percent)
+ mock.proxy(TwitterCldr::LocalizedNumber).new(42, :fr, :type => type)
+ percent.send(method)
+ end
+ end
end
end
- describe "#to_s" do
- it "should default precision to zero for fixnums" do
- mock(@number.formatter).format(@number.base_obj, :precision => 0)
- @number.to_s
+ describe '#to_s' do
+ let(:number) { LocalizedNumber.new(10, :en) }
+
+ it 'should default precision to zero for fixnums' do
+ mock(number.formatter).format(number.base_obj, :precision => 0)
+ number.to_s
end
- it "should not overwrite precision when explicitly passed" do
- mock(@number.formatter).format(@number.base_obj, :precision => 2)
- @number.to_s(:precision => 2)
+ it 'should not overwrite precision when explicitly passed' do
+ mock(number.formatter).format(number.base_obj, :precision => 2)
+ number.to_s(:precision => 2)
end
end
- describe "#plural_rule" do
- it "should return the appropriate plural rule for the number" do
+ describe '#plural_rule' do
+ it 'returns the appropriate plural rule for the number' do
1.localize(:ru).plural_rule.should == :one
2.localize(:ru).plural_rule.should == :few
5.localize(:ru).plural_rule.should == :many
+ end
+
+ it 'takes FastGettext.locale into account' do
FastGettext.locale = :es
1.localize.plural_rule.should == :one
2.localize.plural_rule.should == :other

0 comments on commit 9c0a3fc

Please sign in to comment.