field_mask_parser 0.3.2 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/Gemfile.lock +1 -1
- data/README.md +5 -3
- data/lib/field_mask_parser/dispatcher/active_record_dispatcher.rb +9 -2
- data/lib/field_mask_parser/dispatcher/type.rb +4 -3
- data/lib/field_mask_parser/node.rb +24 -15
- data/lib/field_mask_parser/parser.rb +9 -3
- data/lib/field_mask_parser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38f5a42c2c54bca7181d9d4ca27e9169b20c4aa360f15630de3892fb178e8af0
|
4
|
+
data.tar.gz: 05cb2a3625d5bf88bbd0dd4302c116c224b535fc5df1ec122300e1ea280e41d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ddb7a8153f7e06543c05d222f53e28c202560a05055004c39f4d5dda9f75b6d15cf824ee5cad136a7309268af9bb291bbe11c4191ccaee45266daed2918d25f
|
7
|
+
data.tar.gz: 6a89cf3c0d1ffe95aedcf0e2625aafb48922f9bb4cca8d5208283efec89afac92b6ed1d21ca0dbf0093b3e38cfbb4389c4949030b02bf70784e5f16552c09b1c
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -62,15 +62,17 @@ end
|
|
62
62
|
:is_leaf=>false,
|
63
63
|
:klass=>User,
|
64
64
|
:attrs=>[:id],
|
65
|
-
:
|
65
|
+
:has_ones=>[
|
66
66
|
{
|
67
67
|
:name=>:profile,
|
68
68
|
:is_leaf=>false,
|
69
69
|
:klass=>Profile,
|
70
70
|
:attrs=>[:name],
|
71
|
-
:
|
71
|
+
:has_ones=>[],
|
72
|
+
:has_manies=>[]
|
72
73
|
}
|
73
|
-
]
|
74
|
+
],
|
75
|
+
:has_manies=>[]
|
74
76
|
}
|
75
77
|
```
|
76
78
|
|
@@ -7,8 +7,15 @@ module FieldMaskParser
|
|
7
7
|
def dispatch(klass, name)
|
8
8
|
if klass.attribute_names.include?(name.to_s)
|
9
9
|
Type::ATTRIBUTE
|
10
|
-
elsif klass.reflect_on_association(name)
|
11
|
-
|
10
|
+
elsif (assoc = klass.reflect_on_association(name))
|
11
|
+
case assoc
|
12
|
+
when ActiveRecord::Reflection::HasOneReflection
|
13
|
+
Type::HAS_ONE
|
14
|
+
when ActiveRecord::Reflection::HasManyReflection
|
15
|
+
Type::HAS_MANY
|
16
|
+
else
|
17
|
+
raise "invalid association!"
|
18
|
+
end
|
12
19
|
else
|
13
20
|
Type::UNKNOWN
|
14
21
|
end
|
@@ -6,9 +6,10 @@ module FieldMaskParser
|
|
6
6
|
@type = type
|
7
7
|
end
|
8
8
|
|
9
|
-
ATTRIBUTE
|
10
|
-
|
11
|
-
|
9
|
+
ATTRIBUTE = self.new("Dispatcher::Type::ATTRIBUTE")
|
10
|
+
HAS_ONE = self.new("Dispatcher::Type::HAS_ONE")
|
11
|
+
HAS_MANY = self.new("Dispatcher::Type::HAS_MANY")
|
12
|
+
UNKNOWN = self.new("Dispatcher::Type::UNKNOWN")
|
12
13
|
end
|
13
14
|
end
|
14
15
|
end
|
@@ -1,18 +1,17 @@
|
|
1
1
|
module FieldMaskParser
|
2
2
|
class Node
|
3
|
-
attr_reader :name, :is_leaf, :klass, :attrs, :
|
3
|
+
attr_reader :name, :is_leaf, :klass, :attrs, :has_ones, :has_manies
|
4
4
|
|
5
5
|
# @param [Symbol | NilClass] name nil when being top-level node
|
6
6
|
# @param [bool] is_leaf
|
7
|
-
# @param [<Symbol>] attrs
|
8
|
-
# @param [Set<Node>] assocs
|
9
7
|
# @param [ActiveRecord::Base] klass
|
10
8
|
def initialize(name:, is_leaf:, klass:)
|
11
|
-
@name
|
12
|
-
@is_leaf
|
13
|
-
@klass
|
14
|
-
@attrs
|
15
|
-
@
|
9
|
+
@name = name
|
10
|
+
@is_leaf = is_leaf
|
11
|
+
@klass = klass
|
12
|
+
@attrs = []
|
13
|
+
@has_ones = []
|
14
|
+
@has_manies = []
|
16
15
|
end
|
17
16
|
|
18
17
|
# @param [Symbol] f
|
@@ -21,17 +20,27 @@ module FieldMaskParser
|
|
21
20
|
end
|
22
21
|
|
23
22
|
# @param [Node] n
|
24
|
-
def
|
25
|
-
@
|
23
|
+
def push_has_one(n)
|
24
|
+
@has_ones.push(n)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param [Node] n
|
28
|
+
def push_has_many(n)
|
29
|
+
@has_manies.push(n)
|
30
|
+
end
|
31
|
+
|
32
|
+
def assocs
|
33
|
+
has_ones + has_manies
|
26
34
|
end
|
27
35
|
|
28
36
|
def to_h
|
29
37
|
{
|
30
|
-
name:
|
31
|
-
is_leaf:
|
32
|
-
klass:
|
33
|
-
attrs:
|
34
|
-
|
38
|
+
name: @name,
|
39
|
+
is_leaf: @is_leaf,
|
40
|
+
klass: @klass,
|
41
|
+
attrs: @attrs,
|
42
|
+
has_ones: @has_ones.map(&:to_h),
|
43
|
+
has_manies: @has_manies.map(&:to_h),
|
35
44
|
}
|
36
45
|
end
|
37
46
|
end
|
@@ -24,14 +24,20 @@ module FieldMaskParser
|
|
24
24
|
# @param [Class] klass inheriting ActiveRecord::Base
|
25
25
|
def set_attrs_and_assocs!(node, attrs_or_assocs, klass)
|
26
26
|
attrs_or_assocs.each do |name, dhn|
|
27
|
-
|
27
|
+
type = @dispatcher.dispatch(klass, name)
|
28
|
+
|
29
|
+
case type
|
28
30
|
when Dispatcher::Type::ATTRIBUTE
|
29
31
|
# NOTE: If dhn.is_leaf is false, it is invalid. But ignore it now.
|
30
32
|
node.push_attr(name)
|
31
|
-
when Dispatcher::Type::
|
33
|
+
when Dispatcher::Type::HAS_ONE, Dispatcher::Type::HAS_MANY
|
32
34
|
_klass = get_assoc_klass(klass, name)
|
33
35
|
n = Node.new(name: name, is_leaf: dhn.is_leaf, klass: _klass)
|
34
|
-
|
36
|
+
if type == Dispatcher::Type::HAS_ONE
|
37
|
+
node.push_has_one(n)
|
38
|
+
else # Dispatcher::Type::HAS_MANY
|
39
|
+
node.push_has_many(n)
|
40
|
+
end
|
35
41
|
if dhn.children.size > 0
|
36
42
|
set_attrs_and_assocs!(n, dhn.children, _klass)
|
37
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: field_mask_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nao Minami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|