mobility_typed 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +69 -0
- data/LICENSE.txt +21 -0
- data/README.md +119 -0
- data/Rakefile +10 -0
- data/lib/mobility_typed/version.rb +9 -0
- data/lib/mobility_typed/writer.rb +69 -0
- data/lib/mobility_typed.rb +5 -0
- data/mobility_typed.gemspec +38 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d488719d610215bf72bd49b82b0a817780278e3dffdd9d400b308774a7b67688
|
4
|
+
data.tar.gz: '068e0a7f91c31411978574d9c5acd61f724384789ad013dfc2bef0d97a9ff413'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a0bb4e7f952c28d6d113968627584a7ef9f8512243a4e8fcc5771ee09ba650a62bba1c41a2efc3ca9fe1ac500766e16e79690d65e293c1ed3df1f952c7da662
|
7
|
+
data.tar.gz: 626e7d6abd6d5d613583010c16ea5adcde6e5bdbc340ce0a37ad455f4e2d9c7f5505ada90db817e07fb03574d4ed292334d42af28c30c10b08ad9459b9a3c0ad
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mobility_typed (0.1)
|
5
|
+
mobility (~> 1.1.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.2)
|
11
|
+
concurrent-ruby (1.2.2)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
i18n (1.13.0)
|
14
|
+
concurrent-ruby (~> 1.0)
|
15
|
+
mobility (1.1.3)
|
16
|
+
i18n (>= 0.6.10, < 2)
|
17
|
+
request_store (~> 1.0)
|
18
|
+
parallel (1.23.0)
|
19
|
+
parser (3.2.2.1)
|
20
|
+
ast (~> 2.4.1)
|
21
|
+
rack (3.0.7)
|
22
|
+
rainbow (3.1.1)
|
23
|
+
rake (10.5.0)
|
24
|
+
regexp_parser (2.8.0)
|
25
|
+
request_store (1.5.1)
|
26
|
+
rack (>= 1.4)
|
27
|
+
rexml (3.2.5)
|
28
|
+
rspec (3.12.0)
|
29
|
+
rspec-core (~> 3.12.0)
|
30
|
+
rspec-expectations (~> 3.12.0)
|
31
|
+
rspec-mocks (~> 3.12.0)
|
32
|
+
rspec-core (3.12.2)
|
33
|
+
rspec-support (~> 3.12.0)
|
34
|
+
rspec-expectations (3.12.3)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.12.0)
|
37
|
+
rspec-mocks (3.12.5)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.12.0)
|
40
|
+
rspec-support (3.12.0)
|
41
|
+
rubocop (0.89.1)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 2.7.1.1)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
regexp_parser (>= 1.7)
|
46
|
+
rexml
|
47
|
+
rubocop-ast (>= 0.3.0, < 1.0)
|
48
|
+
ruby-progressbar (~> 1.7)
|
49
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
50
|
+
rubocop-ast (0.8.0)
|
51
|
+
parser (>= 2.7.1.5)
|
52
|
+
rubocop-rspec (1.40.0)
|
53
|
+
rubocop (>= 0.68.1)
|
54
|
+
ruby-progressbar (1.13.0)
|
55
|
+
unicode-display_width (1.8.0)
|
56
|
+
|
57
|
+
PLATFORMS
|
58
|
+
ruby
|
59
|
+
|
60
|
+
DEPENDENCIES
|
61
|
+
bundler (~> 1.17)
|
62
|
+
mobility_typed!
|
63
|
+
rake (~> 10.0)
|
64
|
+
rspec (~> 3.0)
|
65
|
+
rubocop (~> 0.89.0)
|
66
|
+
rubocop-rspec (~> 1.40.0)
|
67
|
+
|
68
|
+
BUNDLED WITH
|
69
|
+
1.17.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 GeorgeGorbanev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# Mobility. Typed.
|
2
|
+
|
3
|
+
This gem contains plugin for [Mobility](https://github.com/shioyama/mobility) translation framework.
|
4
|
+
It provides type checking for attribute writers.
|
5
|
+
|
6
|
+
## Motivation
|
7
|
+
|
8
|
+
Since Mobility supports different storage backends some of them will not
|
9
|
+
guarantee type safety of input data. For example Postgres
|
10
|
+
([`Mobility::Backend::Jsonb`](http://www.rubydoc.info/gems/mobility/Mobility/Backends/Jsonb)
|
11
|
+
let us to save any kind of json-based data in single column.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Offer < ApplicationRecord
|
15
|
+
# ...
|
16
|
+
|
17
|
+
translates :benefits
|
18
|
+
end
|
19
|
+
|
20
|
+
Offer.create!(benefits: ['fast', 'safe'])
|
21
|
+
Offer.create!(benefits: 'cheap')
|
22
|
+
|
23
|
+
Offer.find_each do |offer|
|
24
|
+
offer.benefits.each { |benefit| Customer.notify_by_benefit(benefit) }
|
25
|
+
# => NoMethodError (undefined method `each' for "cheap":String)
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
To prevent errors like this or at least detect them earlier we can add
|
30
|
+
`:type` option to attribute writer configuration:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class Offer < ApplicationRecord
|
34
|
+
# ...
|
35
|
+
|
36
|
+
translates :benefits, writer: { type: :array }
|
37
|
+
end
|
38
|
+
|
39
|
+
Offer.create!(benefits: ['fast', 'safe'])
|
40
|
+
Offer.create!(benefits: 'cheap')
|
41
|
+
# => MobilityTyped::Writer::TypeError ("benefits= called with string, array expected")
|
42
|
+
# now error exposed before database records was corrupted
|
43
|
+
# so we can fix it earlier and code below will executes safely
|
44
|
+
Offer.find_each do |offer|
|
45
|
+
offer.benefits.each { |benefit| Customer.notify_by_benefit(benefit) }
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
## Installation
|
50
|
+
|
51
|
+
Add this line to your application's Gemfile:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
gem 'mobility_typed'
|
55
|
+
```
|
56
|
+
|
57
|
+
And then execute:
|
58
|
+
|
59
|
+
```sh
|
60
|
+
$ bundle install
|
61
|
+
```
|
62
|
+
|
63
|
+
Then add plugin to your mobility initializer:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# your_rails_app/config/initializers/mobility.rb
|
67
|
+
Mobility.configure do
|
68
|
+
# ...
|
69
|
+
plugins do
|
70
|
+
# ...
|
71
|
+
typed_writer
|
72
|
+
# ...
|
73
|
+
end
|
74
|
+
# ...
|
75
|
+
end
|
76
|
+
```
|
77
|
+
And remove `writer` plugin from your model if you have it:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
Mobility.configure do
|
81
|
+
# ...
|
82
|
+
plugins do
|
83
|
+
# ...
|
84
|
+
writer # <---- remove this line
|
85
|
+
# ...
|
86
|
+
end
|
87
|
+
# ...
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
## Usage
|
92
|
+
|
93
|
+
Just add `type` option to attribute writer configuration:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class YourModel < ApplicationRecord
|
97
|
+
translates :your_attribute, writer: { type: :integer }
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
List of available writers types:
|
102
|
+
1) `:string`
|
103
|
+
2) `:integer`
|
104
|
+
3) `:float`
|
105
|
+
4) `:bool`
|
106
|
+
5) `:array`
|
107
|
+
6) `:hash`
|
108
|
+
|
109
|
+
## Default Mobility::Plugins::Writer compatibility
|
110
|
+
|
111
|
+
To use `typed_writer` plugin you should remove `writer` plugin from your initializer.
|
112
|
+
But `typed_writer` plugin is backward compatible with `writer` plugin. You can translate attributes
|
113
|
+
without passing `type` option to writer configuration. In this case `typed_writer` will work just as `writer` plugin.
|
114
|
+
|
115
|
+
|
116
|
+
## TODO:
|
117
|
+
|
118
|
+
1) Type checking for nested attributes, to check `array` and `hash` content
|
119
|
+
2) RBS generator
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
require 'mobility'
|
4
|
+
|
5
|
+
module MobilityTyped
|
6
|
+
module Writer
|
7
|
+
TYPES_MAP = {
|
8
|
+
String => :string,
|
9
|
+
Integer => :integer,
|
10
|
+
Float => :float,
|
11
|
+
TrueClass => :bool,
|
12
|
+
FalseClass => :bool,
|
13
|
+
Array => :array,
|
14
|
+
Hash => :hash
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
class TypeError < StandardError; end
|
18
|
+
|
19
|
+
extend Mobility::Plugin
|
20
|
+
|
21
|
+
default true
|
22
|
+
requires :backend
|
23
|
+
|
24
|
+
initialize_hook do |*names|
|
25
|
+
if options[:typed_writer]
|
26
|
+
type_option = options[:typed_writer][:type] if options[:typed_writer].is_a?(Hash)
|
27
|
+
|
28
|
+
names.each do |name|
|
29
|
+
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
30
|
+
def #{name}=(value, locale: nil, **options)
|
31
|
+
#{MobilityTyped::Writer.check_type(name, type_option) if type_option}
|
32
|
+
#{MobilityTyped::Writer.setup_source}
|
33
|
+
mobility_backends[:#{name}].write(locale, value, **options)
|
34
|
+
end
|
35
|
+
EOM
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.setup_source
|
41
|
+
<<-EOL
|
42
|
+
return super(value) if options[:super]
|
43
|
+
if (locale &&= locale.to_sym)
|
44
|
+
#{'Mobility.enforce_available_locales!(locale)' if I18n.enforce_available_locales}
|
45
|
+
options[:locale] = true
|
46
|
+
else
|
47
|
+
locale = Mobility.locale
|
48
|
+
end
|
49
|
+
EOL
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.check_type(name, type)
|
53
|
+
<<-EOL
|
54
|
+
if !value.nil?
|
55
|
+
value_type = MobilityTyped::Writer.value_type(value)
|
56
|
+
if value_type != :#{type}
|
57
|
+
raise MobilityTyped::Writer::TypeError, "#{name}= called with \#{value_type}, #{type} expected"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
EOL
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.value_type(value)
|
64
|
+
TYPES_MAP[value.class] || value.class
|
65
|
+
end
|
66
|
+
|
67
|
+
Mobility::Plugins.register_plugin(:typed_writer, MobilityTyped::Writer)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'mobility_typed/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'mobility_typed'
|
9
|
+
spec.version = MobilityTyped.gem_version
|
10
|
+
spec.authors = ['George Gorbanev']
|
11
|
+
spec.email = ['georgegorbanev@gmail.com']
|
12
|
+
|
13
|
+
spec.required_ruby_version = '>= 2.5'
|
14
|
+
|
15
|
+
spec.summary = 'Ruby Mobility gem plugin designed to add type checking for translated attributes'
|
16
|
+
spec.description = <<~DESCRIPTION
|
17
|
+
This gem adds Mobility plugin, which can be turned on for Rails model,
|
18
|
+
and will check types of values passed to setters.
|
19
|
+
DESCRIPTION
|
20
|
+
|
21
|
+
spec.homepage = 'https://github.com/georgegorbanev/mobility_typed'
|
22
|
+
spec.license = 'MIT'
|
23
|
+
|
24
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
25
|
+
spec.metadata['source_code_uri'] = 'https://github.com/georgegorbanev/mobility_typed'
|
26
|
+
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.files = Dir['{lib/**/*,[A-Z]*}']
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.add_dependency 'mobility', '~> 1.1.3'
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
spec.add_development_dependency 'rubocop', '~> 0.89.0'
|
37
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.40.0'
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mobility_typed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- George Gorbanev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mobility
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.3
|
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.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.89.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.89.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.40.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.40.0
|
97
|
+
description: |
|
98
|
+
This gem adds Mobility plugin, which can be turned on for Rails model,
|
99
|
+
and will check types of values passed to setters.
|
100
|
+
email:
|
101
|
+
- georgegorbanev@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- lib/mobility_typed.rb
|
112
|
+
- lib/mobility_typed/version.rb
|
113
|
+
- lib/mobility_typed/writer.rb
|
114
|
+
- mobility_typed.gemspec
|
115
|
+
homepage: https://github.com/georgegorbanev/mobility_typed
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
homepage_uri: https://github.com/georgegorbanev/mobility_typed
|
120
|
+
source_code_uri: https://github.com/georgegorbanev/mobility_typed
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '2.5'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubygems_version: 3.1.4
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Ruby Mobility gem plugin designed to add type checking for translated attributes
|
140
|
+
test_files: []
|