edtf-humanize 0.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +5 -5
  2. data/config/locales/en.edtf.yml +44 -0
  3. data/config/locales/fr.edtf.yml +55 -0
  4. data/lib/edtf-humanize.rb +5 -0
  5. data/lib/edtf/humanize.rb +22 -41
  6. data/lib/edtf/humanize/century.rb +4 -4
  7. data/lib/edtf/humanize/decade.rb +4 -4
  8. data/lib/edtf/humanize/interval.rb +4 -17
  9. data/lib/edtf/humanize/iso_date.rb +4 -4
  10. data/lib/edtf/humanize/language.rb +13 -0
  11. data/lib/edtf/humanize/language/default.rb +17 -0
  12. data/lib/edtf/humanize/language/default/century.rb +20 -0
  13. data/lib/edtf/humanize/language/default/decade.rb +21 -0
  14. data/lib/edtf/humanize/language/default/formats.rb +115 -0
  15. data/lib/edtf/humanize/language/default/interval.rb +132 -0
  16. data/lib/edtf/humanize/language/default/iso_date.rb +21 -0
  17. data/lib/edtf/humanize/language/default/season.rb +30 -0
  18. data/lib/edtf/humanize/language/default/set.rb +78 -0
  19. data/lib/edtf/humanize/language/default/unknown.rb +17 -0
  20. data/lib/edtf/humanize/language/english.rb +11 -0
  21. data/lib/edtf/humanize/language/french.rb +36 -0
  22. data/lib/edtf/humanize/season.rb +4 -4
  23. data/lib/edtf/humanize/set.rb +4 -12
  24. data/lib/edtf/humanize/unknown.rb +5 -3
  25. data/lib/edtf/humanize/version.rb +3 -1
  26. data/spec/edtf_humanize_en_spec.rb +102 -0
  27. data/spec/edtf_humanize_fr_spec.rb +110 -0
  28. data/spec/spec_helper.rb +103 -0
  29. metadata +102 -100
  30. data/Rakefile +0 -34
  31. data/lib/edtf/humanize/formats.rb +0 -73
  32. data/lib/tasks/edtf_humanize_tasks.rake +0 -4
  33. data/test/dummy/README.rdoc +0 -28
  34. data/test/dummy/Rakefile +0 -6
  35. data/test/dummy/app/assets/javascripts/application.js +0 -13
  36. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  37. data/test/dummy/app/controllers/application_controller.rb +0 -5
  38. data/test/dummy/app/helpers/application_helper.rb +0 -2
  39. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  40. data/test/dummy/bin/bundle +0 -3
  41. data/test/dummy/bin/rails +0 -4
  42. data/test/dummy/bin/rake +0 -4
  43. data/test/dummy/bin/setup +0 -29
  44. data/test/dummy/config.ru +0 -4
  45. data/test/dummy/config/application.rb +0 -26
  46. data/test/dummy/config/boot.rb +0 -5
  47. data/test/dummy/config/database.yml +0 -25
  48. data/test/dummy/config/environment.rb +0 -5
  49. data/test/dummy/config/environments/development.rb +0 -41
  50. data/test/dummy/config/environments/production.rb +0 -79
  51. data/test/dummy/config/environments/test.rb +0 -42
  52. data/test/dummy/config/initializers/assets.rb +0 -11
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  54. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  56. data/test/dummy/config/initializers/inflections.rb +0 -16
  57. data/test/dummy/config/initializers/mime_types.rb +0 -4
  58. data/test/dummy/config/initializers/session_store.rb +0 -3
  59. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  60. data/test/dummy/config/locales/en.yml +0 -23
  61. data/test/dummy/config/routes.rb +0 -56
  62. data/test/dummy/config/secrets.yml +0 -22
  63. data/test/dummy/db/test.sqlite3 +0 -0
  64. data/test/dummy/log/test.log +0 -865
  65. data/test/dummy/public/404.html +0 -67
  66. data/test/dummy/public/422.html +0 -67
  67. data/test/dummy/public/500.html +0 -66
  68. data/test/dummy/public/favicon.ico +0 -0
  69. data/test/edtf_humanize_test.rb +0 -78
  70. data/test/test_helper.rb +0 -19
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ module Interval
8
+ include Edtf::Humanize::Language::Default::Formats
9
+
10
+ extend self
11
+
12
+ def humanizer(date)
13
+ if date.from == :open || date.to == :open
14
+ open_interval(date)
15
+ else
16
+ "#{interval_prefix(date)}" \
17
+ "#{formatted_date(date.from)}" \
18
+ "#{interval_connector(date)}" \
19
+ "#{formatted_date(date.to)}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def open_interval(date)
26
+ if date.from == :open
27
+ open_start_interval(formatted_date: formatted_date(date.to),
28
+ precision: date.to.precision)
29
+ elsif date.to == :open
30
+ open_end_interval(formatted_date: formatted_date(date.from),
31
+ precision: date.from.precision)
32
+ end
33
+ end
34
+
35
+ # open/1980 => Jusqu'en 1980
36
+ def open_start_interval(formatted_date:, precision:)
37
+ case precision
38
+ when :year
39
+ I18n.t('edtf.terms.open_start_interval_with_year',
40
+ date: formatted_date,
41
+ default: "until #{formatted_date}")
42
+ when :month
43
+ I18n.t('edtf.terms.open_start_interval_with_month',
44
+ date: formatted_date,
45
+ default: "until #{formatted_date}")
46
+ when :day
47
+ I18n.t('edtf.terms.open_start_interval_with_day',
48
+ date: formatted_date,
49
+ default: "until #{formatted_date}")
50
+ end
51
+ end
52
+
53
+ # 1980/open => Depuis 1980
54
+ def open_end_interval(formatted_date:, precision:)
55
+ case precision
56
+ when :year
57
+ I18n.t('edtf.terms.open_end_interval_with_year',
58
+ date: formatted_date,
59
+ default: "since #{formatted_date}")
60
+ when :month
61
+ I18n.t('edtf.terms.open_end_interval_with_month',
62
+ date: formatted_date,
63
+ default: "since #{formatted_date}")
64
+ when :day
65
+ I18n.t('edtf.terms.open_end_interval_with_day',
66
+ date: formatted_date,
67
+ default: "since #{formatted_date}")
68
+ end
69
+ end
70
+
71
+ def formatted_date(date)
72
+ "#{apply_prefix_if_approximate(date)}" \
73
+ "#{date_format(date)}" \
74
+ "#{apply_suffix_if_approximate(date)}"
75
+ end
76
+
77
+ def interval_prefix(date)
78
+ case date.from.precision
79
+ when :year
80
+ I18n.t('edtf.terms.interval_prefix_year', default: '')
81
+ when :month
82
+ I18n.t('edtf.terms.interval_prefix_month', default: '')
83
+ when :day
84
+ I18n.t('edtf.terms.interval_prefix_day', default: '')
85
+ end
86
+ end
87
+
88
+ def interval_connector(date)
89
+ return interval_connector_open if date.to == :open
90
+
91
+ return interval_connector_approx if date.to.approximate.day ||
92
+ date.to.approximate.month ||
93
+ date.to.approximate.year
94
+
95
+ interval_connector_other(date)
96
+ end
97
+
98
+ def interval_connector_open
99
+ I18n.t('edtf.terms.interval_connector_open')
100
+ end
101
+
102
+ def interval_connector_approx
103
+ I18n.t('edtf.terms.interval_connector_approximate',
104
+ default: ' to ')
105
+ end
106
+
107
+ def interval_connector_other(date)
108
+ case date.to.precision
109
+ when :year
110
+ I18n.t('edtf.terms.interval_connector_year', default: ' to ')
111
+ when :month
112
+ I18n.t('edtf.terms.interval_connector_month', default: ' to ')
113
+ when :day
114
+ I18n.t('edtf.terms.interval_connector_day', default: ' to ')
115
+ end
116
+ end
117
+
118
+ # '198u/199u' => 1980s to 1990s
119
+ def apply_if_unspecified_year(date)
120
+ if date.respond_to?(:unspecified?) && date.unspecified?(:year)
121
+ return "#{date_precision(date)}" \
122
+ "#{I18n.t('edtf.terms.interval_unspecified_suffix',
123
+ default: 's')}"
124
+ end
125
+
126
+ date_precision(date)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ module IsoDate
8
+ include Edtf::Humanize::Language::Default::Formats
9
+
10
+ extend self
11
+
12
+ def humanizer(date)
13
+ "#{apply_prefix_if_approximate(date)}" \
14
+ "#{date_format(date)}" \
15
+ "#{apply_suffix_if_approximate(date)}"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ module Season
8
+ include Edtf::Humanize::Language::Default::Formats
9
+
10
+ extend self
11
+
12
+ def humanizer(date)
13
+ "#{apply_prefix_if_approximate(date)}"\
14
+ "#{translate_season(date.season)} #{date.year}"\
15
+ "#{apply_suffix_if_approximate(date)}"\
16
+ "#{apply_if_uncertain(date)}"
17
+ end
18
+
19
+ private
20
+
21
+ def translate_season(season)
22
+ I18n.translate!("date.seasons.#{season}")
23
+ rescue I18n::MissingTranslationData
24
+ season
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ module Set
8
+ include Edtf::Humanize::Language::Default::Formats
9
+
10
+ extend self
11
+
12
+ def humanizer(date)
13
+ format_set_entries(date).to_sentence(
14
+ words_connector:
15
+ I18n.t("edtf.terms.set_dates_connector_#{mode(date)}",
16
+ default: ', '),
17
+ last_word_connector:
18
+ I18n.t("edtf.terms.set_last_date_connector_#{mode(date)}",
19
+ default: default_word_connector(date)),
20
+ two_words_connector:
21
+ I18n.t("edtf.terms.set_two_dates_connector_#{mode(date)}",
22
+ default: default_word_connector(date))
23
+ )
24
+ end
25
+
26
+ private
27
+
28
+ def default_word_connector(date)
29
+ mode(date) == 'inclusive' ? ' and ' : ' or '
30
+ end
31
+
32
+ def format_set_entries(dates)
33
+ dates.entries.map.with_index do |date, index|
34
+ "#{apply_if_earlier(dates, index)}"\
35
+ "#{apply_if_later(dates, index)}"\
36
+ "#{apply_prefix_if_approximate(date)}"\
37
+ "#{date_format(date)}"\
38
+ "#{apply_suffix_if_approximate(date)}"\
39
+ end
40
+ end
41
+
42
+ # '[..1760-12-03]' => on or before December 3, 1760
43
+ def apply_if_earlier(dates, index)
44
+ return earlier_prefix(dates) if dates.earlier? && index.zero?
45
+ end
46
+
47
+ def earlier_prefix(dates)
48
+ I18n.t("edtf.terms.set_earlier_prefix_#{mode(dates)}",
49
+ default: earlier_prefix_default(dates))
50
+ end
51
+
52
+ def earlier_prefix_default(dates)
53
+ mode(dates) == 'inclusive' ? 'on and before ' : 'on or before '
54
+ end
55
+
56
+ # '[1760-12..]' => on or after December 1760
57
+ def apply_if_later(dates, index)
58
+ return later_prefix(dates) if dates.later? &&
59
+ (index + 1) == dates.size
60
+ end
61
+
62
+ def later_prefix(dates)
63
+ I18n.t("edtf.terms.set_later_prefix_#{mode(dates)}",
64
+ default: later_prefix_default(dates))
65
+ end
66
+
67
+ def later_prefix_default(dates)
68
+ mode(dates) == 'inclusive' ? 'on and after ' : 'on or after '
69
+ end
70
+
71
+ def mode(date)
72
+ date.choice? ? 'exclusive' : 'inclusive'
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ module Unknown
8
+ extend self
9
+
10
+ def humanizer(_date)
11
+ I18n.t('edtf.terms.unknown', default: 'unknown')
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module English
7
+ include Default
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module French
7
+ include Default
8
+
9
+ module Century
10
+ extend self
11
+
12
+ def humanizer(date)
13
+ require 'roman'
14
+ "#{(date.year.abs / 100 + 1).to_roman}" \
15
+ "#{century_number_suffix(date)}" \
16
+ "#{century_sign_suffix(date)}"
17
+ end
18
+
19
+ private
20
+
21
+ def century_number_suffix(date)
22
+ return 'er siècle' if (date.year.abs / 100).zero?
23
+
24
+ 'e siècle'
25
+ end
26
+
27
+ def century_sign_suffix(date)
28
+ return ' avant J.C.' if date.year.negative?
29
+
30
+ ''
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,13 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Edtf
2
4
  module Humanize
