transilator 0.1.5

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
+ SHA1:
3
+ metadata.gz: e0f009b5dd9214e74a92e739c42ad3f32c35c3fd
4
+ data.tar.gz: 46227bcf9dcfeefd70f755311ea61d9e3af95e95
5
+ SHA512:
6
+ metadata.gz: 792b88055096cb842a2bdffba5ec0932e2e4280c791ca1375589a08450c3746d2e6414ce8b8aa7ebc5c6ce055caae6c3d9eafa4b9cd1d0c82acacfd2b51dc069
7
+ data.tar.gz: 64b1226aa3d7a46af12d0f22525e0e18d61e51c7d787f66258db212ea91c32398dc87ec41261a78d837f7dc2c318d6bf1303edddd4e8148dce60120c6f708e6b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transilator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # Transilator
2
+
3
+ [![Build Status](https://travis-ci.org/hendricius/transilator.svg?branch=master)](https://travis-ci.org/hendricius/transilator)
4
+
5
+ Transilator makes storing translations for database records painless.
6
+
7
+ Instead of storing translations in different database tables, translations for your attributes are stored in a JSON/JSONB or Hstore column directly in your table. Internally everything is stored as a hash with keys being locales and values the translations. Based on the locale whenever calling the attribute the translated value is retrieved internally.
8
+
9
+ Everything has been developed with performance in mind, especially when dealing with millions of records.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'transilator'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install transilator
26
+
27
+ ## Usage
28
+
29
+ This is a walkthrough with all steps you need to get you up and running.
30
+
31
+ You may need to create migration for your model as usual. Assuming you have no
32
+ table yet, create the table:
33
+
34
+ ```ruby
35
+ create_table :posts do |t|
36
+ t.hstore :title # note for this to work you need to enable the hstore extension in your database.
37
+ t.jsonb :summary # this is the suggested way.
38
+ t.timestamps
39
+ end
40
+ ```
41
+
42
+ Then adjust your model and integrate transilator:
43
+
44
+ ```ruby
45
+ class Post < ActiveRecord::Base
46
+ transilator :title, :summary
47
+ end
48
+ ```
49
+
50
+ Thats it!
51
+
52
+ Now you are able to translate values for the attributes :title and :description per locale:
53
+
54
+ ```ruby
55
+ I18n.locale = :en
56
+ post.title = 'Eel is a typical food in Hamburg'
57
+ I18n.locale = :de
58
+ post.title = 'Aal ist typisch für Hamburg'
59
+
60
+ I18n.locale = :en
61
+ post.title #=> Eel is a typical food in Hamburg
62
+ I18n.locale = :de
63
+ post.title #=> Aal ist typisch für Hamburg
64
+ ```
65
+
66
+ If this is not enough, and sometimes you want to access one of the attributes directly you can also do:
67
+
68
+
69
+ ```ruby
70
+ post.title_en = 'I have no idea what I am doing'
71
+ post.title_en #=> I have no idea what I am doing
72
+ ```
73
+
74
+
75
+ You may use initialization if needed:
76
+
77
+ Post.new(title: {en: 'The German', de: 'Serr German'})
78
+
79
+ ## Performance ##
80
+
81
+ When developing I tested against against [bithavoc/multilang-hstore](https://github.com/bithavoc/multilang-hstore) as this has been my choice for years.
82
+
83
+ **1,000,000 translation read operations**
84
+
85
+ transilator:
86
+ ```
87
+ 1.310000 0.020000 1.330000 ( 1.342136)
88
+ ```
89
+
90
+ multilang-hstore:
91
+ ```
92
+ 3.350000 0.040000 3.390000 ( 3.383317)
93
+ ```
94
+
95
+
96
+ **Setting a new translation entry, 100,000 times**
97
+
98
+ transilator:
99
+ ```
100
+ 6.650000 0.030000 6.680000 ( 6.681483)
101
+ ```
102
+
103
+ multilang-hstore:
104
+ ```
105
+ 6.860000 0.030000 6.890000 ( 6.892723)
106
+ ```
107
+
108
+ **Setting a completely new object with translations 100,000 times**
109
+
110
+ transilator:
111
+
112
+ ```
113
+ 6.980000 0.020000 7.000000 ( 7.000557)
114
+ ```
115
+
116
+ multilang-hstore:
117
+
118
+ ```
119
+ 7.620000 0.040000 7.660000 ( 7.669240)
120
+ ```
121
+
122
+ ## Bugs and Feedback
123
+
124
+ Use [http://github.com/hendricius/transilator/issues](http://github.com/hendricius/transilator/issues)
125
+
126
+ ## Development
127
+
128
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
129
+
130
+ 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).
131
+
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hendricius/transilator
136
+
137
+ ## License(MIT)
138
+
139
+ * Copyright (c) 2017 Hendrik Kleinwaechter and contributors
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "transilator"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,15 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.4.1
4
+
5
+ test:
6
+ override:
7
+ - bundle exec rake test
8
+
9
+ database:
10
+ override:
11
+ - echo "Skipping database install..."
12
+
13
+ dependencies:
14
+ override:
15
+ - gem install bundler && (bundle check --path=/home/ubuntu/.bundle || bundle install --path=/home/ubuntu/.bundle --jobs=2 --retry=5)
@@ -0,0 +1,5 @@
1
+ require "transilator/version"
2
+ require "transilator/active_record_extensions"
3
+
4
+ module Transilator
5
+ end
@@ -0,0 +1,69 @@
1
+ module Transilator
2
+ module ActiveRecordExtensions
3
+
4
+ module ClassMethods
5
+
6
+ def transilator(*args)
7
+
8
+
9
+ args.each do |attribute|
10
+
11
+ # read the attribute from the hstore attribute and return it.
12
+ #
13
+ # returns the attribute or an empty string if the attribute is nil
14
+ define_method attribute do
15
+ (self[attribute] || {})[I18n.locale.to_s] || ""
16
+ end
17
+
18
+ # set the attribute on the model on the hstore hash
19
+ #
20
+ # returns the new value
21
+ define_method "#{attribute}=" do |value|
22
+ if value.is_a?(Hash)
23
+ write_attribute attribute, value
24
+ elsif self[attribute].present?
25
+ write_attribute attribute, self[attribute].merge({I18n.locale => value })
26
+ value
27
+ else
28
+ write_attribute attribute, {I18n.locale => value }
29
+ value
30
+ end
31
+ end
32
+
33
+ # returns the raw hstore directly
34
+ define_method "#{attribute}_before_type_cast" do
35
+ self[attribute] || {}
36
+ end
37
+
38
+ module_eval do
39
+ serialize "#{attribute}", ActiveRecord::Coders::Hstore
40
+ end if defined?(ActiveRecord::Coders::Hstore)
41
+
42
+
43
+ # for each locale add a getter and setter method in the type of
44
+ # title_de or title_en
45
+ I18n.available_locales.map(&:to_s).each do |locale|
46
+
47
+ define_method "#{attribute}_#{locale}" do
48
+ (self[attribute] || {})[locale] || ""
49
+ end
50
+
51
+ define_method "#{attribute}_#{locale}=" do |value|
52
+ if self[attribute].present?
53
+ write_attribute attribute, self[attribute].merge({locale => value })
54
+ else
55
+ write_attribute attribute, {locale => value }
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+
63
+
64
+ end
65
+
66
+ end
67
+ end
68
+
69
+ ActiveRecord::Base.extend Transilator::ActiveRecordExtensions::ClassMethods
@@ -0,0 +1,3 @@
1
+ module Transilator
2
+ VERSION = "0.1.5"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transilator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transilator"
8
+ spec.version = Transilator::VERSION
9
+ spec.authors = ['Hendrik Kleinwaechter']
10
+ spec.email = ['hendrik.kleinwaechter@gmail.com']
11
+
12
+ spec.summary = %q{Fast and efficient model translations in the database for PostgreSQL hstore and JSON}
13
+ spec.description = %q{Fast and efficient model translations in the databse for PostgreSQL hstore and JSON.}
14
+ spec.homepage = "https://github.com/hendricius/transilator"
15
+ spec.licenses = ['MIT']
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', "~> 1.10"
23
+ spec.add_development_dependency 'rake', "~> 10.0"
24
+ spec.add_development_dependency 'minitest', "~> 5.0"
25
+ spec.add_development_dependency 'pry', "~> 0.1"
26
+
27
+ spec.add_dependency 'activerecord', '~> 5.0'
28
+ spec.add_dependency 'pg', '~> 0.19'
29
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transilator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Hendrik Kleinwaechter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-10 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.19'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.19'
97
+ description: Fast and efficient model translations in the databse for PostgreSQL hstore
98
+ and JSON.
99
+ email:
100
+ - hendrik.kleinwaechter@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - README.md
108
+ - Rakefile
109
+ - bin/console
110
+ - bin/setup
111
+ - circle.yml
112
+ - lib/transilator.rb
113
+ - lib/transilator/active_record_extensions.rb
114
+ - lib/transilator/version.rb
115
+ - transilator.gemspec
116
+ homepage: https://github.com/hendricius/transilator
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.13
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Fast and efficient model translations in the database for PostgreSQL hstore
140
+ and JSON
141
+ test_files: []