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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ffc85276d16c72328a2e3f875c9b4f4ec11a6b8bee4976c6aebf5d77e266c44
4
- data.tar.gz: 04357c1acce3b82c820cf2d4855301cea0670c84bc266bf5a49c54745a900812
3
+ metadata.gz: 38f5a42c2c54bca7181d9d4ca27e9169b20c4aa360f15630de3892fb178e8af0
4
+ data.tar.gz: 05cb2a3625d5bf88bbd0dd4302c116c224b535fc5df1ec122300e1ea280e41d3
5
5
  SHA512:
6
- metadata.gz: 3345941f970b56f855e68b46ad25824f02c099c75a92fe9458b627124cccea6747b79d7ec22e80a61c0a7aa8b7da9a94046b8caf8137f978e68892aa20072f4b
7
- data.tar.gz: 965aa5fdc3f14ffee21c1beeef5036183423de9682a5cc0265c0b589d53d8cbd4479316ed68a338c691f4c490d72c2fcfabdc2e21a8620e2c33c749c0ad64143
6
+ metadata.gz: 3ddb7a8153f7e06543c05d222f53e28c202560a05055004c39f4d5dda9f75b6d15cf824ee5cad136a7309268af9bb291bbe11c4191ccaee45266daed2918d25f
7
+ data.tar.gz: 6a89cf3c0d1ffe95aedcf0e2625aafb48922f9bb4cca8d5208283efec89afac92b6ed1d21ca0dbf0093b3e38cfbb4389c4949030b02bf70784e5f16552c09b1c
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.5.1
5
- before_install: gem install bundler -v 1.16.2
4
+ - 2.5.1
5
+ before_install:
6
+ - gem install bundler -v 1.16.2
7
+ script:
8
+ - bundle exec rspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- field_mask_parser (0.3.2)
4
+ field_mask_parser (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -62,15 +62,17 @@ end
62
62
  :is_leaf=>false,
63
63
  :klass=>User,
64
64
  :attrs=>[:id],
65
- :assocs=>[
65
+ :has_ones=>[
66
66
  {
67
67
  :name=>:profile,
68
68
  :is_leaf=>false,
69
69
  :klass=>Profile,
70
70
  :attrs=>[:name],
71
- :assocs=>[]
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
- Type::ASSOCIATION
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 = self.new("Dispatcher::Type::ATTRIBUTE")
10
- ASSOCIATION = self.new("Dispatcher::Type::ASSOCIATION")
11
- UNKNOWN = self.new("Dispatcher::Type::UNKNOWN")
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, :assocs
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 = name
12
- @is_leaf = is_leaf
13
- @klass = klass
14
- @attrs = []
15
- @assocs = []
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 push_assoc(n)
25
- @assocs.push(n)
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: @name,
31
- is_leaf: @is_leaf,
32
- klass: @klass,
33
- attrs: @attrs,
34
- assocs: @assocs.map(&:to_h)
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
- case @dispatcher.dispatch(klass, name)
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::ASSOCIATION
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
- node.push_assoc(n)
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
@@ -1,3 +1,3 @@
1
1
  module FieldMaskParser
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.1"
3
3
  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.3.2
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-10-19 00:00:00.000000000 Z
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler