twitter_cldr 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/History.txt +6 -0
  2. data/README.md +61 -5
  3. data/Rakefile +64 -60
  4. data/js/lib/twitter_cldr_js.rb +0 -2
  5. data/lib/twitter_cldr/core_ext.rb +12 -12
  6. data/lib/twitter_cldr/formatters/calendars/timespan_formatter.rb +13 -11
  7. data/lib/twitter_cldr/localized/localized_array.rb +33 -0
  8. data/lib/twitter_cldr/localized/localized_date.rb +23 -0
  9. data/lib/twitter_cldr/localized/localized_datetime.rb +63 -0
  10. data/lib/twitter_cldr/localized/localized_number.rb +50 -0
  11. data/lib/twitter_cldr/localized/localized_object.rb +38 -0
  12. data/lib/twitter_cldr/localized/localized_string.rb +41 -0
  13. data/lib/twitter_cldr/localized/localized_symbol.rb +20 -0
  14. data/lib/twitter_cldr/localized/localized_time.rb +23 -0
  15. data/lib/twitter_cldr/localized/localized_timespan.rb +26 -0
  16. data/lib/twitter_cldr/localized.rb +18 -0
  17. data/lib/twitter_cldr/normalization.rb +23 -0
  18. data/lib/twitter_cldr/resources/{tries_dumper.rb → collation_tries_dumper.rb} +1 -1
  19. data/lib/twitter_cldr/resources/composition_exclusions_importer.rb +1 -1
  20. data/lib/twitter_cldr/resources/language_codes_importer.rb +232 -0
  21. data/lib/twitter_cldr/resources/locales_resources_importer.rb +1 -1
  22. data/lib/twitter_cldr/resources/phone_codes_importer.rb +1 -1
  23. data/lib/twitter_cldr/resources/postal_codes_importer.rb +1 -1
  24. data/lib/twitter_cldr/resources/tailoring_importer.rb +12 -3
  25. data/lib/twitter_cldr/resources/unicode_data_importer.rb +3 -1
  26. data/lib/twitter_cldr/resources.rb +2 -1
  27. data/lib/twitter_cldr/shared/calendar.rb +2 -6
  28. data/lib/twitter_cldr/shared/language_codes.rb +75 -0
  29. data/lib/twitter_cldr/shared/languages.rb +4 -11
  30. data/lib/twitter_cldr/shared.rb +8 -7
  31. data/lib/twitter_cldr/tokenizers/base.rb +2 -8
  32. data/lib/twitter_cldr/utils.rb +8 -0
  33. data/lib/twitter_cldr/version.rb +1 -1
  34. data/lib/twitter_cldr.rb +5 -4
  35. data/resources/custom/locales/cs/units.yml +3 -3
  36. data/resources/custom/locales/pl/units.yml +4 -4
  37. data/resources/custom/locales/pt/units.yml +2 -2
  38. data/resources/shared/language_codes_table.dump +0 -0
  39. data/spec/core_ext_spec.rb +19 -0
  40. data/spec/{core_ext/array_spec.rb → localized/localized_array_spec.rb} +1 -1
  41. data/spec/{core_ext/calendars/date_spec.rb → localized/localized_date_spec.rb} +24 -44
  42. data/spec/localized/localized_datetime_spec.rb +81 -0
  43. data/spec/{core_ext/numbers → localized}/localized_number_spec.rb +34 -1
  44. data/spec/localized/localized_object_spec.rb +89 -0
  45. data/spec/{core_ext/string_spec.rb → localized/localized_string_spec.rb} +16 -33
  46. data/spec/{core_ext/symbol_spec.rb → localized/localized_symbol_spec.rb} +3 -1
  47. data/spec/localized/localized_time_spec.rb +70 -0
  48. data/spec/normalization_spec.rb +42 -0
  49. data/spec/readme_spec.rb +51 -5
  50. data/spec/shared/language_codes_spec.rb +161 -0
  51. data/spec/shared/phone_codes_spec.rb +2 -2
  52. data/spec/shared/postal_codes_spec.rb +2 -2
  53. data/spec/spec_helper.rb +2 -0
  54. data/spec/tokenizers/base_spec.rb +15 -6
  55. data/spec/utils_spec.rb +18 -2
  56. data/twitter_cldr.gemspec +2 -1
  57. metadata +28 -44
  58. data/lib/twitter_cldr/core_ext/array.rb +0 -35
  59. data/lib/twitter_cldr/core_ext/calendars/date.rb +0 -25
  60. data/lib/twitter_cldr/core_ext/calendars/datetime.rb +0 -65
  61. data/lib/twitter_cldr/core_ext/calendars/time.rb +0 -25
  62. data/lib/twitter_cldr/core_ext/calendars/timespan.rb +0 -24
  63. data/lib/twitter_cldr/core_ext/localized_object.rb +0 -25
  64. data/lib/twitter_cldr/core_ext/numbers/bignum.rb +0 -8
  65. data/lib/twitter_cldr/core_ext/numbers/fixnum.rb +0 -8
  66. data/lib/twitter_cldr/core_ext/numbers/float.rb +0 -8
  67. data/lib/twitter_cldr/core_ext/numbers/localized_number.rb +0 -54
  68. data/lib/twitter_cldr/core_ext/string.rb +0 -51
  69. data/lib/twitter_cldr/core_ext/symbol.rb +0 -22
  70. data/spec/core_ext/calendars/datetime_spec.rb +0 -90
  71. data/spec/core_ext/calendars/time_spec.rb +0 -90
  72. data/spec/core_ext/calendars_spec.rb +0 -34
  73. data/spec/core_ext/numbers/bignum_spec.rb +0 -25
  74. data/spec/core_ext/numbers/fixnum_spec.rb +0 -25
  75. data/spec/core_ext/numbers/float_spec.rb +0 -25
  76. data/spec/core_ext/numbers_spec.rb +0 -39
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.8.0
2
+
3
+ * Added support for language code conversion.
4
+ * `#localize` methods (eg. for Hash, String, etc) now dynamically generated, part of the `TwitterCldr::Localized` namespace.
5
+ * New convenience method `TwitterCldr::Normalization#normalize`.
6
+
1
7
  === 1.7.0
