field_masked_model 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +21 -0
- data/README.md +111 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/main.rb +25 -0
- data/examples/protos/lib/users_pb.rb +24 -0
- data/examples/protos/users.proto +16 -0
- data/field_masked_model.gemspec +32 -0
- data/lib/field_masked_model.rb +6 -0
- data/lib/field_masked_model/attribute_converter.rb +30 -0
- data/lib/field_masked_model/base.rb +240 -0
- data/lib/field_masked_model/error.rb +4 -0
- data/lib/field_masked_model/model_pool.rb +25 -0
- data/lib/field_masked_model/version.rb +3 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 979adcce46c3f04897e9803e28edb1d99cd13f39c8a56379265efe21b0d31644
|
4
|
+
data.tar.gz: 15d382af15602a920594796633d03405cc371d0919e4f7b09c2201c6677fb5ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9d79eb5b69069e03773ffd32eaf056a41f1357487c9605524095e1afd2666076487a1e8b7fc297c9e8a7aa02134c301259964a35764a2f0eb2ed3f9471d826c3
|
7
|
+
data.tar.gz: 19ad31fba8d2f4c3cec3da298c496071d12b2f3f70ff5d59f2447a4eb934bc559c57e46d875f300e36cb132d390e9a7e73c0c09eb14e1773b11dca1b266a36cd
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
field_masked_model (0.2.1)
|
5
|
+
fmparser (= 0.1.0)
|
6
|
+
google-protobuf (~> 3.7)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
coderay (1.1.2)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
fmparser (0.1.0)
|
14
|
+
google-protobuf (3.8.0-universal-darwin)
|
15
|
+
method_source (0.9.2)
|
16
|
+
pry (0.12.2)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.9.0)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.8.0)
|
21
|
+
rspec-core (~> 3.8.0)
|
22
|
+
rspec-expectations (~> 3.8.0)
|
23
|
+
rspec-mocks (~> 3.8.0)
|
24
|
+
rspec-core (3.8.1)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-expectations (3.8.4)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-mocks (3.8.1)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.8.0)
|
32
|
+
rspec-support (3.8.2)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
bundler (~> 1.17)
|
39
|
+
field_masked_model!
|
40
|
+
pry (~> 0.11)
|
41
|
+
rake (~> 10.0)
|
42
|
+
rspec (~> 3.8)
|
43
|
+
|
44
|
+
BUNDLED WITH
|
45
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Nao Minami
|
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,111 @@
|
|
1
|
+
# FieldMaskedModel
|
2
|
+
|
3
|
+
FieldMaskedModel provides masked accessor methods to model classes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'field_masked_model'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install field_masked_model
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
For example, you have .proto file shown below.
|
24
|
+
|
25
|
+
```proto
|
26
|
+
// proto
|
27
|
+
syntax = "proto3";
|
28
|
+
package organization.service;
|
29
|
+
|
30
|
+
option ruby_package = "UsersPb";
|
31
|
+
|
32
|
+
message User {
|
33
|
+
int64 id = 1;
|
34
|
+
string name = 2;
|
35
|
+
int32 age = 3;
|
36
|
+
Profile profile = 4;
|
37
|
+
}
|
38
|
+
|
39
|
+
message Profile {
|
40
|
+
int64 id = 1;
|
41
|
+
string introduction = 2;
|
42
|
+
}
|
43
|
+
```
|
44
|
+
|
45
|
+
By using `FieldMaskedModel::Base`, you can get model classes with masked accessor methods.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# user.rb
|
49
|
+
class User < FieldMaskedModel::Base
|
50
|
+
msgclass UsersPb::User
|
51
|
+
end
|
52
|
+
|
53
|
+
# profile.rb
|
54
|
+
class Profile < FieldMaskedModel::Base
|
55
|
+
msgclass UsersPb::Profile
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
$ u = User.new(
|
61
|
+
* field_mask: Google::Protobuf::FieldMask.new(
|
62
|
+
* paths: [
|
63
|
+
* "id",
|
64
|
+
* "name",
|
65
|
+
* "profile.id",
|
66
|
+
* "profile.introduction"
|
67
|
+
* ]
|
68
|
+
* ),
|
69
|
+
* message: UsersPb::User.new(
|
70
|
+
* id: 3,
|
71
|
+
* name: "Taro",
|
72
|
+
* profile: UsersPb::Profile.new(
|
73
|
+
* id: 4,
|
74
|
+
* introduction: "My name is Taro"
|
75
|
+
* )
|
76
|
+
* ),
|
77
|
+
* )
|
78
|
+
=> <User
|
79
|
+
id: 3,
|
80
|
+
name: "Taro",
|
81
|
+
age: -,
|
82
|
+
profile: Profile>
|
83
|
+
|
84
|
+
$ u.id
|
85
|
+
=> 3
|
86
|
+
|
87
|
+
$ u.name
|
88
|
+
=> "Taro"
|
89
|
+
|
90
|
+
$ u.age
|
91
|
+
=> FieldMaskedModel::InaccessibleError: age is not specified as paths in field_mask!
|
92
|
+
|
93
|
+
$ u.profile
|
94
|
+
=> <Profile
|
95
|
+
id: 4,
|
96
|
+
introduction: "My name is Taro">
|
97
|
+
```
|
98
|
+
|
99
|
+
## Development
|
100
|
+
|
101
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake true` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
102
|
+
|
103
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/south37/field_masked_model.
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "field_masked_model"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/main.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "field_masked_model"
|
2
|
+
require File.join(__dir__, "protos", "lib", "users_pb")
|
3
|
+
|
4
|
+
class User < FieldMaskedModel::Base
|
5
|
+
msgclass UsersPb::User
|
6
|
+
end
|
7
|
+
|
8
|
+
class Profile < FieldMaskedModel::Base
|
9
|
+
msgclass UsersPb::Profile
|
10
|
+
end
|
11
|
+
|
12
|
+
u = User.new(
|
13
|
+
field_mask: Google::Protobuf::FieldMask.new(paths: ["id", "name", "profile.id", "profile.introduction"]),
|
14
|
+
message: UsersPb::User.new(id: 3, name: "Taro", profile: UsersPb::Profile.new(id: 4, introduction: "My name is Taro")),
|
15
|
+
)
|
16
|
+
|
17
|
+
p u
|
18
|
+
p u.id
|
19
|
+
p u.name
|
20
|
+
begin
|
21
|
+
u.age
|
22
|
+
rescue => e
|
23
|
+
p e
|
24
|
+
end
|
25
|
+
p u.profile
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# source: users.proto
|
3
|
+
|
4
|
+
require 'google/protobuf'
|
5
|
+
|
6
|
+
Google::Protobuf::DescriptorPool.generated_pool.build do
|
7
|
+
add_file("users.proto", :syntax => :proto3) do
|
8
|
+
add_message "organization.service.User" do
|
9
|
+
optional :id, :int64, 1
|
10
|
+
optional :name, :string, 2
|
11
|
+
optional :age, :int32, 3
|
12
|
+
optional :profile, :message, 4, "organization.service.Profile"
|
13
|
+
end
|
14
|
+
add_message "organization.service.Profile" do
|
15
|
+
optional :id, :int64, 1
|
16
|
+
optional :introduction, :string, 2
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module UsersPb
|
22
|
+
User = Google::Protobuf::DescriptorPool.generated_pool.lookup("organization.service.User").msgclass
|
23
|
+
Profile = Google::Protobuf::DescriptorPool.generated_pool.lookup("organization.service.Profile").msgclass
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
syntax = "proto3";
|
2
|
+
package organization.service;
|
3
|
+
|
4
|
+
option ruby_package = "UsersPb";
|
5
|
+
|
6
|
+
message User {
|
7
|
+
int64 id = 1;
|
8
|
+
string name = 2;
|
9
|
+
int32 age = 3;
|
10
|
+
Profile profile = 4;
|
11
|
+
}
|
12
|
+
|
13
|
+
message Profile {
|
14
|
+
int64 id = 1;
|
15
|
+
string introduction = 2;
|
16
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "field_masked_model/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "field_masked_model"
|
8
|
+
spec.version = FieldMaskedModel::VERSION
|
9
|
+
spec.authors = ["Nao Minami"]
|
10
|
+
spec.email = ["south37777@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{FieldMaskedModel provides masked accessor methods to models}
|
13
|
+
spec.description = %q{FieldMaskedModel provides masked accessor methods to models}
|
14
|
+
spec.homepage = "https://github.com/south37/field_masked_model"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.8"
|
30
|
+
spec.add_runtime_dependency "fmparser", "0.1.0"
|
31
|
+
spec.add_runtime_dependency "google-protobuf", "~> 3.7"
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "google/protobuf/timestamp_pb"
|
2
|
+
|
3
|
+
module FieldMaskedModel
|
4
|
+
module AttributeConverter
|
5
|
+
class << self
|
6
|
+
def convert(value)
|
7
|
+
case value
|
8
|
+
when Google::Protobuf::Timestamp
|
9
|
+
timestamp_to_time(value)
|
10
|
+
else
|
11
|
+
# TODO(south37) Add conversion logic of other classes
|
12
|
+
value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param [Google::Protobuf::Timestamp] timestamp
|
17
|
+
# @return [Time, ActiveSupport::TimeWithZone]
|
18
|
+
def timestamp_to_time(timestamp)
|
19
|
+
v = timestamp.nanos * (10 ** -9) + timestamp.seconds
|
20
|
+
|
21
|
+
if Time.respond_to?(:zone) && Time.zone.respond_to?(:at)
|
22
|
+
# Use ActiveSupport::TimeWithZone when it is available.
|
23
|
+
Time.zone.at(v)
|
24
|
+
else
|
25
|
+
Time.at(v)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,240 @@
|
|
1
|
+
require "fmparser"
|
2
|
+
require "google/protobuf"
|
3
|
+
require "google/protobuf/field_mask_pb"
|
4
|
+
|
5
|
+
require "field_masked_model/attribute_converter"
|
6
|
+
require "field_masked_model/model_pool"
|
7
|
+
|
8
|
+
module FieldMaskedModel
|
9
|
+
class Base
|
10
|
+
class << self
|
11
|
+
attr_reader :msg, :model_pool, :inaccessible_error_callback
|
12
|
+
|
13
|
+
# @param [Class] klass A class represents the protobuf message class
|
14
|
+
# @param [<Symbol, String>] exclude_fields
|
15
|
+
# @param [FieldMaskedModel::ModelPool] model_pool
|
16
|
+
def msgclass(klass, exclude_fields: [], model_pool: ModelPool.generated_pool)
|
17
|
+
if defined?(@msg)
|
18
|
+
raise "msgclass is already registered!"
|
19
|
+
end
|
20
|
+
|
21
|
+
@model_pool = model_pool
|
22
|
+
@model_pool.add(klass, self)
|
23
|
+
|
24
|
+
exclude_fields.each do |field|
|
25
|
+
exclude_field field
|
26
|
+
end
|
27
|
+
|
28
|
+
@msg = klass
|
29
|
+
define_accessors!
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param [Symbol, String] name
|
33
|
+
def exclude_field(name)
|
34
|
+
if defined?(@msg)
|
35
|
+
Kernel.warn("exclude_field must be called before msgclass", uplevel: 1)
|
36
|
+
end
|
37
|
+
_excluded_fields << name.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param [Proc] callback
|
41
|
+
def set_inaccessible_error_callback(callback)
|
42
|
+
@inaccessible_error_callback = callback
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [<Symbol, { Symbol => Array }>]
|
46
|
+
def fields
|
47
|
+
r = []
|
48
|
+
children = {}
|
49
|
+
self.entries.each do |entry|
|
50
|
+
n = entry.name.to_sym
|
51
|
+
type = self.dispatch(entry)
|
52
|
+
case type
|
53
|
+
when :attribute
|
54
|
+
r << n
|
55
|
+
when :association
|
56
|
+
modelclass = @model_pool.lookup(entry.subtype.msgclass)
|
57
|
+
children[n] = modelclass.fields
|
58
|
+
when :repeated_association
|
59
|
+
modelclass = @model_pool.lookup(entry.subtype.msgclass)
|
60
|
+
children[n] = modelclass.fields
|
61
|
+
end
|
62
|
+
end
|
63
|
+
r << children if children.size > 0
|
64
|
+
r
|
65
|
+
end
|
66
|
+
|
67
|
+
# @return [<Google::Protobuf::FieldDescriptor>]
|
68
|
+
def entries
|
69
|
+
@msg.descriptor.entries.select do |e|
|
70
|
+
!_excluded_fields.include?(e.name)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# @param [Google::Protobuf::FieldDescriptor] entry
|
75
|
+
# @return [Symbol]
|
76
|
+
def dispatch(entry)
|
77
|
+
case entry.type
|
78
|
+
when :message
|
79
|
+
m = @model_pool.lookup(entry.subtype.msgclass)
|
80
|
+
# NOTE: If msgclass is not registered in ModelPool, we treat it
|
81
|
+
# as a one of attribute classes.
|
82
|
+
if m.nil?
|
83
|
+
return :attribute
|
84
|
+
end
|
85
|
+
|
86
|
+
case entry.label
|
87
|
+
when :repeated
|
88
|
+
:repeated_association
|
89
|
+
else
|
90
|
+
:association
|
91
|
+
end
|
92
|
+
else # eum or scalar
|
93
|
+
:attribute
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def define_accessors!
|
100
|
+
self.entries.each do |entry|
|
101
|
+
n = entry.name.to_sym
|
102
|
+
define_accessor!(name: n, entry: entry)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def define_accessor!(name:, entry:)
|
107
|
+
define_method(name) do
|
108
|
+
validate!(name)
|
109
|
+
|
110
|
+
ivar = :"@_#{name}"
|
111
|
+
if instance_variable_defined?(ivar)
|
112
|
+
next instance_variable_get(ivar)
|
113
|
+
end
|
114
|
+
|
115
|
+
v = @message.send(name)
|
116
|
+
type = self.class.dispatch(entry)
|
117
|
+
r =
|
118
|
+
case type
|
119
|
+
when :attribute
|
120
|
+
AttributeConverter.convert(v)
|
121
|
+
when :association
|
122
|
+
if v.nil?
|
123
|
+
nil
|
124
|
+
else
|
125
|
+
modelclass = self.class.model_pool.lookup(entry.subtype.msgclass)
|
126
|
+
modelclass.new(field_mask_node: @fm_node.get_child(name), message: v)
|
127
|
+
end
|
128
|
+
when :repeated_association
|
129
|
+
modelclass = self.class.model_pool.lookup(entry.subtype.msgclass)
|
130
|
+
v.map do |vv|
|
131
|
+
if vv.nil?
|
132
|
+
nil
|
133
|
+
else
|
134
|
+
modelclass.new(field_mask_node: @fm_node.get_child(name), message: vv)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
instance_variable_set(ivar, r)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def _excluded_fields
|
144
|
+
@_excluded_fields ||= Set.new
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# @param [Google::Protobuf::FieldMask, nil] field_mask_node
|
149
|
+
# @param [FMParser::MessageNode, nil] field_mask_node
|
150
|
+
# @param [Object] message represents the protobuf message object
|
151
|
+
def initialize(field_mask: nil, field_mask_node: nil, message:)
|
152
|
+
if field_mask.nil? && field_mask_node.nil?
|
153
|
+
raise ArgumentError.new("missing keyword: field_mask or field_mask_node")
|
154
|
+
end
|
155
|
+
@fm_node = field_mask_node || FMParser.parse(paths: field_mask.paths, root: self.class.msg)
|
156
|
+
@message = message
|
157
|
+
|
158
|
+
@accessible_fields ||= Set.new(@fm_node.field_names)
|
159
|
+
end
|
160
|
+
|
161
|
+
# @return [Hash]
|
162
|
+
def to_h
|
163
|
+
r = {}
|
164
|
+
self.class.entries.each do |entry|
|
165
|
+
n = entry.name.to_sym
|
166
|
+
next if !@accessible_fields.include?(n)
|
167
|
+
|
168
|
+
v = self.send(n)
|
169
|
+
type = self.class.dispatch(entry)
|
170
|
+
case type
|
171
|
+
when :attribute
|
172
|
+
r[n] = v
|
173
|
+
when :association
|
174
|
+
if v.nil?
|
175
|
+
r[n] = nil
|
176
|
+
else
|
177
|
+
r[n] = v.to_h
|
178
|
+
end
|
179
|
+
when :repeated_association
|
180
|
+
r[n] = v.map(&:to_h)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
r
|
184
|
+
end
|
185
|
+
|
186
|
+
# @return [String]
|
187
|
+
def inspect
|
188
|
+
h = {}
|
189
|
+
self.class.entries.each do |entry|
|
190
|
+
n = entry.name.to_sym
|
191
|
+
if !@accessible_fields.include?(n)
|
192
|
+
h[n] = "-"
|
193
|
+
next
|
194
|
+
end
|
195
|
+
|
196
|
+
v = self.send(n)
|
197
|
+
type = self.class.dispatch(entry)
|
198
|
+
case type
|
199
|
+
when :attribute
|
200
|
+
case v
|
201
|
+
when NilClass
|
202
|
+
h[n] = "nil"
|
203
|
+
when String
|
204
|
+
h[n] = "\"#{v}\""
|
205
|
+
else
|
206
|
+
h[n] = v
|
207
|
+
end
|
208
|
+
when :association
|
209
|
+
case v
|
210
|
+
when NilClass
|
211
|
+
h[n] = "nil"
|
212
|
+
else
|
213
|
+
h[n] = v.class.name.split("::").last
|
214
|
+
end
|
215
|
+
when :repeated_association
|
216
|
+
if v.size > 0
|
217
|
+
h[n] = "[#{v[0].class.name.split("::").last}]"
|
218
|
+
else
|
219
|
+
h[n] = "[]"
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
"<#{self.class.name}#{h.map { |k, v| "\n #{k}: #{v}" }.join(',')}>"
|
224
|
+
end
|
225
|
+
|
226
|
+
private
|
227
|
+
|
228
|
+
# @param [Symbol] field
|
229
|
+
# @raise [FieldMaskedModel::InaccessibleError]
|
230
|
+
def validate!(field)
|
231
|
+
if !@accessible_fields.include?(field)
|
232
|
+
if self.class.inaccessible_error_callback
|
233
|
+
self.class.inaccessible_error_callback.call(field)
|
234
|
+
else
|
235
|
+
raise FieldMaskedModel::InaccessibleError.new("`#{field}` is not specified as paths in field_mask!")
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module FieldMaskedModel
|
2
|
+
class ModelPool
|
3
|
+
class << self
|
4
|
+
def generated_pool
|
5
|
+
@generated_pool ||= self.new
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@table = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param [Class] msgclass A class represents the protobuf message class
|
14
|
+
# @return [Class] A class inheriting Models::Base
|
15
|
+
def lookup(msgclass)
|
16
|
+
@table[msgclass]
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [Class] msgclass A class represents the protobuf message class
|
20
|
+
# @param [Class] modelclass A class inheriting Models::Base
|
21
|
+
def add(msgclass, modelclass)
|
22
|
+
@table[msgclass] = modelclass
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: field_masked_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nao Minami
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.11'
|
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.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: fmparser
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.1.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.1.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: google-protobuf
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.7'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.7'
|
97
|
+
description: FieldMaskedModel provides masked accessor methods to models
|
98
|
+
email:
|
99
|
+
- south37777@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- examples/main.rb
|
115
|
+
- examples/protos/lib/users_pb.rb
|
116
|
+
- examples/protos/users.proto
|
117
|
+
- field_masked_model.gemspec
|
118
|
+
- lib/field_masked_model.rb
|
119
|
+
- lib/field_masked_model/attribute_converter.rb
|
120
|
+
- lib/field_masked_model/base.rb
|
121
|
+
- lib/field_masked_model/error.rb
|
122
|
+
- lib/field_masked_model/model_pool.rb
|
123
|
+
- lib/field_masked_model/version.rb
|
124
|
+
homepage: https://github.com/south37/field_masked_model
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubygems_version: 3.0.3
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: FieldMaskedModel provides masked accessor methods to models
|
147
|
+
test_files: []
|