mobility 1.2.0 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5cedc8fabef42f478f93a4bb4fa1e8764759855dcded4d25f397c018f1811d2
4
- data.tar.gz: 1fb647fdbcbafa3b8a5a6d47d71500e2481556aa72674c5269ea0dc560d63ca0
3
+ metadata.gz: d0dd974f8a077b31fc0ab41df158e4cac50c6aee4e7fd66348db1ed2ef5f78f9
4
+ data.tar.gz: ea72521a4bb8daedf08d3012353c591ec47f76e3fe6ec3c10b87e331a5404db9
5
5
  SHA512:
6
- metadata.gz: ee138cb5544fc8942dc8a6dbf7fcfd54bf3829329075950755b8fea3ba676c430b880b14366dcfc09b66651b06577957e02130c5b4f2a62d7db96d02f1a150c8
7
- data.tar.gz: 980502d8c285bedf8e2958a24cb77c129ee39f1cfb946c44bbd83f7cc89b30009a829bfb17b515aac9e02d3f3a810b1433cab416aabec67c1daab9c6f100683d
6
+ metadata.gz: 15b9df45774c9b3d4ae343b2f083b2a85ddfd7585283998b0dc624aa62eee5c6e5fade0737ae78b3dd728dd273a03dcf10afecabcca6eb8f90400619de0a7fa4
7
+ data.tar.gz: 1f199821139bf35b4e176f52dac0249f482ffb5edc9d7957d437e9262a908812fab01ba5dea52e18a9b1f37f92744b345c34ca0c84d97da0641f9fab19577704
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,22 +1,38 @@
1
1
  # Mobility Changelog
2
2
 
