tolken 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tolken
4
+ module Translates
5
+ def translates(field_name, options = {})
6
+ define_getter(field_name)
7
+ serialize(field_name, HashSerializer)
8
+
9
+ if options[:presence]
10
+ store_accessor(field_name, *I18n.available_locales.map { |locale| "#{field_name}_#{locale}" })
11
+ define_validator(field_name) if options[:presence]
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def define_getter(field_name)
18
+ define_method(field_name) do |locale = nil|
19
+ return self[field_name.to_sym] unless locale
20
+
21
+ begin
22
+ self[field_name.to_sym].fetch(locale.to_s)
23
+ rescue IndexError
24
+ raise ArgumentError, "Invalid locale #{locale}" unless I18n.available_locales.include?(locale.to_sym)
25
+ nil
26
+ end
27
+ end
28
+ end
29
+
30
+ def define_validator(field)
31
+ validate(
32
+ proc do
33
+ invalid = I18n.available_locales.map do |locale|
34
+ errors.add(:"#{field}_#{locale}", :blank) if self[field][locale.to_s].blank?
35
+ end
36
+
37
+ errors.add(field) if invalid.compact.present?
38
+ end
39
+ )
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tolken
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,40 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "tolken/version"
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "tolken"
10
+ spec.version = Tolken::VERSION
11
+ spec.authors = ["Nicklas Ramhöj, Julia Fridberg"]
12
+ spec.email = ["nicklas.ramhoj@varvet.com, julia.fridberg@varvet.com"]
13
+
14
+ spec.summary = "A down-to-earth database translation Gem for Rails using psql jsonb"
15
+ spec.description = """
16
+ #{spec.summary}. Designed to be less magic than most other translation projects.
17
+ Tolken's API is more verbose than most similar gems. The philosophy is that you should be aware of when you're dealing with translatable fields and what language you're interested in in any given moment. This comes from experience working with gems such as [Globalize](https://github.com/globalize/globalize), while it might fit some projects we've found that the magic that starts out as a convenience quickly becomes a liability.
18
+ In Tolken a translatable field is just a Ruby hash which makes it easy to reason about. See *Usage* for details.
19
+ """
20
+ spec.homepage = "https://github.com/varvet/tolken"
21
+ spec.license = "MIT"
22
+
23
+ spec.files = %x(git ls-files -z).split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "rails", "5.2.0.rc2"
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.16"
31
+ spec.add_development_dependency "pg", "~> 1.0"
32
+ spec.add_development_dependency "pry", "~>0.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "rubocop", "0.52.1"
36
+ spec.add_development_dependency "rubocop-rspec", "~> 1.23"
37
+ spec.add_development_dependency "simple_form", "~> 1.4"
38
+ spec.add_development_dependency "with_model", "~> 2.0"
39
+ spec.add_development_dependency "simplecov", "~> 0.16"
40
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tolken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicklas Ramhöj, Julia Fridberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0.rc2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0.rc2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pg
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.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.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.52.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.52.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.23'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.23'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simple_form
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.4'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.4'
139
+ - !ruby/object:Gem::Dependency
140
+ name: with_model
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.16'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.16'
167
+ description: "\n A down-to-earth database translation Gem for Rails using psql
168
+ jsonb. Designed to be less magic than most other translation projects.\n Tolken's
169
+ API is more verbose than most similar gems. The philosophy is that you should be
170
+ aware of when you're dealing with translatable fields and what language you're interested
171
+ in in any given moment. This comes from experience working with gems such as [Globalize](https://github.com/globalize/globalize),
172
+ while it might fit some projects we've found that the magic that starts out as a
173
+ convenience quickly becomes a liability.\n In Tolken a translatable field is
174
+ just a Ruby hash which makes it easy to reason about. See *Usage* for details.\n
175
+ \ "
176
+ email:
177
+ - nicklas.ramhoj@varvet.com, julia.fridberg@varvet.com
178
+ executables: []
179
+ extensions: []
180
+ extra_rdoc_files: []
181
+ files:
182
+ - ".gitignore"
183
+ - ".rspec"
184
+ - ".rubocop.yml"
185
+ - ".travis.yml"
186
+ - Gemfile
187
+ - Gemfile.lock
188
+ - LICENSE.txt
189
+ - README.md
190
+ - Rakefile
191
+ - bin/console
192
+ - bin/setup
193
+ - lib/tolken.rb
194
+ - lib/tolken/hash_serializer.rb
195
+ - lib/tolken/simle_form.rb
196
+ - lib/tolken/translates.rb
197
+ - lib/tolken/version.rb
198
+ - tolken.gemspec
199
+ homepage: https://github.com/varvet/tolken
200
+ licenses:
201
+ - MIT
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
212
+ required_rubygems_version: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - ">="
215
+ - !ruby/object:Gem::Version
216
+ version: '0'
217
+ requirements: []
218
+ rubyforge_project:
219
+ rubygems_version: 2.7.3
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: A down-to-earth database translation Gem for Rails using psql jsonb
223
+ test_files: []