i18n-migrations 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba15d5100518deebb6bfd7a43960ee5c5f76e92e6cf2b998a5d69066321e7dff
4
- data.tar.gz: 5e9816cf86492b7b5dd4c012fa402030c6db4d8cb2429cce411bbe38ac695803
3
+ metadata.gz: a61642308e6f7429d13a4c6f1b0f6789b75af51091b3966474c72fe1834e79f9
4
+ data.tar.gz: 046affdbc02080b90a1d18e0b89f668a025e404e25016cab3036fa7151c1680f
5
5
  SHA512:
6
- metadata.gz: 171849dde3e0cc39e5d50e9a5632e03d36c5601dad85e47c6d639ed5c58461403457887bb45fd4164229f84220552aaf5b0a933ed73bb59e595bcd7a57626ccd
7
- data.tar.gz: 14c98f74cd3e493af2fb9cf65f787025bc226d9bbe1802c1d3bcc0986bdb23d1a1f088e4eaa55c777ee8a754c1a21c6e1847edfaa86017d3765e92dcc350433a
6
+ metadata.gz: b2383e487b6aff057aa3e78d831b692344bab7058ce9b94df3ee95b9e559fd9bbeb8112c5b75315097d5473679380f1f1ee9a0e46beb83b045ad2d1a501da5ed
7
+ data.tar.gz: ee172e7cda5416a57e31370dc694c5df868217b780a6de51b3eb99e6389d298e6f82d8c9997faaf19e9b9b6c1c821be1066714b82c9461e2d4bb659cda1dec04
data/.ruby-gemset CHANGED
@@ -1 +1 @@
1
- transparentclassroom
1
+ i18n-migrations
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.5.5
1
+ ruby-2.6.4
@@ -1,32 +1,42 @@
1
1
  module I18n
2
2
  module Migrations
3
3
  class Migration
4
+ class Logger
5
+ def initialize(verbose: false)
6
+ @verbose = verbose
7
+ end
8
+
9
+ def info(message)
10
+ puts message if @verbose
11
+ end
12
+ end
13
+
14
+ # translations = facade over translations
4
15
  # locale = en | es | ...
5
- # data = all keys -> all translations in this locale
6
- # notes = some keys -> notes about the translation in this locale
7
16
  # dictionary = call dictionary.lookup(term) to get localized version of a term
8
17
  # direction = :up | :down (up when migrating, down when rolling back)
9
- def initialize(locale, data, notes, dictionary, direction = :up, verbose = false)
10
- @locale, @data, @notes, @dictionary, @direction, @verbose = locale, data, notes, dictionary, direction, verbose
18
+ def initialize(translations:, locale_code:, dictionary:, direction: :up, logger: Logger.new)
19
+ @translations, @locale_code, @dictionary, @direction, @logger =
20
+ translations, locale_code, dictionary, direction, logger
11
21
  end
12
22
 
13
23
  # Overrides can be provided, e.g. { es: 'El foo de la barro' }
14
24
  def add(key, term, overrides = {})
15
25
  if @direction == :up
16
- info "adding #{key}"
26
+ @logger.info "adding #{key}"
17
27
  _add key, term, overrides
18
28
  else
19
- info "unadding #{key}"
29
+ @logger.info "unadding #{key}"
20
30
  _rm key
21
31
  end
22
32
  end
23
33
 
24
34
  def mv(old_key, new_key)
25
35
  if @direction == :up
26
- info "moving #{old_key} => #{new_key}"
36
+ @logger.info "moving #{old_key} => #{new_key}"
27
37
  _mv old_key, new_key
28
38
  else
29
- info "moving back #{new_key} => #{old_key}"
39
+ @logger.info "moving back #{new_key} => #{old_key}"
30
40
  _mv new_key, old_key
31
41
  end
32
42
  end
@@ -34,10 +44,10 @@ module I18n
34
44
  # Overrides can be provided, e.g. { es: 'El foo de la barro' }
35
45
  def rm(key, old_term, overrides = {})
36
46
  if @direction == :up
37
- info "removing #{key}"
47
+ @logger.info "removing #{key}"
38
48
  _rm key
39
49
  else
40
- info "unremoving #{key}"
50
+ @logger.info "unremoving #{key}"
41
51
  _add key, old_term, overrides
42
52
  end
43
53
  end
@@ -45,10 +55,10 @@ module I18n
45
55
  # Overrides can be provided, e.g. { es: 'El foo de la barro' }
