edtf-humanize 1.0.0 → 2.1.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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/config/locales/en.edtf.yml +44 -0
  3. data/config/locales/fr.edtf.yml +55 -0
  4. data/config/locales/it.edtf.yml +55 -0
  5. data/lib/edtf/humanize/century.rb +4 -4
  6. data/lib/edtf/humanize/decade.rb +4 -4
  7. data/lib/edtf/humanize/interval.rb +4 -21
  8. data/lib/edtf/humanize/iso_date.rb +4 -4
  9. data/lib/edtf/humanize/language/default/century.rb +20 -0
  10. data/lib/edtf/humanize/language/default/decade.rb +21 -0
  11. data/lib/edtf/humanize/language/default/formats.rb +112 -0
  12. data/lib/edtf/humanize/language/default/interval.rb +132 -0
  13. data/lib/edtf/humanize/language/default/iso_date.rb +21 -0
  14. data/lib/edtf/humanize/language/default/season.rb +30 -0
  15. data/lib/edtf/humanize/language/default/set.rb +78 -0
  16. data/lib/edtf/humanize/language/default/unknown.rb +17 -0
  17. data/lib/edtf/humanize/language/default.rb +17 -0
  18. data/lib/edtf/humanize/language/english.rb +11 -0
  19. data/lib/edtf/humanize/language/french.rb +36 -0
  20. data/lib/edtf/humanize/language/italian.rb +34 -0
  21. data/lib/edtf/humanize/language.rb +13 -0
  22. data/lib/edtf/humanize/season.rb +4 -16
  23. data/lib/edtf/humanize/set.rb +4 -33
  24. data/lib/edtf/humanize/unknown.rb +5 -3
  25. data/lib/edtf/humanize/version.rb +3 -1
  26. data/lib/edtf/humanize.rb +24 -45
  27. data/lib/edtf-humanize.rb +5 -0
  28. data/spec/edtf_humanize_en_spec.rb +102 -0
  29. data/spec/edtf_humanize_fr_spec.rb +110 -0
  30. data/spec/edtf_humanize_it_spec.rb +110 -0
  31. data/spec/spec_helper.rb +2 -0
  32. metadata +70 -20
  33. data/lib/edtf/humanize/formats.rb +0 -73
  34. data/spec/edtf_humanize_spec.rb +0 -65
@@ -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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Default
7
+ include Century
8
+ include Decade
9
+ include Interval
10
+ include IsoDate
11
+ include Season
12
+ include Set
13
+ include Unknown
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
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ module Italian
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}" \
16
+ "#{century_sign_suffix(date)}"
17
+ end
18
+
19
+ private
20
+
21
+ def century_number_suffix
22
+ ' secolo'
23
+ end
24
+
25
+ def century_sign_suffix(date)
26
+ return ' a. C.' if date.year.negative?
27
+
28
+ ''
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edtf
4
+ module Humanize
5
+ module Language
6
+ extend self
7
+
8
+ def language_strategy
9
+ Edtf::Humanize.configuration.language_strategy(I18n.locale)
10
+ end
11
+ end
12
+ end
13
+ 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.1.0'
4
6
  end
5
7
  end
data/lib/edtf/humanize.rb CHANGED
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Edtf
2
4
  module Humanize
3
-
4
5
  require 'edtf'
5
6
 
6
- require 'edtf/humanize/formats'
7
+ require 'edtf/humanize/language'
7
8
  require 'edtf/humanize/decade'
8
9
  require 'edtf/humanize/century'
9
10
  require 'edtf/humanize/season'
@@ -11,6 +12,18 @@ module Edtf
11
12
  require 'edtf/humanize/set'
12
13
  require 'edtf/humanize/unknown'
13
14
  require 'edtf/humanize/iso_date'
15
+ require 'edtf/humanize/language/default/formats'
16
+ require 'edtf/humanize/language/default/decade'
17
+ require 'edtf/humanize/language/default/century'
18
+ require 'edtf/humanize/language/default/season'
19
+ require 'edtf/humanize/language/default/interval'
20
+ require 'edtf/humanize/language/default/set'
21
+ require 'edtf/humanize/language/default/unknown'
22
+ require 'edtf/humanize/language/default/iso_date'
23
+ require 'edtf/humanize/language/default'
24
+ require 'edtf/humanize/language/english'
25
+ require 'edtf/humanize/language/french'
26
+ require 'edtf/humanize/language/italian'
14
27
 
15
28
  EDTF::Decade.include Edtf::Humanize::Decade
16
29
  EDTF::Century.include Edtf::Humanize::Century
@@ -20,7 +33,6 @@ module Edtf
20
33
  EDTF::Unknown.include Edtf::Humanize::Unknown
21
34
  Date.include Edtf::Humanize::IsoDate
22
35
 
23
-
24
36
  def self.configuration
25
37
  @configuration ||= Configuration.new
26
38
  end
@@ -34,51 +46,18 @@ module Edtf
34
46
  end
35
47
 
36
48
  class Configuration
37
-
38
- attr_accessor :day_precision_strftime_format,
39
- :month_precision_strftime_format,
40
- :year_precision_strftime_format,
41
- :approximate_date_prefix,
42
- :uncertain_date_suffix,
43
- :decade_suffix,
44
- :century_suffix,
45
- :unspecified_digit_substitute,
46
- :interval_connector,
47
- :interval_unspecified_suffix,
48
- :set_dates_connector,
49
- :set_last_date_connector,
50
- :set_two_dates_connector,
51
- :set_earlier_prefix,
52
- :set_later_prefix,
53
- :unknown
54
-
55
49
  def initialize
