translates 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02f3c68a373d5e35193cc5b9f28f5b97a2c36f612fc6ec85d38bf7f4ce6f1081
4
+ data.tar.gz: 5c620c75c5adf392fdd7aa14456df50a970699debcbaef827fac03531cb0a0eb
5
+ SHA512:
6
+ metadata.gz: b20fe3e5634caf3e8b3860c5034dc9677d7fe6b4d7edf40f26ae777a4f04d78d1422e4f01c8fa076cc00ce76953b9b3b161df0955707712197367a039787a080
7
+ data.tar.gz: cc212c5724604aef05e5612764152a1d0fa194521d5ef53009756909882cc0e13efbbc47e716417012eefd99d2ae2417c8ea4e6ae45117c53306fe0e62773291
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 translates.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Chris Salzberg
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.
@@ -0,0 +1,39 @@
1
+ # Translates
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/translates`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'translates'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install translates
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shioyama/translates.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,33 @@
1
+ require "translates/version"
2
+
3
+ module Translates
4
+ def translates(*attributes, backend:, plugins: [])
5
+ backend_subclass = Class.new(backend)
6
+ plugins.each { |plugin| backend_subclass.include plugin }
7
+
8
+ attributes.each do |attribute|
9
+ define_accessor(attribute)
10
+ define_backend(attribute, backend_subclass)
11
+ end
12
+ backend_subclass.setup_model(self, attributes)
13
+ end
14
+
15
+ private
16
+
17
+ def define_backend(attribute, backend_class)
18
+ define_method "#{attribute}_backend" do
19
+ @backends ||= {}
20
+ @backends[attribute] ||= backend_class.new(self, attribute)
21
+ end
22
+ end
23
+
24
+ def define_accessor(attribute)
25
+ define_method(attribute) do
26
+ send("#{attribute}_backend").read(I18n.locale)
27
+ end
28
+
29
+ define_method("#{attribute}=") do |value|
30
+ send("#{attribute}_backend").write(I18n.locale, value)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ module Translates
2
+ class ColumnBackend
3
+ def initialize(model, attribute)
4
+ @model, @attribute = model, attribute
5
+ end
6
+
7
+ def read(locale)
8
+ @model.read_attribute(column(locale))
9
+ end
10
+
11
+ def write(locale, value)
12
+ @model.write_attribute(column(locale), value)
13
+ end
14
+
15
+ def self.setup_model(*)
16
+ end
17
+
18
+ private
19
+
20
+ def column(locale)
21
+ "#{@attribute}_#{locale.to_s.downcase.gsub('-', '_')}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Translates
2
+ module FallbacksPlugin
3
+ def read(locale)
4
+ fallback_locales(locale).each do |locale|
5
+ value = super(locale)
6
+ return value if present?(value)
7
+ end
8
+ nil
9
+ end
10
+
11
+ def fallback_locales(locale)
12
+ [locale, I18n.default_locale]
13
+ end
14
+
15
+ def present?(value)
16
+ !value.nil? && (value != "")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Translates
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Salzberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.10
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.10
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ description:
76
+ email:
77
+ - csalzberg@degica.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - lib/translates.rb
87
+ - lib/translates/column_backend.rb
88
+ - lib/translates/fallbacks_plugin.rb
89
+ - lib/translates/version.rb
90
+ homepage: https://github.com/shioyama/translates
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Minimal implementation of backend/plugin-based translation framework.
114
+ test_files: []