glossync 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cd9b544462c2d05d9ff02e06db91b943cc322f1e9cbee1ac4a14f8b287e06996
4
+ data.tar.gz: cac223ba8b7ed1be058a16f22de5665eba2d5cee63cba8c9ff93224dfef6dbb5
5
+ SHA512:
6
+ metadata.gz: ea8951faccca78562cb5c648b60971222c2b1bc845d2260f7319394a378ed6e8f04241b89d9189b37d375cc80c079fb78b526911d385ffb33bb76e4f896cdd73
7
+ data.tar.gz: 03db39e6f1c9ce3e4f7332e79487bb27e703b929fffc39b6094207d72e99dae17eae73594764719dda026c4370b3981ba3f9f971929d11a687ad316dc7636ce8
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in glossync.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ glossync (0.1.0)
5
+ i18n (~> 1.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ concurrent-ruby (1.1.4)
11
+ diff-lcs (1.3)
12
+ i18n (1.3.0)
13
+ concurrent-ruby (~> 1.0)
14
+ rake (10.5.0)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.2)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.17)
34
+ rake (~> 10.0)
35
+ rspec (~> 3.0)
36
+ glossync!
37
+
38
+ BUNDLED WITH
39
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Stone Tickle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Glossync
2
+ [![Build Status](https://travis-ci.org/annacrombie/glossync.svg?branch=master)](https://travis-ci.org/annacrombie/glossync)
3
+
4
+ Easily manage multiple translations for models in your database.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'glossync'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ``
18
+ bundle
19
+ ``
20
+
21
+ Or install it yourself as:
22
+
23
+ ``sh
24
+ gem install glossync
25
+ ``
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ class Question < ApplicationRecord
31
+ include Glossync
32
+ has_translations_for :question
33
+ end
34
+
35
+ I18n.locale = :en
36
+ Question.first.question #=> "What si your name?"
37
+ Question.first.update(question: "What is your name?")
38
+ I18n.locale = :ja
39
+ Question.first.update(question: "お名前はなんですか")
40
+ Question.first.question #=> "お名前はなんですか"
41
+ I18n.locale = :en
42
+ Question.first.question #=> "What is your name?"
43
+ ```
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/annacrombie/glossync.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'glossync'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/glossync.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'glossync/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'glossync'
7
+ spec.version = Glossync::VERSION
8
+ spec.authors = ['Stone Tickle']
9
+ spec.email = ['lattis@mochiro.moe']
10
+
11
+ spec.summary = "A simple mixin to help make ActiveRecord models with
12
+ localized strings easier to deal with"
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
16
+ `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ end
20
+
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.17'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+
29
+ spec.add_dependency 'i18n', '~> 1.1'
30
+ end
@@ -0,0 +1,9 @@
1
+ module Glossync
2
+ # A railtie to add rake tasks in to rails
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ Dir.glob(File.join(__dir__, '../tasks/*'))
6
+ .each { |task| load task }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module Glossync
2
+ class TlAdapter
3
+ include I18n::Backend::Base
4
+
5
+ TlElem = Struct.new(:value) do
6
+ def update(value:)
7
+ self.value = value
8
+ end
9
+ end
10
+
11
+ def initialize
12
+ super
13
+
14
+ @db = {}
15
+ end
16
+
17
+ def exists?(locale:, key:)
18
+ @db.key?(key) && @db[key].key?(locale)
19
+ end
20
+
21
+ def find_by(locale:, key:)
22
+ return nil unless exists?(locale: locale, key: key)
23
+
24
+ @db[key][locale]
25
+ end
26
+
27
+ def create(locale:, key:, value:)
28
+ @db[key] ||= {}
29
+ @db[key][locale] = TlElem.new(value)
30
+ end
31
+
32
+ def lookup(locale, key, *)
33
+ find_by(locale: locale, key: key).value
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Glossync
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/glossync.rb ADDED
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require 'glossync/version'
5
+ require 'glossync/tl_adapter'
6
+ require 'glossync/railtie' if defined?(Rails)
7
+
8
+ # Include into ActiveRecords to obtain some nice I18n features
9
+ module Glossync
10
+ # methods that match this pattern will be responded to in method_missing()
11
+ METHOD_PATTERN = /_translated$/.freeze
12
+
13
+ class << self
14
+ attr_writer :tl_store
15
+
16
+ def tl_store
17
+ return @tl_store unless @tl_store.nil?
18
+
19
+ Translation
20
+ rescue NameError
21
+ raise 'No Translation store'
22
+ end
23
+
24
+ # Should we call I18n.backend.reload! on each call to tl_update that
25
+ # modifies data?
26
+ def reload_backend_on_tl_update?
27
+ @reload_backend_on_tl_update ||= true
28
+ end
29
+
30
+ # :nodoc:
31
+ def reload_backend_on_tl_update=(bool)
32
+ @reload_backend_on_tl_update = !!bool
33
+ end
34
+
35
+ # called when a module is included, add methods to the base class
36
+ def append_features(rcvr)
37
+ super
38
+
39
+ # attr_writer for @glossync
40
+ rcvr.define_singleton_method(:has_translations_for) do |*keys|
41
+ @glossync =
42
+ keys.map(&:to_sym).reject { |k| glossync.include?(k) }.map do |k|
43
+ # overwrite the reader method for the specified key to just return
44
+ # the translation.
45
+ rcvr.define_method(k) do
46
+ tl(k)
47
+ end
48
+
49
+ # overwrite the writer method for the specified key to update the
50
+ # translation for the key as well as update the key
51
+ rcvr.define_method(:"#{k}=") do |val|
52
+ tl_update(k, val)
53
+ defined?(super) ? super(val) : val
54
+ end
55
+
56
+ k
57
+ end + glossync
58
+ end
59
+
60
+ # attr_reader for @glossync
61
+ rcvr.define_singleton_method(:glossync) do
62
+ @glossync || []
63
+ end
64
+ end
65
+ end
66
+
67
+ # This sticks some code in front of #update that also updates any
68
+ # translations that were modified
69
+ def update(hash)
70
+ hash.to_h.map { |k, v| [k, v] }.to_h # really convert it to a hash
71
+ .transform_keys(&:to_sym)
72
+ .select { |k, _| self.class.glossync.include?(k) }
73
+ .each { |k, v| tl_update(k, v) }
74
+
75
+ super if defined?(super)
76
+ end
77
+
78
+ # If you add _translated to the end of a glossync field, call tl_key
79
+ # on that field automatically
80
+ def method_missing(method, *args, &block)
81
+ if respond_to_missing?(method)
82
+ tl(method.to_s.gsub(METHOD_PATTERN, ''))
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ # Determines is a missing method name can be handled
89
+ def respond_to_missing?(method, include_private = false)
90
+ (METHOD_PATTERN.match?(method) &&
91
+ self.class
92
+ .glossync
93
+ .include?(method.to_s.gsub(METHOD_PATTERN, '').to_sym)) ||
94
+ super
95
+ end
96
+
97
+ # Creates a key for the translation of a field, suitable for I18n.t()
98
+ #
99
+ # The key consists of the name of the model downcased, the id of the model,
100
+ # and the name of the field translated, joined with a '.'
101
+ #
102
+ # @example QuizAnswer.first.tl_key(:answer) => "quizanswer.1.answer"
103
+ #
104
+ # @param field the field to generate a key for
105
+ # @return [String] a key
106
+ def tl_key(field)
107
+ [
108
+ self.class.name.split('::').last.downcase,
109
+ id,
110
+ field.to_s
111
+ ].join('.')
112
+ end
113
+
114
+ # Call I18n.translate for the key corresponding to field
115
+ #
116
+ # @example QuizAnswer.first.tl_key(:answer) => "True"
117
+ #
118
+ # @param field the field to translate
119
+ # @return [String] the translated string
120
+ def tl(field)
121
+ I18n.translate(tl_key(field))
122
+ end
123
+
124
+ # Update the translation associated with a field and locale
125
+ #
126
+ # This method creates a new Translation if no pre-existing translation is
127
+ # found
128
+ #
129
+ # @param field the field to update the translation of
130
+ # @param value the new translation
131
+ # @param locale the locale of the new translation, if none is specified, the
132
+ # default locale will be used
133
+ # @return [Boolean] wether or not an update was performed
134
+ def tl_update(field, value, locale = nil)
135
+ locale ||= I18n.locale
136
+ tls = Glossync.tl_store
137
+
138
+ h = { locale: locale, key: tl_key(field) }
139
+
140
+ if tls.exists?(h)
141
+ tl = tls.find_by(h)
142
+ return false if tl.value == value
143
+
144
+ tl.update(value: value)
145
+ else
146
+ tls.create(h.merge(value: value))
147
+ end
148
+
149
+ I18n.backend.reload! if Glossync.reload_backend_on_tl_update?
150
+
151
+ true
152
+ end
153
+ end
@@ -0,0 +1,90 @@
1
+ desc 'Extract and insert Translations for Glossync models'
2
+ task setup_model_translations: :environment do
3
+
4
+ # eager load all the rails constants
5
+ Rails.application.eager_load!
6
+ # don't reload the I18n backend on each tl_update
7
+ Glossync.reload_backend_on_tl_update = false
8
+ # require 'io/console' so we can get the terminal's width
9
+ require 'io/console'
10
+
11
+ # a lambda to insert translations, returns true if a translation was inserted
12
+ # or false otherwise
13
+ insert_translation = lambda do |locale|
14
+ lambda do |key, value|
15
+ if Translation.exists?(key: key)
16
+ false
17
+ else
18
+ Translation.create(locale: locale, key: key, value: value)
19
+ true
20
+ end
21
+ end
22
+ end
23
+
24
+ # handy lambda to calculate percents
25
+ pct = ->(y) { ->(x) { (x * 100.0) / y } }
26
+
27
+ # lambda to display a pretty log of our progress
28
+ log = ->(m, i, t, pct, a) do
29
+ graphw = ($stdout.winsize[1] - 70)
30
+ printf(
31
+ "\r[\e[35m%5.1f%%\e[0m] -- processing " +
32
+ "\e[34m%-20.20s\e[0m (\e[35m%5d\e[0m/\e[37m%5d\e[0m) " +
33
+ "\e[%sm%-8.8s\e[0m [\e[35m%-#{graphw}.#{graphw}s\e[0m]\e[K",
34
+ pct, m.to_s, i + 1, t, a == 'skipping' ? '33' : '32', a,
35
+ graphw.positive? ? ('#' * (graphw * pct / 100).round) : ''
36
+ )
37
+ end
38
+
39
+ # setup the translation lambda to insert english translations
40
+ tl = insert_translation.('en')
41
+
42
+ # get a list of models that include Glossync
43
+ models = ActiveRecord::Base.descendants.select do |c|
44
+ c.included_modules.include?(Glossync)
45
+ end
46
+
47
+ # setup some variables for displaying information
48
+ pct = pct.(models.map(&:count).reduce(0, :+))
49
+ x = 0
50
+ actions = { 'skipping' => 0, 'adding' => 0 }
51
+ start_time = Time.now
52
+
53
+ # this is the main logic of the task, for each model and each key within that
54
+ # model that is translatable, insert a new translation
55
+
56
+ # for each model that includes Glossync
57
+ models.each do |model|
58
+ # get a list of the keys that module ``has_translations_for''
59
+ keys = model.translatable
60
+ # c is the number of instances of this particular model (used later in
61
+ # printing the progress bar)
62
+ c = model.count
63
+
64
+ # for each instance of the model,
65
+ model.all.each_with_index do |e, i|
66
+ # for each key that has a translatoin call the above tl Proc an get an
67
+ # array of bools indicating wether or not the translation was inserted or
68
+ # skipped
69
+ status = keys.map {|k| tl.(e.tl_key(k), e[k]) }
70
+
71
+ # make a message indicating wether we added anything
72
+ msg = status.any? ? 'adding' : 'skipping'
73
+
74
+ # record the action count
75
+ actions[msg] += 1
76
+
77
+ # print some info
78
+ log.(model, i, c, pct.(x += 1), msg)
79
+ end
80
+ end
81
+
82
+ # print a summary
83
+ printf(
84
+ "\r[\e[35m100.0%%\e[0m] -- done, " +
85
+ "processed \e[35m%d\e[0m models " +
86
+ "(\e[33mskipped %d\e[0m, \e[32madded %d\e[0m) " +
87
+ "in \e[34m%.1f\e[0m seconds\e[K\n",
88
+ x, actions['skipping'], actions['adding'], Time.now - start_time
89
+ )
90
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glossync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stone Tickle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description:
70
+ email:
71
+ - lattis@mochiro.moe
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - glossync.gemspec
87
+ - lib/glossync.rb
88
+ - lib/glossync/railtie.rb
89
+ - lib/glossync/tl_adapter.rb
90
+ - lib/glossync/version.rb
91
+ - lib/tasks/setup_model_translations.rake
92
+ homepage:
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A simple mixin to help make ActiveRecord models with localized strings easier
116
+ to deal with
117
+ test_files: []