3
5
  module Season
4
-
5
- include Edtf::Humanize::Formats
6
+ include Edtf::Humanize::Language
6
7
 
7
8
  def humanize
8
- "#{apply_if_approximate(self)}#{self.season} #{self.year}#{apply_if_uncertain(self)}"
9
+ language_strategy::Season.humanizer(self)
9
10
  end
10
-
11
11
  end
12
12
  end
13
13
  end
@@ -1,21 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Edtf
2
4
  module Humanize
3
5
  module Set
4
-
5
- include Edtf::Humanize::Formats
6
+ include Edtf::Humanize::Language
6
7
 
7
8
  def humanize
8
- display = []
9
- self.entries.each do |date|
10
- display << "#{apply_if_approximate(date)}#{simple_date_format(date)}"
11
- end
12
- display.to_sentence(
13
- words_connector: Edtf::Humanize.configuration.set_dates_connector,
14
- last_word_connector: Edtf::Humanize.configuration.set_last_date_connector,
15
- two_words_connector: Edtf::Humanize.configuration.set_two_dates_connector
16
- )
9
+ language_strategy::Set.humanizer(self)
17
10
  end
18
-
19
11
  end
20
12
  end
21
13
  end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Edtf
2
4
  module Humanize
3
5
  module Unknown
6
+ include Edtf::Humanize::Language
4
7
 
