/
mobility.rb
264 lines (222 loc) · 7.6 KB
/
mobility.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require 'i18n'
require 'request_store'
require 'mobility/version'
%w[object string].each do |type|
begin
require "active_support/core_ext/#{type}"
rescue LoadError
require "mobility/core_ext/#{type}"
end
end
=begin
Mobility is a gem for storing and retrieving localized data through attributes
on a class. The {Mobility} module includes all necessary methods and modules to
support defining backend accessors on a class.
To enable Mobility on a class, simply include or extend the {Mobility} module,
and define any attribute accessors using {Translates#mobility_accessor} (aliased to the
value of {Mobility.accessor_method}, which defaults to +translates+).
class MyClass
include Mobility
translates :title, backend: :key_value
end
When defining this module, Mobility attempts to +require+ various gems (for
example, +active_record+ and +sequel+) to evaluate which are loaded. Loaded
gems are tracked with dynamic subclasses of the {Loaded} module and referenced
in backends to define gem-dependent behavior.
=end
module Mobility
autoload :Attributes, "mobility/attributes"
autoload :Backend, "mobility/backend"
autoload :Backends, "mobility/backends"
autoload :BackendResetter, "mobility/backend_resetter"
autoload :Configuration, "mobility/configuration"
autoload :Plugins, "mobility/plugins"
autoload :Translates, "mobility/translates"
autoload :Wrapper, "mobility/wrapper"
require "mobility/orm"
# General error for version compatibility conflicts
class VersionNotSupportedError < ArgumentError; end
begin
require "active_record"
raise VersionNotSupportedError, "Mobility is only compatible with ActiveRecord 4.2 and greater" if ::ActiveRecord::VERSION::STRING < "4.2"
autoload :ActiveModel, "mobility/active_model"
autoload :ActiveRecord, "mobility/active_record"
Loaded::ActiveRecord = true
rescue LoadError
Loaded::ActiveRecord = false
end
begin
require "rails"
Loaded::Rails = true
require "rails/generators/mobility/generators"
rescue LoadError
Loaded::Rails = false
end
begin
require "sequel"
raise VersionNotSupportedError, "Mobility is only compatible with Sequel 4.0 and greater" if ::Sequel::MAJOR < 4
require "sequel/plugins/mobility"
require "sequel/extensions/inflector"
require "sequel/plugins/dirty"
autoload :Sequel, "mobility/sequel"
Loaded::Sequel = true
rescue LoadError
Loaded::Sequel = false
end
class << self
def extended(model_class)
return if model_class.respond_to? :mobility_accessor
model_class.include(InstanceMethods)
model_class.extend(ClassMethods)
if translates = Mobility.config.accessor_method
model_class.singleton_class.send(:alias_method, translates, :mobility_accessor)
end
if Loaded::ActiveRecord
model_class.include(ActiveRecord) if model_class < ::ActiveRecord::Base
model_class.include(ActiveRecord::AttributeMethods) if model_class.ancestors.include?(::ActiveRecord::AttributeMethods)
end
if Loaded::Sequel
model_class.include(Sequel) if model_class < ::Sequel::Model
end
end
# Extends model with this class so that +include Mobility+ is equivalent to
# +extend Mobility+
# @param model_class
def included(model_class)
model_class.extend self
end
# @!group Locale Accessors
# @return [Symbol] Mobility locale
def locale
read_locale || I18n.locale
end
# Sets Mobility locale
# @param [Symbol] locale Locale to set
# @raise [InvalidLocale] if locale is nil or not in
# +I18n.available_locales+ (if +I18n.enforce_available_locales+ is +true+)
# @return [Symbol] Locale
def locale=(locale)
set_locale(locale)
end
# Sets Mobility locale around block
# @param [Symbol] locale Locale to set in block
# @yield [Symbol] Locale
def with_locale(locale)
previous_locale = read_locale
begin
set_locale(locale)
yield(locale)
ensure
set_locale(previous_locale)
end
end
# @!endgroup
# @return [RequestStore] Request store
def storage
RequestStore.store
end
# @!group Configuration Methods
# @return [Mobility::Configuration] Mobility configuration
def config
@configuration ||= Mobility::Configuration.new
end
# (see Mobility::Configuration#accessor_method)
# @!method accessor_method
#
# (see Mobility::Configuration#query_method)
# @!method query_method
# (see Mobility::Configuration#default_fallbacks)
# @!method default_fallbacks
# (see Mobility::Configuration#default_backend)
# @!method default_backend
# (see Mobility::Configuration#default_options)
# @!method default_options
#
# (see Mobility::Configuration#plugins)
# @!method plugins
#
# (see Mobility::Configuration#default_accessor_locales)
# @!method default_accessor_locales
%w[accessor_method query_method default_backend default_options plugins default_accessor_locales].each do |method_name|
define_method method_name do
config.public_send(method_name)
end
end
define_method :default_fallbacks do |*args|
config.public_send(:default_fallbacks, *args)
end
# Configure Mobility
# @yield [Mobility::Configuration] Mobility configuration
def configure
yield config
end
# @!endgroup
# Return normalized locale
# @param [String,Symbol] locale
# @return [String] Normalized locale
# @example
# Mobility.normalize_locale(:ja)
# #=> "ja"
# Mobility.normalize_locale("pt-BR")
# #=> "pt_br"
def normalize_locale(locale = Mobility.locale)
"#{locale.to_s.downcase.sub("-", "_")}".freeze
end
alias_method :normalized_locale, :normalize_locale
# Return normalized locale accessor name
# @param [String,Symbol] attribute
# @param [String,Symbol] locale
# @return [String] Normalized locale accessor name
# @example
# Mobility.normalize_locale_accessor(:foo, :ja)
# #=> "foo_ja"
# Mobility.normalize_locale_accessor(:bar, "pt-BR")
# #=> "bar_pt_br"
def normalize_locale_accessor(attribute, locale = Mobility.locale)
"#{attribute}_#{normalize_locale(locale)}".freeze
end
# Raises InvalidLocale exception if the locale passed in is present but not available.
# @param [String,Symbol] locale
# @raise [InvalidLocale] if locale is present but not available
def enforce_available_locales!(locale)
if I18n.enforce_available_locales
raise Mobility::InvalidLocale.new(locale) unless (I18n.locale_available?(locale) || locale.nil?)
end
end
protected
def read_locale
storage[:mobility_locale]
end
def set_locale(locale)
locale = locale.to_sym if locale
enforce_available_locales!(locale)
storage[:mobility_locale] = locale
end
end
module InstanceMethods
# Fetch backend for an attribute
# @param [String] attribute Attribute
def mobility_backend_for(attribute)
send(Backend.method_name(attribute))
end
def initialize_dup(other)
@mobility_backends = nil
super
end
end
module ClassMethods
include Translates
def mobility
@mobility ||= Mobility::Wrapper.new(self)
end
def translated_attribute_names
mobility.translated_attribute_names
end
def inherited(subclass)
subclass.instance_variable_set(:@mobility, @mobility.dup)
super
end
end
class BackendRequired < ArgumentError; end
class InvalidLocale < I18n::InvalidLocale; end
end