46
56
  def update(key, new_term, old_term, overrides = {})
47
57
  if @direction == :up
48
- info "updating #{key}"
58
+ @logger.info "updating #{key}"
49
59
  _update key, new_term, overrides
50
60
  else
51
- info "unupdating #{key}"
61
+ @logger.info "unupdating #{key}"
52
62
  _update key, old_term, {}
53
63
  end
54
64
  end
@@ -63,7 +73,7 @@ module I18n
63
73
  def _mv(from, to)
64
74
  assert_exists! from
65
75
  assert_does_not_exist! to
66
- @data[to] = @data.delete(from)
76
+ @translations.move_term(from, to)
67
77
  end
68
78
 
69
79
  def _update(key, term, overrides)
@@ -73,27 +83,28 @@ module I18n
73
83
 
74
84
  def _rm(key)
75
85
  assert_exists! key
76
- @data.delete(key)
77
- @notes.delete(key)
78
- end
79
-
80
- def info(message)
81
- puts message if @verbose
86
+ delete_translation key
82
87
  end
83
88
 
84
89
  def assert_exists!(key)
85
- raise "#{key} doesn't exist in #{@locale}" unless @data.has_key?(key)
90
+ raise "#{key} doesn't exist in #{@locale_code}" unless @translations.get_term(key)
86
91
  end
87
92
 
88
93
  def assert_does_not_exist!(key)
89
- raise "#{key} already exists in #{@locale}" if @data.has_key?(key)
94
+ raise "#{key} already exists in #{@locale_code}" if @translations.get_term(key)
95
+ end
96
+
97
+ # should delete key & return translation
98
+ def delete_translation(key)
99
+ @translations.delete_term(key)
90
100
  end
91
101
 
92
102
  def assign_translation(key, term, overrides)
93
- if overrides[@locale.to_sym]
94
- @data[key] = overrides[@locale.to_sym]
103
+ if overrides[@locale_code.to_sym]
104
+ @translations.set_term(key, value: overrides[@locale_code.to_sym])
95
105
  else
96
- @data[key], @notes[key] = @dictionary.lookup(term, key: key)
106
+ value, notes = @dictionary.lookup(term, key: key)
107
+ @translations.set_term(key, value: value, notes: notes)
97
108
  end
98
109
  end
99
110
  end
@@ -18,14 +18,50 @@ module I18n
18
18
  raise("Can't parse version: #{version}") unless version =~ /^(\d{12})_(.*)/
19
19
  migration_class_name = "#{$2.camelcase}#{$1}"
20
20
 
21
+ translations = Translations.new(data: data, notes: notes)
21
22
  migration = begin
22
- migration_class_name.constantize.new(locale, data, notes, dictionary, direction)
23
+ migration_class_name.constantize.new(locale_code: locale,
24
+ translations: translations,
25
+ dictionary: dictionary,
26
+ direction: direction)
23
27
  rescue
24
28
  raise "Couldn't load migration #{migration_class_name} in #{filename}"
25
29
  end
26
30
 
27
31
  migration.change
28
32
  end
33
+
34
+ # This is a facade over our translations
35
+ # data = all keys -> all translations in this locale
36
+ # notes = some keys -> notes about the translation in this locale
37
+ class Translations
38
+ def initialize(data:, notes:)
39
+ @data, @notes = data, notes
40
+ end
41
+
42
+ def get_term(key)
43
+ @data[key]
44
+ end
45
+
46
+ def set_term(key, value:, notes: nil)
47
+ @data[key] = value
48
+ if notes.present?
49
+ @notes[key] = notes
50
+ else
51
+ @notes.delete(key)
52
+ end
53
+ end
54
+
55
+ def delete_term(key)
56
+ @data.delete(key)
57
+ @notes.delete(key)
58
+ end
59
+
60
+ def move_term(from, to)
61
+ @data[to] = @data.delete(from)
62
+ @notes[to] = @notes.delete(from)
63
+ end
64
+ end
29
65
  end
30
66
  end
31
67
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Migrations
3
- VERSION = "1.0.6"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Lightsmith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-06 00:00:00.000000000 Z
11
+ date: 2019-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  requirements: []
154
- rubygems_version: 3.0.3
154
+ rubygems_version: 3.0.6
155
155
  signing_key:
156
156
  specification_version: 4
157
157
  summary: Migrations for doing i18n.