5
8
  def humanize
6
- Edtf::Humanize.configuration.unknown
9
+ language_strategy::Unknown.humanizer(self)
7
10
  end
8
-
9
11
  end
10
12
  end
11
- end
13
+ end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Edtf
2
4
  module Humanize
3
- VERSION = "0.0.4"
5
+ VERSION = '2.0.0'
4
6
  end
5
7
  end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'edtf-humanize'
4
+
5
+ RSpec.describe Edtf::Humanize do
6
+ before do
7
+ I18n.locale = :en
8
+ end
9
+
10
+ it { is_expected.to be_a(Module) }
11
+
12
+ context 'with a decade' do
13
+ it 'returns a humanized decade string' do
14
+ expect(Date.edtf('199x').humanize).to eq('1990s')
15
+ end
16
+ end
17
+
18
+ context 'with a century' do
19
+ it 'returns a humanized century string' do
20
+ expect(Date.edtf('19xx').humanize).to eq('1900s')
21
+ end
22
+ end
23
+
24
+ context 'with an interval' do
25
+ it 'returns a humanized interval string' do
26
+ expect(Date.edtf('1970/1980').humanize).to eq('1970 to 1980')
27
+ end
28
+ end
29
+
30
+ context 'with an open interval' do
31
+ it 'returns a humanized interval string' do
32
+ expect(Date.edtf('1970/open').humanize).to eq('since 1970')
33
+ end
34
+ end
35
+
36
+ context 'with an appoximate interval'
37
+ it 'returns a humanized approximate interval string' do
38
+ expect(Date.edtf('1970~/1980~').humanize).to(
39
+ eq('circa 1970 to circa 1980')
40
+ )
41
+ end
42
+
43
+ context 'with an iso date' do
44
+ it 'returns a humanized ISO date string' do
45
+ expect(Date.edtf('1975-07-01').humanize).to eq('July 1, 1975')
46
+ end
47
+ end
48
+
49
+ context 'with an uncertain iso date' do
50
+ it 'returns a humanized uncertain ISO date string' do
51
+ expect(Date.edtf('1975?').humanize).to eq('1975?')
52
+ end
53
+ end
54
+
55
+ context 'with an unspecfic year iso date' do
56
+ it 'returns a humanized unspecified year ISO date string' do
57
+ expect(Date.edtf('197u').humanize).to eq('197x')
58
+ end
59
+ end
60
+
61
+ context 'with a season' do
62
+ it 'returns a humanized season string' do
63
+ expect(Date.edtf('1975-22').humanize).to eq('summer 1975')
64
+ end
65
+ end
66
+
67
+ context 'with a set' do
68
+ it 'returns a humanized exclusive set string' do
69
+ expect(Date.edtf('[1980, 1981, 1983]').humanize).to(
70
+ eq('1980, 1981 or 1983')
71
+ )
72
+ end
73
+
74
+ it 'returns a humanized inclusive set string' do
75
+ expect(Date.edtf('{1980, 1981, 1983}').humanize).to(
76
+ eq('1980, 1981 and 1983')
77
+ )
78
+ end
79
+
80
+ it 'returns a humanized open (before) exclusive set string' do
81
+ expect(Date.edtf('[..1980]').humanize).to eq('on or before 1980')
82
+ end
83
+
84
+ it 'returns a humanized open (after) exclusive set string' do
85
+ expect(Date.edtf('[1980..]').humanize).to eq('on or after 1980')
86
+ end
87
+
88
+ it 'returns a humanized open (before) inclusive set string' do
89
+ expect(Date.edtf('{..1980}').humanize).to eq('on and before 1980')
90
+ end
91
+
92
+ it 'returns a humanized open (after) inclusive set string' do
93
+ expect(Date.edtf('{1980..}').humanize).to eq('on and after 1980')
94
+ end
95
+ end
96
+
97
+ context 'with an unknown value' do
98
+ it 'returns a humanized unknown string' do
99
+ expect(Date.edtf('uuuu').humanize).to eq('unknown')
100
+ end
101
+ end
102
+ end