mobility 0.8.13 → 1.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +26 -0
- data/Gemfile +5 -2
- data/Gemfile.lock +79 -8
- data/README.md +183 -91
- data/lib/mobility.rb +40 -166
- data/lib/mobility/arel/nodes/pg_ops.rb +1 -1
- data/lib/mobility/backend.rb +19 -41
- data/lib/mobility/backends.rb +20 -0
- data/lib/mobility/backends/active_record.rb +4 -0
- data/lib/mobility/backends/active_record/column.rb +2 -0
- data/lib/mobility/backends/active_record/container.rb +4 -2
- data/lib/mobility/backends/active_record/hstore.rb +2 -0
- data/lib/mobility/backends/active_record/json.rb +2 -0
- data/lib/mobility/backends/active_record/jsonb.rb +2 -0
- data/lib/mobility/backends/active_record/key_value.rb +5 -3
- data/lib/mobility/backends/active_record/pg_hash.rb +1 -1
- data/lib/mobility/backends/active_record/serialized.rb +2 -0
- data/lib/mobility/backends/active_record/table.rb +5 -3
- data/lib/mobility/backends/column.rb +0 -6
- data/lib/mobility/backends/container.rb +2 -1
- data/lib/mobility/backends/hash.rb +39 -0
- data/lib/mobility/backends/hstore.rb +0 -1
- data/lib/mobility/backends/json.rb +0 -1
- data/lib/mobility/backends/jsonb.rb +0 -1
- data/lib/mobility/backends/key_value.rb +22 -14
- data/lib/mobility/backends/null.rb +2 -0
- data/lib/mobility/backends/sequel.rb +3 -0
- data/lib/mobility/backends/sequel/column.rb +2 -0
- data/lib/mobility/backends/sequel/container.rb +3 -1
- data/lib/mobility/backends/sequel/hstore.rb +2 -0
- data/lib/mobility/backends/sequel/json.rb +2 -0
- data/lib/mobility/backends/sequel/jsonb.rb +3 -1
- data/lib/mobility/backends/sequel/key_value.rb +8 -6
- data/lib/mobility/backends/sequel/serialized.rb +2 -0
- data/lib/mobility/backends/sequel/table.rb +5 -2
- data/lib/mobility/backends/serialized.rb +1 -3
- data/lib/mobility/backends/table.rb +14 -6
- data/lib/mobility/pluggable.rb +36 -0
- data/lib/mobility/plugin.rb +260 -0
- data/lib/mobility/plugins.rb +26 -25
- data/lib/mobility/plugins/active_model.rb +17 -0
- data/lib/mobility/plugins/active_model/cache.rb +26 -0
- data/lib/mobility/plugins/active_model/dirty.rb +112 -77
- data/lib/mobility/plugins/active_record.rb +34 -0
- data/lib/mobility/plugins/active_record/backend.rb +25 -0
- data/lib/mobility/plugins/active_record/cache.rb +28 -0
- data/lib/mobility/plugins/active_record/dirty.rb +34 -17
- data/lib/mobility/plugins/active_record/query.rb +43 -31
- data/lib/mobility/plugins/active_record/uniqueness_validation.rb +60 -0
- data/lib/mobility/plugins/attribute_methods.rb +28 -20
- data/lib/mobility/plugins/attributes.rb +70 -0
- data/lib/mobility/plugins/backend.rb +138 -0
- data/lib/mobility/plugins/backend_reader.rb +34 -0
- data/lib/mobility/plugins/cache.rb +59 -24
- data/lib/mobility/plugins/default.rb +22 -17
- data/lib/mobility/plugins/dirty.rb +12 -33
- data/lib/mobility/plugins/fallbacks.rb +51 -43
- data/lib/mobility/plugins/fallthrough_accessors.rb +20 -23
- data/lib/mobility/plugins/locale_accessors.rb +25 -35
- data/lib/mobility/plugins/presence.rb +28 -21
- data/lib/mobility/plugins/query.rb +8 -17
- data/lib/mobility/plugins/reader.rb +50 -0
- data/lib/mobility/plugins/sequel.rb +34 -0
- data/lib/mobility/plugins/sequel/backend.rb +25 -0
- data/lib/mobility/plugins/sequel/cache.rb +24 -0
- data/lib/mobility/plugins/sequel/dirty.rb +32 -21
- data/lib/mobility/plugins/sequel/query.rb +21 -6
- data/lib/mobility/plugins/writer.rb +44 -0
- data/lib/mobility/translations.rb +95 -0
- data/lib/mobility/version.rb +12 -1
- data/lib/rails/generators/mobility/templates/initializer.rb +95 -77
- metadata +28 -27
- metadata.gz.sig +0 -0
- data/lib/mobility/active_model.rb +0 -4
- data/lib/mobility/active_model/backend_resetter.rb +0 -26
- data/lib/mobility/active_record.rb +0 -23
- data/lib/mobility/active_record/backend_resetter.rb +0 -26
- data/lib/mobility/active_record/uniqueness_validator.rb +0 -60
- data/lib/mobility/attributes.rb +0 -324
- data/lib/mobility/backend/orm_delegator.rb +0 -44
- data/lib/mobility/backend_resetter.rb +0 -50
- data/lib/mobility/configuration.rb +0 -138
- data/lib/mobility/fallbacks.rb +0 -28
- data/lib/mobility/interface.rb +0 -0
- data/lib/mobility/loaded.rb +0 -4
- data/lib/mobility/plugins/active_record/attribute_methods.rb +0 -38
- data/lib/mobility/plugins/cache/translation_cacher.rb +0 -40
- data/lib/mobility/sequel.rb +0 -9
- data/lib/mobility/sequel/backend_resetter.rb +0 -23
- data/lib/mobility/translates.rb +0 -73
data/lib/mobility/fallbacks.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module Mobility
|
2
|
-
=begin
|
3
|
-
|
4
|
-
Subclasses +I18n::Locale::Fallbacks+ such that instances of this class
|
5
|
-
fall through to fallbacks defined in +I18n.fallbacks+. This allows models to
|
6
|
-
customize fallbacks while still falling through to any fallbacks defined
|
7
|
-
globally.
|
8
|
-
|
9
|
-
=end
|
10
|
-
class Fallbacks < ::I18n::Locale::Fallbacks
|
11
|
-
# @param [Symbol] locale
|
12
|
-
# @return [Array] locales
|
13
|
-
def [](locale)
|
14
|
-
super | I18n.fallbacks[locale]
|
15
|
-
end
|
16
|
-
|
17
|
-
# For this set of fallbacks, return a new fallbacks hash.
|
18
|
-
# @param [Hash] fallbacks
|
19
|
-
# @return [I18n::Locale::Fallbacks,Mobility::Fallbacks] fallbacks hash
|
20
|
-
def self.build(fallbacks)
|
21
|
-
if I18n.respond_to?(:fallbacks)
|
22
|
-
new(fallbacks)
|
23
|
-
else
|
24
|
-
I18n::Locale::Fallbacks.new(fallbacks)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/mobility/interface.rb
DELETED
File without changes
|
data/lib/mobility/loaded.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
module Mobility
|
2
|
-
module Plugins
|
3
|
-
module ActiveRecord
|
4
|
-
module TranslatedAttributes
|
5
|
-
def translated_attributes
|
6
|
-
{}
|
7
|
-
end
|
8
|
-
|
9
|
-
def attributes
|
10
|
-
super.merge(translated_attributes)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
=begin
|
15
|
-
|
16
|
-
Module builder adding translated attributes to #attributes hash on model
|
17
|
-
instance. See {Mobility::Plugins::AttributeMethods} for further details.
|
18
|
-
|
19
|
-
=end
|
20
|
-
class AttributeMethods < Module
|
21
|
-
def initialize(*attribute_names)
|
22
|
-
include TranslatedAttributes
|
23
|
-
define_method :translated_attributes do
|
24
|
-
super().merge(attribute_names.inject({}) do |attributes, name|
|
25
|
-
attributes.merge(name.to_s => send(name))
|
26
|
-
end)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def included(model_class)
|
31
|
-
model_class.class_eval do
|
32
|
-
define_method :untranslated_attributes, ::ActiveRecord::Base.instance_method(:attributes)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Mobility
|
2
|
-
module Plugins
|
3
|
-
module Cache
|
4
|
-
=begin
|
5
|
-
|
6
|
-
Creates a module to cache a given translation fetch method. The cacher defines
|
7
|
-
private methods +cache+ and +clear_cache+ to access and clear, respectively, a
|
8
|
-
translations hash.
|
9
|
-
|
10
|
-
This cacher is used to cache translation values in {Mobility::Plugins::Cache},
|
11
|
-
and also to cache translation *records* in {Mobility::Backends::Table} and
|
12
|
-
{Mobility::Backends::KeyValue}.
|
13
|
-
|
14
|
-
=end
|
15
|
-
class TranslationCacher < Module
|
16
|
-
# @param [Symbol] fetch_method Name of translation fetch method to cache
|
17
|
-
def initialize(fetch_method)
|
18
|
-
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
19
|
-
def #{fetch_method} locale, **options
|
20
|
-
return super(locale, options) if options.delete(:cache) == false
|
21
|
-
if cache.has_key?(locale)
|
22
|
-
cache[locale]
|
23
|
-
else
|
24
|
-
cache[locale] = super(locale, options)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
EOM
|
28
|
-
|
29
|
-
include CacheMethods
|
30
|
-
end
|
31
|
-
|
32
|
-
module CacheMethods
|
33
|
-
private
|
34
|
-
def cache; @cache ||= {}; end
|
35
|
-
def clear_cache; @cache = {}; end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
data/lib/mobility/sequel.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Mobility
|
2
|
-
module Sequel
|
3
|
-
=begin
|
4
|
-
|
5
|
-
Backend resetter for Sequel models. Triggers backend reset when +refresh+
|
6
|
-
method is called.
|
7
|
-
|
8
|
-
=end
|
9
|
-
class BackendResetter < Mobility::BackendResetter
|
10
|
-
|
11
|
-
# (see Mobility::BackendResetter#initialize)
|
12
|
-
def initialize(attribute_names, &block)
|
13
|
-
super
|
14
|
-
|
15
|
-
model_reset_method = @model_reset_method
|
16
|
-
|
17
|
-
define_method :refresh do
|
18
|
-
super().tap { instance_eval(&model_reset_method) }
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/mobility/translates.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Mobility
|
4
|
-
=begin
|
5
|
-
|
6
|
-
Defines methods for attaching backends to a class. A block can optionally be
|
7
|
-
passed to accessors to configure backend (see example below).
|
8
|
-
|
9
|
-
@example Defining backend on a class
|
10
|
-
class MyClass
|
11
|
-
extend Translates
|
12
|
-
mobility_accessor :foo, option: :value
|
13
|
-
end
|
14
|
-
|
15
|
-
@example Passing backend to a block
|
16
|
-
class MyClass
|
17
|
-
extend Translates
|
18
|
-
mobility_accessor :foo, option: :value do
|
19
|
-
# add custom code to backend class for this attribute only
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
@example Defining only a backend reader and presence method
|
24
|
-
class MyClass
|
25
|
-
extend Translates
|
26
|
-
mobility_reader :foo
|
27
|
-
end
|
28
|
-
|
29
|
-
instance = MyClass.new
|
30
|
-
instance.foo #=> (some value)
|
31
|
-
instance.foo? #=> true
|
32
|
-
instance.foo = "foo" #=> NoMethodError
|
33
|
-
|
34
|
-
@example Defining only a backend writer
|
35
|
-
class MyClass
|
36
|
-
extend Translates
|
37
|
-
mobility_writer :foo
|
38
|
-
end
|
39
|
-
|
40
|
-
instance = MyClass.new
|
41
|
-
instance.foo #=> NoMethodError
|
42
|
-
instance.foo? #=> NoMethodError
|
43
|
-
instance.foo = "foo" #=> (sets attribute to value "foo")
|
44
|
-
=end
|
45
|
-
module Translates
|
46
|
-
# Defines mobility accessor on model class.
|
47
|
-
# @!method mobility_accessor(*attributes, **options)
|
48
|
-
# @param [Array<String>] attributes
|
49
|
-
# @param [Hash] options
|
50
|
-
# @yield Yields to block with backend as context
|
51
|
-
|
52
|
-
# Defines mobility reader and presence method on model class.
|
53
|
-
# @!method mobility_reader(*attributes, **options)
|
54
|
-
# @param [Array<String>] attributes
|
55
|
-
# @param [Hash] options
|
56
|
-
# @yield Yields to block with backend as context
|
57
|
-
|
58
|
-
# Defines mobility writer on model class.
|
59
|
-
# @!method mobility_writer(*attributes, **options)
|
60
|
-
# @param [Array<String>] attributes
|
61
|
-
# @param [Hash] options
|
62
|
-
# @yield Yields to block with backend as context
|
63
|
-
%w[accessor reader writer].each do |method|
|
64
|
-
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
65
|
-
def mobility_#{method}(*args, **options, &block)
|
66
|
-
attributes = Attributes.new(*args, method: :#{method}, **options)
|
67
|
-
attributes.backend.instance_eval(&block) if block_given?
|
68
|
-
include attributes
|
69
|
-
end
|
70
|
-
EOM
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|