edtf-humanize 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,25 +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)}"\
9
- "#{translate_season(self.season)} #{self.year}"\
10
- "#{apply_if_uncertain(self)}"
11
- end
12
-
13
- private
14
-
15
- def translate_season(season)
16
- begin
17
- I18n.translate!("date.seasons.#{self.season}")
18
- rescue I18n::MissingTranslationData
19
- self.season
20
- end
9
+ language_strategy::Season.humanizer(self)
21
10
  end
22
-
23
11
  end
24
12
  end
25
13
  end
@@ -1,42 +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
- format_set_entries(self).to_sentence(
9
- words_connector: Edtf::Humanize.configuration.set_dates_connector,
10
- last_word_connector: Edtf::Humanize.configuration.set_last_date_connector,
11
- two_words_connector: Edtf::Humanize.configuration.set_two_dates_connector
12
- )
13
- end
14
-
15
- private
16
-
17
- def format_set_entries(dates)
18
- dates.entries.map.with_index { |date, index|
19
- "#{apply_if_earlier(dates, index)}"\
20
- "#{apply_if_later(dates, index)}"\
21
- "#{apply_if_approximate(date)}"\
22
- "#{simple_date_format(date)}"
23
- }
9
+ language_strategy::Set.humanizer(self)
24
10
  end
25
-
26
- # '[..1760-12-03]' => on or before December 3, 1760
27
- def apply_if_earlier(dates, index)
28
- if dates.earlier? && index == 0
29
- Edtf::Humanize.configuration.set_earlier_prefix
30
- end
31
- end
32
-
33
- # '[1760-12..]' => on or after December 1760
34
- def apply_if_later(dates, index)
35
- if dates.later? && (index + 1) == dates.size
36
- Edtf::Humanize.configuration.set_later_prefix
37
- end
38
- end
39
-
40
11
  end
41
12
  end
42
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 = "1.0.0"
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