3
- ## Unreleased
4
- - Assign blank values in pg hash backends
5
- ([#516](https://github.com/shioyama/mobility/pull/516))
3
+ ## 1.2
6
4
 
7
- ## 1.1
5
+ ### 1.2.4
6
+ - Fix fallbacks performance regression
7
+ ([#548](https://github.com/shioyama/mobility/pull/548))
8
8
 
9
- ## 1.2
9
+ ### 1.2.3
10
+ - Fix passing wrong options to super in fallbacks plugin
11
+ ([#539](https://github.com/shioyama/mobility/pull/539))
12
+
13
+ ### 1.2.2
14
+ - Make models work with `Marshal.dump`
15
+ ([#532](https://github.com/shioyama/mobility/pull/532))
16
+ - Fix Sequel container op in Sequel
17
+ ([#533](https://github.com/shioyama/mobility/pull/533))
18
+ - Simplify Fallbacks plugin
19
+ ([#531](https://github.com/shioyama/mobility/pull/531))
20
+
21
+ ### 1.2.1
22
+ - Refactor ColumnFallback plugin
23
+ ([#530](https://github.com/shioyama/mobility/pull/530))
10
24
 
11
25
  ### 1.2.0
12
26
  - Add ColumnFallback plugin
13
- ([#512](https://github.com/shioyama/mobility/pull/512))
27
+ ([#512](https://github.com/shioyama/mobility/pull/512))
14
28
  - Fix Sequel querying on untranslated attributes in `i18n` block
15
- ([#529](https://github.com/shioyama/mobility/pull/529))
29
+ ([#529](https://github.com/shioyama/mobility/pull/529))
16
30
  - Allow passing configured backend class as third argument to setup
17
- ([#528](https://github.com/shioyama/mobility/pull/528))
31
+ ([#528](https://github.com/shioyama/mobility/pull/528))
18
32
  - Clearly distinguish backend classes from their configured subclasses
19
- ([#527](Clearly distinguish backend classes from their configured subclasses))
33
+ ([#527](https://github.com/shioyama/mobility/pull/527))
34
+
35
+ ## 1.1
20
36
 
21
37
  ### 1.1.3
22
38
  - Do not swallow keyword args on ruby 3 in fallthrough accessors
data/Gemfile.lock CHANGED
@@ -1,28 +1,44 @@
1
+ GIT
2
+ remote: https://github.com/rails/rails.git
3
+ revision: 5647a9c1ced68d20338552d47a3b755e10a271c4
4
+ branch: main
5
+ specs:
6
+ activemodel (7.0.0.alpha2)
7
+ activesupport (= 7.0.0.alpha2)
8
+ activerecord (7.0.0.alpha2)
9
+ activemodel (= 7.0.0.alpha2)
10
+ activesupport (= 7.0.0.alpha2)
11
+ activesupport (7.0.0.alpha2)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+
1
17
  PATH
2
18
  remote: .
3
19
  specs:
4
- mobility (1.2.0.alpha)
20
+ mobility (1.3.0.alpha)
5
21
  i18n (>= 0.6.10, < 2)
6
22
  request_store (~> 1.0)
7
23
 
8
24
  GEM
9
25
  remote: https://rubygems.org/
10
26
  specs:
11
- benchmark-ips (2.8.4)
27
+ benchmark-ips (2.9.1)
12
28
  byebug (11.1.3)
13
29
  coderay (1.1.3)
14
30
  concurrent-ruby (1.1.9)
15
31
  database_cleaner (1.99.0)
16
32
  diff-lcs (1.4.4)
17
- ffi (1.15.0)
18
- formatador (0.2.5)
19
- guard (2.16.2)
33
+ ffi (1.15.4)
34
+ formatador (0.3.0)
35
+ guard (2.18.0)
20
36
  formatador (>= 0.2.4)
21
37
  listen (>= 2.7, < 4.0)
22
38
  lumberjack (>= 1.0.12, < 2.0)
23
39
  nenv (~> 0.1)
24
40
  notiffany (~> 0.0)
25
- pry (>= 0.9.12)
41
+ pry (>= 0.13.0)
26
42
  shellany (~> 0.0)
27
43
  thor (>= 0.18.1)
28
44
  guard-compat (1.2.1)
@@ -32,11 +48,12 @@ GEM
32
48
  rspec (>= 2.99.0, < 4.0)
33
49
  i18n (1.8.10)
34
50
  concurrent-ruby (~> 1.0)
35
- listen (3.5.1)
51
+ listen (3.7.0)
36
52
  rb-fsevent (~> 0.10, >= 0.10.3)
37
53
  rb-inotify (~> 0.9, >= 0.9.10)
38
54
  lumberjack (1.2.8)
39
55
  method_source (1.0.0)
56
+ minitest (5.14.4)
40
57
  nenv (0.3.0)
41
58
  notiffany (0.1.3)
42
59
  nenv (~> 0.1)
@@ -50,7 +67,7 @@ GEM
50
67
  pry (~> 0.13.0)
51
68
  rack (2.2.3)
52
69
  rake (12.3.3)
53
- rb-fsevent (0.10.4)
70
+ rb-fsevent (0.11.0)
54
71
  rb-inotify (0.10.1)
55
72
  ffi (~> 1.0)
56
73
  request_store (1.5.0)
@@ -68,15 +85,18 @@ GEM
68
85
  diff-lcs (>= 1.2.0, < 2.0)
69
86
  rspec-support (~> 3.10.0)
70
87
  rspec-support (3.10.2)
71
- sequel (5.48.0)
72
88
  shellany (0.0.1)
73
89
  thor (1.1.0)
90
+ tzinfo (2.0.4)
91
+ concurrent-ruby (~> 1.0)
74
92
  yard (0.9.26)
75
93
 
76
94
  PLATFORMS
77
95
  ruby
78
96
 
79
97
  DEPENDENCIES
98
+ activerecord!
99
+ activesupport!
80
100
  benchmark-ips
81
101
  database_cleaner (~> 1.5, >= 1.5.3)
82
102
  guard-rspec
@@ -85,7 +105,6 @@ DEPENDENCIES
85
105
  pry-byebug
86
106
  rake (~> 12, >= 12.2.1)
87
107
  rspec (~> 3.0)
88
- sequel (~> 5.0)
89
108
  yard (~> 0.9.0)
90
109
 
91
110
  BUNDLED WITH
data/README.md CHANGED
@@ -55,7 +55,7 @@ Installation
55
55
  Add this line to your application's Gemfile:
56
56
 
57
57
  ```ruby
58
- gem 'mobility', '~> 1.2.0'
58
+ gem 'mobility', '~> 1.2.4'
59
59
  ```
60
60
 
61
61
  ### ActiveRecord (Rails)
@@ -77,6 +77,12 @@ On top of this, a backend will normally:
77
77
  @attribute = args[1]
78
78
  end
79
79
 
80
+ def ==(backend)
81
+ backend.class == self.class &&
82
+ backend.attribute == attribute &&
83
+ backend.model == model
84
+ end
85
+
80
86
  # @!macro [new] backend_reader
81
87
  # Gets the translated value for provided locale from configured backend.
82
88
  # @param [Symbol] locale Locale to read
@@ -100,7 +100,7 @@ Implements the {Mobility::Backends::Container} backend for Sequel models.
100
100
  # @return [Mobility::Backends::Sequel::Container::JSONOp,Mobility::Backends::Sequel::Container::JSONBOp]
101
101
  def self.build_op(attr, locale)
102
102
  klass = const_get("#{options[:column_type].upcase}Op")
103
- klass.new(klass.new(column_name.to_sym)[locale.to_s]).get_text(attr)
103
+ klass.new(klass.new(column_name.to_sym).get(locale.to_s)).get_text(attr)
104
104
  end
105
105
 
106
106
  class JSONOp < ::Sequel::Postgres::JSONOp; end
@@ -14,120 +14,47 @@ Plugin to use an original column for a given locale, and otherwise use the backe
14
14
  requires :column_fallback, include: false
15
15
 
16
16
  included_hook do |_, backend_class|
17
- case (column_fallback = options[:column_fallback])
17
+ backend_class.include BackendInstanceMethods
18
+ backend_class.extend BackendClassMethods
19
+ end
20
+
21
+ def self.use_column_fallback?(options, locale)
22
+ case column_fallback = options[:column_fallback]
18
23
  when TrueClass
19
- backend_class.include I18nDefaultLocaleBackend
20
- when Array, Proc
21
- backend_class.include BackendModule.new(column_fallback)
24
+ locale == I18n.default_locale
25
+ when Array
26
+ column_fallback.include?(locale)
27
+ when Proc
28
+ column_fallback.call(locale)
22
29
  else
23
- raise ArgumentError, "column_fallback value must be a boolean, an array of locales or a proc"
30
+ false
24
31
  end
25
32
  end
26
33
 
27
- module I18nDefaultLocaleBackend
34
+ module BackendInstanceMethods
28
35
  def read(locale, **)
29
- locale == I18n.default_locale ? model.read_attribute(attribute) : super
36
+ if ColumnFallback.use_column_fallback?(options, locale)
37
+ model.read_attribute(attribute)
38
+ else
39
+ super
40
+ end
30
41
  end
31
42
 
32
43
  def write(locale, value, **)
33
- locale == I18n.default_locale ? model.send(:write_attribute, attribute, value) : super
34
- end
35
-
36
- def self.included(base)
37
- base.extend(ClassMethods)
38
- end
39
-
40
- module ClassMethods
41
- def build_node(attr, locale)
42
- if locale == I18n.default_locale
43
- model_class.arel_table[attr]
44
- else
45
- super
46
- end
44
+ if ColumnFallback.use_column_fallback?(options, locale)
45
+ model.send(:write_attribute, attribute, value)
46
+ else
47
+ super
47
48
  end
48
49
  end
49
50
  end
50
51
 
51
- class BackendModule < Module
52
- def initialize(column_fallback)
53
- case (@column_fallback = column_fallback)
54
- when Array
55
- define_array_accessors
56
- when Proc
57
- define_proc_accessors
58
- end
59
- end
60
-
61
- def included(base)
62
- base.extend(ClassMethods.new(@column_fallback))
63
- end
64
-
65
- private
66
-
67
- def define_array_accessors
68
- column_fallback = @column_fallback
69
-
70
- module_eval <<-EOM, __FILE__, __LINE__ + 1
71
- def read(locale, **)
72
- if #{column_fallback}.include?(locale)
73
- model.read_attribute(attribute)
74
- else
75
- super
76
- end
77
- end
78
-
79
- def write(locale, value, **)
80
- if #{column_fallback}.include?(locale)
81
- model.send(:write_attribute, attribute, value)
82
- else
83
- super
84
- end
85
- end
86
- EOM
87
- end
88
-
89
- def define_proc_accessors
90
- column_fallback = @column_fallback
91
-
92
- define_method :read do |locale, **options|
93
- if column_fallback.call(locale)
94
- model.read_attribute(attribute)
95
- else
96
- super(locale, **options)
97
- end
98
- end
99
-
100
- define_method :write do |locale, value, **options|
101
- if column_fallback.call(locale)
102
- model.send(:write_attribute, attribute, value)
103
- else
104
- super(locale, value, **options)
105
- end
106
- end
107
- end
108
-
109
- class ClassMethods < Module
110
- def initialize(column_fallback)
111
- case column_fallback
112
- when Array
113
- module_eval <<-EOM, __FILE__, __LINE__ + 1
114
- def build_node(attr, locale)
115
- if #{column_fallback}.include?(locale)
116
- model_class.arel_table[attr]
117
- else
118
- super
119
- end
120
- end
121
- EOM
122
- when Proc
123
- define_method(:build_node) do |attr, locale|
124
- if column_fallback.call(locale)
125
- model_class.arel_table[attr]
126
- else
127
- super(attr, locale)
128
- end
129
- end
130
- end
52
+ module BackendClassMethods
53
+ def build_node(attr, locale)
54
+ if ColumnFallback.use_column_fallback?(options, locale)
55
+ model_class.arel_table[attr]
56
+ else
57
+ super
131
58
  end
132
59
  end
133
60
  end
@@ -114,15 +114,33 @@ Defines:
114
114
  defaults[key] = [backend, backend_options] if backend
115
115
  end
116
116
 
117
+ class MobilityBackends < Hash
118
+ def initialize(model)
119
+ @model = model
120
+ super()
121
+ end
122
+
123
+ def [](name)
124
+ return fetch(name) if has_key?(name)
125
+ return self[name.to_sym] if String === name
126
+ self[name] = @model.class.mobility_backend_class(name).new(@model, name.to_s)
127
+ end
128
+
129
+ def marshal_dump
130
+ @model
131
+ end
132
+
133
+ def marshal_load(model)
134
+ @model = model
135
+ end
136
+ end
137
+
117
138
  module InstanceMethods
118
139
  # Return a new backend for an attribute name.
119
140
  # @return [Hash] Hash of attribute names and backend instances
120
141
  # @api private
121
142
  def mobility_backends
122
- @mobility_backends ||= ::Hash.new do |hash, name|
123
- next hash[name.to_sym] if String === name
124
- hash[name] = self.class.mobility_backend_class(name).new(self, name.to_s)
125
- end
143
+ @mobility_backends ||= MobilityBackends.new(self)
126
144
  end
127
145
 
128
146
  def initialize_dup(other)
@@ -117,8 +117,21 @@ the current locale was +nil+.
117
117
  # Applies fallbacks plugin to attributes. Completely disables fallbacks
118
118
  # on model if option is +false+.
119
119
  included_hook do |_, backend_class|
120
- fallbacks = options[:fallbacks]
121
- backend_class.include(BackendReader.new(fallbacks, method(:generate_fallbacks))) unless fallbacks == false
120
+ unless options[:fallbacks] == false
121
+ backend_class.include(BackendInstanceMethods)
122
+
123
+ fallbacks =
124
+ if options[:fallbacks].is_a?(Hash)
125
+ generate_fallbacks(options[:fallbacks])
126
+ elsif options[:fallbacks] == true
127
+ generate_fallbacks({})
128
+ else
129
+ ::Hash.new { [] }
130
+ end
131
+
132
+ backend_class.singleton_class.attr_reader :fallbacks
133
+ backend_class.instance_variable_set(:@fallbacks, fallbacks)
134
+ end
122
135
  end
123
136
 
124
137
  private
@@ -134,36 +147,17 @@ the current locale was +nil+.
134
147
  end
135
148
  end
136
149
 
137
- class BackendReader < Module
138
- def initialize(fallbacks_option, fallbacks_generator)
139
- @fallbacks_generator = fallbacks_generator
140
- define_read(convert_option_to_fallbacks(fallbacks_option))
141
- end
142
-
143
- private
150
+ module BackendInstanceMethods
151
+ def read(locale, fallback: true, **kwargs)
152
+ return super(locale, **kwargs) if !fallback || kwargs[:locale]
144
153
 
145
- def define_read(fallbacks)
146
- define_method :read do |locale, fallback: true, **options|
147
- return super(locale, **options) if !fallback || options[:locale]
148
-
149
- locales = fallback == true ? fallbacks[locale] : [locale, *fallback]
150
- locales.each do |fallback_locale|
151
- value = super(fallback_locale, **options)
152
- return value if Util.present?(value)
153
- end
154
-
155
- super(locale, **options)
154
+ locales = fallback == true ? self.class.fallbacks[locale] : [locale, *fallback]
155
+ locales.each do |fallback_locale|
156
+ value = super(fallback_locale, **kwargs)
157
+ return value if Util.present?(value)
156
158
  end
157
- end
158
159
 
159
- def convert_option_to_fallbacks(option)
160
- if option.is_a?(::Hash)
161
- @fallbacks_generator[option]
162
- elsif option == true
163
- @fallbacks_generator[{}]
164
- else
165
- ::Hash.new { [] }
166
- end
160
+ super(locale, **kwargs)
167
161
  end
168
162
  end
169
163
  end
@@ -14,114 +14,47 @@ Plugin to use an original column for a given locale, and otherwise use the backe
14
14
  requires :column_fallback, include: false
15
15
 
16
16
  included_hook do |_, backend_class|
17
- case (column_fallback = options[:column_fallback])
17
+ backend_class.include BackendInstanceMethods
18
+ backend_class.extend BackendClassMethods
19
+ end
20
+
21
+ def self.use_column_fallback?(options, locale)
22
+ case column_fallback = options[:column_fallback]
18
23
  when TrueClass
19
- backend_class.include I18nDefaultLocaleBackend
20
- when Array, Proc
21
- backend_class.include BackendModule.new(column_fallback)
24
+ locale == I18n.default_locale
25
+ when Array
26
+ column_fallback.include?(locale)
27
+ when Proc
28
+ column_fallback.call(locale)
29
+ else
30
+ false
22
31
  end
23
32
  end
24
33
 
25
- module I18nDefaultLocaleBackend
34
+ module BackendInstanceMethods
26
35
  def read(locale, **)
27
- locale == I18n.default_locale ? model[attribute.to_sym] : super
36
+ if ColumnFallback.use_column_fallback?(options, locale)
37
+ model[attribute.to_sym]
38
+ else
39
+ super
40
+ end
28
41
  end
29
42
 
30
43
  def write(locale, value, **)
31
- if locale == I18n.default_locale
44
+ if ColumnFallback.use_column_fallback?(options, locale)
32
45
  model[attribute.to_sym] = value
33
46
  else
34
47
  super
35
48
  end
36
49
  end
37
-
38
- def self.included(base)
39
- base.extend(ClassMethods)
40
- end
41
-
42
- module ClassMethods
43
- def build_op(attr, locale)
44
- if locale == I18n.default_locale
45
- ::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name, attr.to_sym)
46
- else
47
- super
48
- end
49
- end
50
- end
51
50
  end
52
51
 
53
- class BackendModule < Module
54
- def initialize(column_fallback)
55
- case (@column_fallback = column_fallback)
56
- when Array
57
- define_array_accessors
58
- when Proc
59
- define_proc_accessors
60
- end
61
- end
62
-
63
- def included(base)
64
- base.extend(ClassMethods.new(@column_fallback))
65
- end
66
-
67
- private
68
-
69
- def define_array_accessors
70
- column_fallback = @column_fallback
71
-
72
- module_eval <<-EOM, __FILE__, __LINE__ + 1
73
- def read(locale, **)
74
- #{column_fallback}.include?(locale) ? model[attribute.to_sym] : super
75
- end
76
-
77
- def write(locale, value, **)
78
- if #{column_fallback}.include?(locale)
79
- model[attribute.to_sym] = value
80
- else
81
- super
82
- end
83
- end
84
- EOM
85
- end
86
-
87
- def define_proc_accessors
88
- column_fallback = @column_fallback
89
-
90
- define_method :read do |locale, **options|
91
- column_fallback.call(locale) ? model[attribute.to_sym] : super(locale, **options)
92
- end
93
-
94
- define_method :write do |locale, value, **options|
95
- if column_fallback.call(locale)
96
- model[attribute.to_sym] = value
97
- else
98
- super(locale, value, **options)
99
- end
100
- end
101
- end
102
-
103
- class ClassMethods < Module
104
- def initialize(column_fallback)
105
- case column_fallback
106
- when Array
107
- module_eval <<-EOM, __FILE__, __LINE__ + 1
108
- def build_op(attr, locale)
109
- if #{column_fallback}.include?(locale)
110
- ::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name, attr.to_sym)
111
- else
112
- super
113
- end
114
- end
115
- EOM
116
- when Proc
117
- define_method(:build_op) do |attr, locale|
118
- if column_fallback.call(locale)
119
- ::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name, attr.to_sym)
120
- else
121
- super(attr, locale)
122
- end
123
- end
124
- end
52
+ module BackendClassMethods
53
+ def build_op(attr, locale)
54
+ if ColumnFallback.use_column_fallback?(options, locale)
55
+ ::Sequel::SQL::QualifiedIdentifier.new(model_class.table_name, attr.to_sym)
56
+ else
57
+ super
125
58
  end
126
59
  end
127
60
  end
@@ -8,7 +8,7 @@ module Mobility
8
8
  module VERSION
9
9
  MAJOR = 1
10
10
  MINOR = 2
11
- TINY = 0
11
+ TINY = 4
12
12
  PRE = nil
13
13
 
14
14
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
@@ -34,7 +34,7 @@ cert_chain:
34
34
  eBMcZq0d1tbtv1M1UXND9mOfhLZ31YvoSTPkrJiRpljUNgD0+ugelnr1/5X/9k8y
35
35
  J9QOd3C5jpSShf/HMvpJnFuSYFm19cH9GrHjvw==
36
36
  -----END CERTIFICATE-----
37
- date: 2021-09-26 00:00:00.000000000 Z
37
+ date: 2021-11-28 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: request_store
metadata.gz.sig CHANGED
Binary file