2
8
 
3
9
  * Wrote rake tasks to update CLDR and ICU resources.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## twitter-cldr-rb [![Build Status](https://secure.travis-ci.org/twitter/twitter-cldr-rb.png?branch=master)](http://travis-ci.org/twitter/twitter-cldr-rb)
1
+ ## twitter-cldr-rb [![Build Status](https://secure.travis-ci.org/twitter/twitter-cldr-rb.png?branch=master)](http://travis-ci.org/twitter/twitter-cldr-rb) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/twitter/twitter-cldr-rb)
2
2
 
3
3
  TwitterCldr uses Unicode's Common Locale Data Repository (CLDR) to format certain types of text into their
4
4
  localized equivalents. Currently supported types of text include dates, times, currencies, decimals, percentages, and symbols.
@@ -57,7 +57,7 @@ TwitterCldr patches core Ruby objects like `Fixnum` and `Date` to make localizat
57
57
  Behind the scenes, these convenience methods are creating instances of `LocalizedNumber`. You can do the same thing if you're feeling adventurous:
58
58
 
59
59
  ```ruby
60
- num = TwitterCldr::LocalizedNumber.new(1337, :es)
60
+ num = TwitterCldr::Localized::LocalizedNumber.new(1337, :es)
61
61
  num.to_currency.to_s # ...etc
62
62
  ```
63
63
 
@@ -105,7 +105,7 @@ The CLDR data set only includes 4 specific date formats, full, long, medium, and
105
105
  Behind the scenes, these convenience methods are creating instances of `LocalizedDate`, `LocalizedTime`, and `LocalizedDateTime`. You can do the same thing if you're feeling adventurous:
106
106
 
107
107
  ```ruby
108
- dt = TwitterCldr::LocalizedDateTime.new(DateTime.now, :es)
108
+ dt = TwitterCldr::Localized::LocalizedDateTime.new(DateTime.now, :es)
109
109
  dt.to_short_s # ...etc
110
110
  ```
111
111
 
@@ -145,11 +145,11 @@ Specify a different reference point for the time span calculation:
145
145
  Behind the scenes, these convenience methods are creating instances of `LocalizedTimespan`, whose constructor accepts a number of seconds as the first argument. You can do the same thing if you're feeling adventurous:
146
146
 