56
- @day_precision_strftime_format = "%B %-d, %Y"
57
- @month_precision_strftime_format = "%B %Y"
58
- @year_precision_strftime_format = "%Y"
59
-
60
- @approximate_date_prefix = "circa "
61
-
62
- @uncertain_date_suffix = "?"
63
-
64
- @decade_suffix = "s"
65
- @century_suffix = "s"
66
-
67
- @unspecified_digit_substitute = "x"
68
-
69
- @interval_connector = " to "
70
- @interval_unspecified_suffix = "s"
71
-
72
- @set_dates_connector = ", "
73
- @set_last_date_connector = " or "
74
- @set_two_dates_connector = " or "
75
- @set_earlier_prefix = "on or before "
76
- @set_later_prefix = "on or after "
77
-
78
- @unknown = "unknown"
50
+ @language_strategies = {
51
+ default: Edtf::Humanize::Language::Default,
52
+ en: Edtf::Humanize::Language::English,
53
+ fr: Edtf::Humanize::Language::French,
54
+ it: Edtf::Humanize::Language::Italian
55
+ }
79
56
  end
80
57
 
58
+ def language_strategy(language)
59
+ @language_strategies[language.to_sym] || @language_strategies[:default]
60
+ end
81
61
  end
82
-
83
62
  end
84
63
  end
data/lib/edtf-humanize.rb CHANGED
@@ -1 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'edtf/humanize'
4
+
5
+ I18n.load_path +=
6
+ Dir.glob("#{File.dirname(__FILE__)}/../config/locales/*.{rb,yml}")
@@ -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 approximate 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 unspecified 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
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'edtf-humanize'
4
+
5
+ RSpec.describe Edtf::Humanize do
6
+ before do
7
+ I18n.locale = :fr
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('Les années 1990')
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('XXe siècle')
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('De 1970 à 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('Depuis 1970')
33
+ end
34
+ end
35
+
36
+ context 'with an approximate interval'
37
+ it 'returns a humanized approximate interval string' do
38
+ expect(Date.edtf('1970~/1980~').humanize).to(
39
+ eq('De 1970 environ à 1980 environ')
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('1 Juillet 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 unspecified 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('été 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 ou 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 et 1983')
77
+ )
78
+ end
79
+
80
+ it 'returns a humanized open (before) exclusive set string' do
81
+ expect(Date.edtf('[..1980]').humanize).to(
82
+ eq('Le ou avant 1980')
83
+ )
84
+ end
85
+
86
+ it 'returns a humanized open (after) exclusive set string' do
87
+ expect(Date.edtf('[1980..]').humanize).to(
88
+ eq('Le ou après 1980')
89
+ )
90
+ end
91
+
92
+ it 'returns a humanized open (before) inclusive set string' do
93
+ expect(Date.edtf('{..1980}').humanize).to(
94
+ eq('Le et avant 1980')
95
+ )
96
+ end
97
+
98
+ it 'returns a humanized open (after) inclusive set string' do
99
+ expect(Date.edtf('{1980..}').humanize).to(
100
+ eq('Le et après 1980')
101
+ )
102
+ end
103
+ end
104
+
105
+ context 'with an unknown value' do
106
+ it 'returns a humanized unknown string' do
107
+ expect(Date.edtf('uuuu').humanize).to eq('Inconnue')
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'edtf-humanize'
4
+
5
+ RSpec.describe Edtf::Humanize do
6
+ before do
7
+ I18n.locale = :it
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('anni 1990')
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('XX secolo')
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 - 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('1970 - ')
33
+ end
34
+ end
35
+
36
+ context 'with an approximate interval'
37
+ it 'returns a humanized approximate interval string' do
38
+ expect(Date.edtf('1970~/1980~').humanize).to(
39
+ eq('circa 1970 - 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('1 luglio 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 unspecified 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('estate 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 o 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 e 1983')
77
+ )
78
+ end
79
+
80
+ it 'returns a humanized open (before) exclusive set string' do
81
+ expect(Date.edtf('[..1980]').humanize).to(
82
+ eq('prima di 1980')
83
+ )
84
+ end
85
+
86
+ it 'returns a humanized open (after) exclusive set string' do
87
+ expect(Date.edtf('[1980..]').humanize).to(
88
+ eq('dopo 1980')
89
+ )
90
+ end
91
+
92
+ it 'returns a humanized open (before) inclusive set string' do
93
+ expect(Date.edtf('{..1980}').humanize).to(
94
+ eq('prima di 1980')
95
+ )
96
+ end
97
+
98
+ it 'returns a humanized open (after) inclusive set string' do
99
+ expect(Date.edtf('{1980..}').humanize).to(
100
+ eq('dopo 1980')
101
+ )
102
+ end
103
+ end
104
+
105
+ context 'with an unknown value' do
106
+ it 'returns a humanized unknown string' do
107
+ expect(Date.edtf('uuuu').humanize).to eq('data sconosciuta')
108
+ end
109
+ end
110
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file was generated by the `rspec --init` command. Conventionally, all
2
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
5
  # The generated `.rspec` file contains `--require spec_helper` which will cause