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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +26 -0
  5. data/Gemfile +5 -2
  6. data/Gemfile.lock +79 -8
  7. data/README.md +183 -91
  8. data/lib/mobility.rb +40 -166
  9. data/lib/mobility/arel/nodes/pg_ops.rb +1 -1
  10. data/lib/mobility/backend.rb +19 -41
  11. data/lib/mobility/backends.rb +20 -0
  12. data/lib/mobility/backends/active_record.rb +4 -0
  13. data/lib/mobility/backends/active_record/column.rb +2 -0
  14. data/lib/mobility/backends/active_record/container.rb +4 -2
  15. data/lib/mobility/backends/active_record/hstore.rb +2 -0
  16. data/lib/mobility/backends/active_record/json.rb +2 -0
  17. data/lib/mobility/backends/active_record/jsonb.rb +2 -0
  18. data/lib/mobility/backends/active_record/key_value.rb +5 -3
  19. data/lib/mobility/backends/active_record/pg_hash.rb +1 -1
  20. data/lib/mobility/backends/active_record/serialized.rb +2 -0
  21. data/lib/mobility/backends/active_record/table.rb +5 -3
  22. data/lib/mobility/backends/column.rb +0 -6
  23. data/lib/mobility/backends/container.rb +2 -1
  24. data/lib/mobility/backends/hash.rb +39 -0
  25. data/lib/mobility/backends/hstore.rb +0 -1
  26. data/lib/mobility/backends/json.rb +0 -1
  27. data/lib/mobility/backends/jsonb.rb +0 -1
  28. data/lib/mobility/backends/key_value.rb +22 -14
  29. data/lib/mobility/backends/null.rb +2 -0
  30. data/lib/mobility/backends/sequel.rb +3 -0
  31. data/lib/mobility/backends/sequel/column.rb +2 -0
  32. data/lib/mobility/backends/sequel/container.rb +3 -1
  33. data/lib/mobility/backends/sequel/hstore.rb +2 -0
  34. data/lib/mobility/backends/sequel/json.rb +2 -0
  35. data/lib/mobility/backends/sequel/jsonb.rb +3 -1
  36. data/lib/mobility/backends/sequel/key_value.rb +8 -6
  37. data/lib/mobility/backends/sequel/serialized.rb +2 -0
  38. data/lib/mobility/backends/sequel/table.rb +5 -2
  39. data/lib/mobility/backends/serialized.rb +1 -3
  40. data/lib/mobility/backends/table.rb +14 -6
  41. data/lib/mobility/pluggable.rb +36 -0
  42. data/lib/mobility/plugin.rb +260 -0
  43. data/lib/mobility/plugins.rb +26 -25
  44. data/lib/mobility/plugins/active_model.rb +17 -0
  45. data/lib/mobility/plugins/active_model/cache.rb +26 -0
  46. data/lib/mobility/plugins/active_model/dirty.rb +112 -77
  47. data/lib/mobility/plugins/active_record.rb +34 -0
  48. data/lib/mobility/plugins/active_record/backend.rb +25 -0
  49. data/lib/mobility/plugins/active_record/cache.rb +28 -0
  50. data/lib/mobility/plugins/active_record/dirty.rb +34 -17
  51. data/lib/mobility/plugins/active_record/query.rb +43 -31
  52. data/lib/mobility/plugins/active_record/uniqueness_validation.rb +60 -0
  53. data/lib/mobility/plugins/attribute_methods.rb +28 -20
  54. data/lib/mobility/plugins/attributes.rb +70 -0
  55. data/lib/mobility/plugins/backend.rb +138 -0
  56. data/lib/mobility/plugins/backend_reader.rb +34 -0
  57. data/lib/mobility/plugins/cache.rb +59 -24
  58. data/lib/mobility/plugins/default.rb +22 -17
  59. data/lib/mobility/plugins/dirty.rb +12 -33
  60. data/lib/mobility/plugins/fallbacks.rb +51 -43
  61. data/lib/mobility/plugins/fallthrough_accessors.rb +20 -23
  62. data/lib/mobility/plugins/locale_accessors.rb +25 -35
  63. data/lib/mobility/plugins/presence.rb +28 -21
  64. data/lib/mobility/plugins/query.rb +8 -17
  65. data/lib/mobility/plugins/reader.rb +50 -0
  66. data/lib/mobility/plugins/sequel.rb +34 -0
  67. data/lib/mobility/plugins/sequel/backend.rb +25 -0
  68. data/lib/mobility/plugins/sequel/cache.rb +24 -0
  69. data/lib/mobility/plugins/sequel/dirty.rb +32 -21
  70. data/lib/mobility/plugins/sequel/query.rb +21 -6
  71. data/lib/mobility/plugins/writer.rb +44 -0
  72. data/lib/mobility/translations.rb +95 -0
  73. data/lib/mobility/version.rb +12 -1
  74. data/lib/rails/generators/mobility/templates/initializer.rb +95 -77
  75. metadata +28 -27
  76. metadata.gz.sig +0 -0
  77. data/lib/mobility/active_model.rb +0 -4
  78. data/lib/mobility/active_model/backend_resetter.rb +0 -26
  79. data/lib/mobility/active_record.rb +0 -23
  80. data/lib/mobility/active_record/backend_resetter.rb +0 -26
  81. data/lib/mobility/active_record/uniqueness_validator.rb +0 -60
  82. data/lib/mobility/attributes.rb +0 -324
  83. data/lib/mobility/backend/orm_delegator.rb +0 -44
  84. data/lib/mobility/backend_resetter.rb +0 -50
  85. data/lib/mobility/configuration.rb +0 -138
  86. data/lib/mobility/fallbacks.rb +0 -28
  87. data/lib/mobility/interface.rb +0 -0
  88. data/lib/mobility/loaded.rb +0 -4
  89. data/lib/mobility/plugins/active_record/attribute_methods.rb +0 -38
  90. data/lib/mobility/plugins/cache/translation_cacher.rb +0 -40
  91. data/lib/mobility/sequel.rb +0 -9
  92. data/lib/mobility/sequel/backend_resetter.rb +0 -23
  93. data/lib/mobility/translates.rb +0 -73
@@ -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
File without changes
@@ -1,4 +0,0 @@
1
- module Mobility
2
- module Loaded
3
- end
4
- end
@@ -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
@@ -1,9 +0,0 @@
1
- module Mobility
2
- =begin
3
-
4
- Module loading Sequel-specific classes for Mobility models.
5
-
6
- =end
7
- module Sequel
8
- end
9
- end
@@ -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
@@ -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