147
147
  ```ruby
148
- ts = TwitterCldr::LocalizedTimespan.new(86400, :locale => :de)
148
+ ts = TwitterCldr::Localized::LocalizedTimespan.new(86400, :locale => :de)
149
149
  ts.to_s # In 1 Tag
150
150
  ts.to_s(:unit => :hour) # In 24 Stunden
151
151
 
152
- ts = TwitterCldr::LocalizedTimespan.new(-86400, :locale => :de)
152
+ ts = TwitterCldr::Localized::LocalizedTimespan.new(-86400, :locale => :de)
153
153
  ts.to_s # Vor 1 Tag
154
154
  ts.to_s(:unit => :hour) # Vor 24 Stunden
155
155
  ```
@@ -326,6 +326,62 @@ Get a list of supported territories by using the `#territories` method:
326
326
  TwitterCldr::Shared::PhoneCodes.territories # [:zw, :an, :tr, :by, :mh, ...]
327
327
  ```
328
328
 
329
+ ### Language Codes
330
+
331
+ Over the years, different standards for language codes have accumulated. Probably the two most popular are ISO-639 and BCP-47 and their children. TwitterCLDR provides a way to convert between these codes systematically.
332
+
333
+ ```ruby
334
+ TwitterCldr::Shared::LanguageCodes.convert(:es, :from => :bcp_47, :to => :iso_639_2) # :spa
335
+ ```
336
+
337
+ Use the `standards_for` method to get the standards that are available for conversion from a given code. In the example below, note that the first argument, `:es`, is the correct BCP-47 language code for Spanish, which is the second argument. The return value comprises all the available conversions:
338
+
339
+ ```ruby
340
+ # [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3]
341
+ TwitterCldr::Shared::LanguageCodes.standards_for(:es, :bcp_47)
342
+ ```
343
+
344
+ Get a list of supported standards for a full English language name:
345
+
346
+ ```ruby
347
+ # [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3]
348
+ TwitterCldr::Shared::LanguageCodes.standards_for_language(:Spanish)
349
+ ```
350
+
351
+ Get a list of supported languages:
352
+
353
+ ```ruby
354
+ TwitterCldr::Shared::LanguageCodes.languages # [:Spanish, :German, :Norwegian, :Arabic ... ]
355
+ ```
356
+
357
+ Determine valid standards:
358
+
359
+ ```ruby
360
+ TwitterCldr::Shared::LanguageCodes.valid_standard?(:iso_639_1) # true
361
+ TwitterCldr::Shared::LanguageCodes.valid_standard?(:blarg) # false
362
+ ```
363
+
364
+ Determine valid codes:
365
+
366
+ ```ruby
367
+ TwitterCldr::Shared::LanguageCodes.valid_code?(:es, :bcp_47) # true
368
+ TwitterCldr::Shared::LanguageCodes.valid_code?(:es, :iso_639_2) # false
369
+ ```
370
+
371
+ Convert the full English name of a language into a language code:
372
+
373
+ ```ruby
374
+ TwitterCldr::Shared::LanguageCodes.from_language(:Spanish, :iso_639_2) # :spa
375
+ ```
376
+
377
+ Convert a language code into it's full English name:
378
+
379
+ ```ruby
380
+ TwitterCldr::Shared::LanguageCodes.to_language(:spa, :iso_639_2) # "Spanish"
381
+ ```
382
+
383
+ **NOTE**: All of the functions in `TwitterCldr::Shared::LanguageCodes` accept both symbol and string parameters.
384
+
329
385
  ### Unicode Data
330
386
 
331
387
  TwitterCLDR provides ways to retrieve individual code points as well as normalize and decompose Unicode text.
data/Rakefile CHANGED
@@ -40,79 +40,83 @@ if RUBY_VERSION < '1.9.0'
40
40
  end
41
41
  end
42
42
 
43
- namespace :resources do
44
- namespace :update do
45
-
46
- desc 'Import locales resources'
47
- task :locales_resources, :cldr_path do |_, args|
48
- TwitterCldr::Resources::LocalesResourcesImporter.new(
49
- args[:cldr_path] || '../cldr',
50
- './resources/locales'
51
- ).import
52
- end
43
+ namespace :update do
44
+ desc 'Import locales resources'
45
+ task :locales_resources, :cldr_path do |_, args|
46
+ TwitterCldr::Resources::LocalesResourcesImporter.new(
47
+ args[:cldr_path] || './vendor/cldr',
48
+ './resources/locales'
49
+ ).import
50
+ end
53
51
 
54
- desc 'Import custom locales resources'
55
- task :custom_locales_resources do
56
- TwitterCldr::Resources::CustomLocalesResourcesImporter.new('./resources/custom/locales').import
57
- end
52
+ desc 'Import custom locales resources'
53
+ task :custom_locales_resources do
54
+ TwitterCldr::Resources::CustomLocalesResourcesImporter.new('./resources/custom/locales').import
55
+ end
58
56
 
59
- desc 'Import tailoring resources from CLDR data (should be executed using JRuby 1.7 in 1.9 mode)'
60
- task :tailoring, :cldr_path, :icu4j_jar_path do |_, args|
61
- TwitterCldr::Resources::TailoringImporter.new(
62
- args[:cldr_path] || '../cldr',
63
- './resources/collation/tailoring',
64
- args[:icu4j_jar_path] ||'../icu4j-49_1.jar'
65
- ).import(TwitterCldr.supported_locales)
66
- end
57
+ desc 'Import tailoring resources from CLDR data (should be executed using JRuby 1.7 in 1.9 mode)'
58
+ task :tailoring_data, :cldr_path, :icu4j_jar_path do |_, args|
59
+ TwitterCldr::Resources::TailoringImporter.new(
60
+ args[:cldr_path] || './vendor/cldr',
61
+ './resources/collation/tailoring',
62
+ args[:icu4j_jar_path] ||'./vendor/icu4j-49_1.jar'
63
+ ).import(TwitterCldr.supported_locales)
64
+ end
67
65
 
68
- desc 'Import Unicode data resources'
69
- task :unicode_data, :unicode_data_path do |_, args|
70
- TwitterCldr::Resources::UnicodeDataImporter.new(
71
- args[:unicode_data_path] || '../unicode-data',
72
- './resources/unicode_data'
73
- ).import
74
- end
66
+ desc 'Import Unicode data resources'
67
+ task :unicode_data, :unicode_data_path do |_, args|
68
+ TwitterCldr::Resources::UnicodeDataImporter.new(
69
+ args[:unicode_data_path] || './vendor/unicode-data',
70
+ './resources/unicode_data'
71
+ ).import
72
+ end
75
73
 
76
- desc 'Import composition exclusions resource'
77
- task :composition_exclusions, :derived_normalization_props_path do |_, args|
78
- TwitterCldr::Resources::CompositionExclusionsImporter.new(
79
- args[:derived_normalization_props_path] || '../unicode-data/DerivedNormalizationProps.txt',
80
- './resources/unicode_data'
81
- ).import
82
- end
74
+ desc 'Import composition exclusions resource'
75
+ task :composition_exclusions, :derived_normalization_props_path do |_, args|
76
+ TwitterCldr::Resources::CompositionExclusionsImporter.new(
77
+ args[:derived_normalization_props_path] || './vendor/unicode-data/DerivedNormalizationProps.txt',
78
+ './resources/unicode_data'
79
+ ).import
80
+ end
83
81
 
84
- desc 'Import postal codes resource'
85
- task :postal_codes, :cldr_path do |_, args|
86
- TwitterCldr::Resources::PostalCodesImporter.new(
87
- args[:cldr_path] || '../cldr',
88
- './resources/shared'
89
- ).import
90
- end
82
+ desc 'Import postal codes resource'
83
+ task :postal_codes, :cldr_path do |_, args|
84
+ TwitterCldr::Resources::PostalCodesImporter.new(
85
+ args[:cldr_path] || './vendor/cldr',
86
+ './resources/shared'
87
+ ).import
88
+ end
91
89
 
92
- desc 'Import phone codes resource'
93
- task :phone_codes, :cldr_path do |_, args|
94
- TwitterCldr::Resources::PhoneCodesImporter.new(
95
- args[:cldr_path] || '../cldr',
96
- './resources/shared'
97
- ).import
98
- end
90
+ desc 'Import phone codes resource'
91
+ task :phone_codes, :cldr_path do |_, args|
92
+ TwitterCldr::Resources::PhoneCodesImporter.new(
93
+ args[:cldr_path] || './vendor/cldr',
94
+ './resources/shared'
95
+ ).import
96
+ end
99
97
 
100
- desc 'Update default and tailoring tries dumps'
101
- task :tries do
102
- TwitterCldr::Resources::TriesDumper.update_dumps
103
- end
98
+ desc 'Import language codes'
99
+ task :language_codes, :language_codes_data do |_, args|
100
+ TwitterCldr::Resources::LanguageCodesImporter.new(
101
+ args[:language_codes_data] || './vendor/language-codes',
102
+ './resources/shared'
103
+ ).import
104
+ end
104
105
 
105
- desc 'Update canonical compositions resource'
106
- task :canonical_compositions do
107
- TwitterCldr::Resources::CanonicalCompositionsUpdater.new('./resources/unicode_data').update
108
- end
106
+ desc 'Update default and tailoring tries dumps'
107
+ task :collation_tries do
108
+ TwitterCldr::Resources::CollationTriesDumper.update_dumps
109
+ end
109
110
 
111
+ desc 'Update canonical compositions resource'
112
+ task :canonical_compositions do
113
+ TwitterCldr::Resources::CanonicalCompositionsUpdater.new('./resources/unicode_data').update
110
114
  end
111
115
  end
112
116
 
113
117
  namespace :js do
118
+ desc 'Build JS files'
114
119
  task :build do
115
- require File.expand_path(File.join(File.dirname(__FILE__), %w[lib twitter_cldr]))
116
120
  TwitterCldr.require_js
117
121
  FileUtils.mkdir_p(TwitterCldr::Js.build_dir)
118
122
  TwitterCldr::Js.output_dir = File.expand_path(ENV["OUTPUT_DIR"])
@@ -120,8 +124,8 @@ namespace :js do
120
124
  TwitterCldr::Js.install
121
125
  end
122
126
 
127
+ desc 'Run JS specs'
123
128
  task :test do
124
- require File.expand_path(File.join(File.dirname(__FILE__), %w[lib twitter_cldr]))
125
129
  TwitterCldr.require_js
126
130
  FileUtils.mkdir_p(TwitterCldr::Js.build_dir)
127
131
  TwitterCldr::Js.make(:locales => [:en])
@@ -3,8 +3,6 @@
3
3
  # Copyright 2012 Twitter, Inc
4
4
  # http://www.apache.org/licenses/LICENSE-2.0
5
5
 
6
- $:.push(File.dirname(__FILE__))
7
-
8
6
  require 'mustache'
9
7
  require 'uglifier'
10
8
  require 'jasmine-headless-webkit'
@@ -1,12 +1,12 @@
1
- require 'twitter_cldr/core_ext/localized_object'
2
- require 'twitter_cldr/core_ext/calendars/datetime'
3
- require 'twitter_cldr/core_ext/calendars/date'
4
- require 'twitter_cldr/core_ext/calendars/time'
5
- require 'twitter_cldr/core_ext/calendars/timespan'
6
- require 'twitter_cldr/core_ext/numbers/localized_number'
7
- require 'twitter_cldr/core_ext/numbers/bignum'
8
- require 'twitter_cldr/core_ext/numbers/fixnum'
9
- require 'twitter_cldr/core_ext/numbers/float'
10
- require 'twitter_cldr/core_ext/symbol'
11
- require 'twitter_cldr/core_ext/string'
12
- require 'twitter_cldr/core_ext/array'
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ [Bignum, Fixnum, Float].each do |klass|
7
+ TwitterCldr::Localized::LocalizedNumber.localize(klass)
8
+ end
9
+
10
+ [Array, Date, DateTime, String, Symbol, Time].each do |klass|
11
+ TwitterCldr::Localized::const_get("Localized#{klass}").localize(klass)
12
+ end
@@ -6,16 +6,17 @@
6
6
  module TwitterCldr
7
7
  module Formatters
8
8
  class TimespanFormatter < Base
9
+
9
10
  DEFAULT_TYPE = :default
10
11
 
11
- TIME_IN_SECONDS = {
12
- :second => 1,
13
- :minute => 60,
14
- :hour => 3600,
15
- :day => 86400,
16
- :week => 604800,
17
- :month => 2629743.83,
18
- :year => 31556926
12
+ TIME_IN_SECONDS = {
13
+ :second => 1,
14
+ :minute => 60,
15
+ :hour => 3600,
16
+ :day => 86400,
17
+ :week => 604800,
18
+ :month => 2629743.83,
19
+ :year => 31556926
19
20
  }
20
21
 
21
22
  def initialize(options = {})
@@ -24,13 +25,14 @@ module TwitterCldr
24
25
  end
25
26
 
26
27
  def format(seconds, options = {})
28
+ options[:type] ||= DEFAULT_TYPE
27
29
  options[:direction] ||= @direction || (seconds < 0 ? :ago : :until)
28
- options[:unit] ||= self.calculate_unit(seconds.abs)
30
+ options[:unit] ||= calculate_unit(seconds.abs)
31
+
29
32
  options[:number] = calculate_time(seconds.abs, options[:unit])
30
- options[:type] ||= DEFAULT_TYPE
31
33
 
32
34
  tokens = @tokenizer.tokens(options)
33
- strings = tokens.map { |token| token[:value]}
35
+ strings = tokens.map { |token| token[:value] }
34
36
  strings.join.gsub(/\{[0-9]\}/, options[:number].to_s)
35
37
  end
36
38
 
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Localized
8
+
9
+ class LocalizedArray < LocalizedObject
10
+ def code_points_to_string
11
+ TwitterCldr::Utils::CodePoints.to_string(base_obj)
12
+ end
13
+
14
+ def sort
15
+ TwitterCldr::Collation::Collator.new(locale).sort(base_obj).localize
16
+ end
17
+
18
+ def sort!
19
+ TwitterCldr::Collation::Collator.new(locale).sort!(base_obj)
20
+ self
21
+ end
22
+
23
+ def formatter_const
24
+ nil
25
+ end
26
+
27
+ def to_a
28
+ @base_obj.dup
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Localized
8
+
9
+ class LocalizedDate < LocalizedDateTime
10
+ def to_datetime(time)
11
+ time_obj = time.is_a?(LocalizedTime) ? time.base_obj : time
12
+ LocalizedDateTime.new(DateTime.parse("#{@base_obj.strftime("%Y-%m-%d")}T#{time_obj.strftime("%H:%M:%S%z")}"), @locale, :calendar_type => @calendar_type)
13
+ end
14
+
15
+ protected
16
+
17
+ def formatter_const
18
+ TwitterCldr::Formatters::DateFormatter
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+
3
+ # Copyright 2012 Twitter, Inc
4
+ # http://www.apache.org/licenses/LICENSE-2.0
5
+
6
+ module TwitterCldr
7
+ module Localized
8
+
9
+ class LocalizedDateTime < LocalizedObject
10
+ attr_reader :calendar_type
11
+
12
+ def initialize(obj, locale, options = {})
13
+ super
14
+ @calendar_type = options[:calendar_type] || TwitterCldr::DEFAULT_CALENDAR_TYPE
15
+ end
16
+
17
+ TwitterCldr::Tokenizers::DateTimeTokenizer::VALID_TYPES.each do |format_type|
18
+ define_method "to_#{format_type}_s" do
19
+ @formatter.format(@base_obj, :type => format_type.to_sym)
20
+ end
21
+ end
22
+
23
+ def to_timespan(options = {})
24
+ base_time = options[:base_time] || Time.now
25
+ seconds = (self.to_time.base_obj.to_i - base_time.to_i).abs
26
+ TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(:locale => @locale, :direction => :none))
27
+ end
28
+
29
+ def ago(options = {})
30
+ base_time = options[:base_time] || Time.now
31
+ seconds = self.to_time.base_obj.to_i - base_time.to_i
32
+ raise ArgumentError.new('Start date is after end date. Consider using "until" function.') if seconds > 0
33
+ TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
34
+ end
35
+
36
+ def until(options = {})
37
+ base_time = options[:base_time] || Time.now
38
+ seconds = self.to_time.base_obj.to_i - base_time.to_i
39
+ raise ArgumentError.new('End date is before start date. Consider using "ago" function.') if seconds < 0
40
+ TwitterCldr::Localized::LocalizedTimespan.new(seconds, options.merge(:locale => @locale))
41
+ end
42
+
43
+ def to_s
44
+ to_default_s
45
+ end
46
+
47
+ def to_date
48
+ LocalizedDate.new(Date.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale, :calendar_type => @calendar_type)
49
+ end
50
+
51
+ def to_time
52
+ LocalizedTime.new(Time.parse(@base_obj.strftime("%Y-%m-%dT%H:%M:%S%z")), @locale, :calendar_type => @calendar_type)
53
+ end
54
+
55
+ protected
56
+
57
+ def formatter_const
58
+ TwitterCldr::Formatters::DateTimeFormatter
59
+ end
60
+ end
61
+
62
+ end
63
+ end