field_mask_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ceeb1f2018ddb48e63a37be9520164c55fdceec7ec9012648796994e71b21931
4
+ data.tar.gz: c89c2c36fc1de17edb857ad3f15f9cfe523ffa3c96bfb1a5880999340ac794d6
5
+ SHA512:
6
+ metadata.gz: a18bb68410e57dfa63e7c066395f336754b512f47f21bf2326eb6b9ae9deaf0f9ca61c77ec4df9bd371d2e2127830269141f452c7b8544254183d4ce1da25dfd
7
+ data.tar.gz: ff3cd3f80ca759bd79ee890a1f836ea74a50a212ef65fd300b1d6196951f8d4297956843388567984db5ed5cea4c7c0a97a9c456f77504d841cc697fe64fd676
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in field_mask_parser.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ field_mask_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (5.2.1)
10
+ activesupport (= 5.2.1)
11
+ activerecord (5.2.1)
12
+ activemodel (= 5.2.1)
13
+ activesupport (= 5.2.1)
14
+ arel (>= 9.0)
15
+ activesupport (5.2.1)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ arel (9.0.0)
21
+ coderay (1.1.2)
22
+ concurrent-ruby (1.0.5)
23
+ diff-lcs (1.3)
24
+ i18n (1.1.0)
25
+ concurrent-ruby (~> 1.0)
26
+ method_source (0.9.0)
27
+ minitest (5.11.3)
28
+ pry (0.11.3)
29
+ coderay (~> 1.1.0)
30
+ method_source (~> 0.9.0)
31
+ rake (10.5.0)
32
+ rspec (3.8.0)
33
+ rspec-core (~> 3.8.0)
34
+ rspec-expectations (~> 3.8.0)
35
+ rspec-mocks (~> 3.8.0)
36
+ rspec-core (3.8.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-expectations (3.8.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-mocks (3.8.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-support (3.8.0)
45
+ thread_safe (0.3.6)
46
+ tzinfo (1.2.5)
47
+ thread_safe (~> 0.1)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ activerecord (~> 5.2)
54
+ bundler (~> 1.16)
55
+ field_mask_parser!
56
+ pry (~> 0.11)
57
+ rake (~> 10.0)
58
+ rspec (~> 3.8)
59
+
60
+ BUNDLED WITH
61
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 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,85 @@
1
+ # FieldMaskParser
2
+
3
+ FieldMask parameter parser.
4
+ If you want to know more about FieldMask, please see https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#fieldmask .
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'field_mask_parser'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install field_mask_parser
21
+
22
+ ## Usage
23
+
24
+ `FieldMaskParser.parse` parses FieldMask parameter and returns `FieldMaskParser::Node` object. It requires `paths` string and ActiveRecord class as `root`.
25
+
26
+ ```ruby
27
+ # app/models/user.rb
28
+
29
+ # == Schema Information
30
+ #
31
+ # Table name: users
32
+ #
33
+ # id :integer not null, primary key
34
+ #
35
+ class User
36
+ has_one :profile
37
+ end
38
+ ```
39
+
40
+ ```ruby
41
+
42
+ # app/models/profile.rb
43
+
44
+ # == Schema Information
45
+ #
46
+ # Table name: users
47
+ #
48
+ # id :integer not null, primary key
49
+ # user_id :string not null, indexed
50
+ # name :string not null
51
+ #
52
+ class Profile
53
+ belongs_to :user
54
+ end
55
+ ```
56
+
57
+ ```ruby
58
+ [1] pry> FieldMaskParser.parse(paths: ["id", "profile.name"], root: User).to_h
59
+ =>
60
+ {
61
+ :name=>nil,
62
+ :attrs=>[:id],
63
+ :assocs=>[
64
+ {
65
+ :name=>:profile,
66
+ :attrs=>[:name],
67
+ :assocs=>[]
68
+ }
69
+ ]
70
+ }
71
+ ```
72
+
73
+ ## Development
74
+
75
+ 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.
76
+
77
+ 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).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/south37/field_mask_parser.
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "field_mask_parser"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "field_mask_parser/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "field_mask_parser"
8
+ spec.version = FieldMaskParser::VERSION
9
+ spec.authors = ["Nao Minami"]
10
+ spec.email = ["south37777@gmail.com"]
11
+
12
+ spec.summary = %q{Parser for field_mask parameter.}
13
+ spec.description = %q{Parser for field_mask parameter.}
14
+ spec.homepage = "https://github.com/south37/field_mask_parser"
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.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.8"
29
+ spec.add_development_dependency "activerecord", "~> 5.2"
30
+ spec.add_development_dependency "pry", "~> 0.11"
31
+ end
@@ -0,0 +1,18 @@
1
+ module FieldMaskParser
2
+ module Dispatcher
3
+ class ActiveRecordDispatcher < Base
4
+ # @param [klass] inheriting ActiveRecord::Base
5
+ # @param [Symbol] name
6
+ # @return [Dispatcher::Type]
7
+ def dispatch(klass, name)
8
+ if klass.attribute_names.include?(name.to_s)
9
+ Type::ATTRIBUTE
10
+ elsif klass.reflect_on_association(name)
11
+ Type::ASSOCIATION
12
+ else
13
+ Type::UNKNOWN
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module FieldMaskParser
2
+ module Dispatcher
3
+ class Base
4
+ # @param [klass] inheriting ActiveRecord::Base
5
+ # @param [name]
6
+ # @return [Dispatcher::Type]
7
+ def dispatch(klass, name)
8
+ raise NotImplementedError.new("#{self.class.name}#dispatch is an abstract method.")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module FieldMaskParser
2
+ module Dispatcher
3
+ class Type
4
+ # @param [String] type
5
+ def initialize(type)
6
+ @type = type
7
+ end
8
+
9
+ ATTRIBUTE = self.new("Dispatcher::Type::ATTRIBUTE")
10
+ ASSOCIATION = self.new("Dispatcher::Type::ASSOCIATION")
11
+ UNKNOWN = self.new("Dispatcher::Type::UNKNOWN")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require 'field_mask_parser/dispatcher/type'
2
+ require 'field_mask_parser/dispatcher/base'
3
+ require 'field_mask_parser/dispatcher/active_record_dispatcher'
4
+
5
+ module FieldMaskParser
6
+ module Dispatcher
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module FieldMaskParser
2
+ class Node
3
+ attr_reader :name, :attrs, :assocs
4
+
5
+ # @param [Symbol | NilClass] name nil when being top-level node
6
+ # @param [<Symbol>] attrs
7
+ # @param [Set<Node>] assocs
8
+ def initialize(name:)
9
+ @name = name
10
+ @attrs = []
11
+ @assocs = []
12
+ end
13
+
14
+ # @param [Symbol] f
15
+ def push_attr(f)
16
+ @attrs.push(f)
17
+ end
18
+
19
+ # @param [Node] n
20
+ def push_assoc(n)
21
+ @assocs.push(n)
22
+ end
23
+
24
+ def to_h
25
+ { name: @name, attrs: @attrs, assocs: @assocs.map(&:to_h) }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,83 @@
1
+ module FieldMaskParser
2
+ class Parser
3
+ # HashBuilder build nested hash from paths (string array).
4
+ # ex.
5
+ # param: ["id", "facebook_uid", "detail.id", "detail.email"]
6
+ # return: {:id=>{}, :facebook_uid=>{}, :detail=>{:id=>{}, :email=>{}}}
7
+ class HashBuilder
8
+ class << self
9
+ # @param [<String>] paths
10
+ # @return [{ Symbol => Hash }]
11
+ def build(paths)
12
+ h = {}
13
+ paths.each do |path|
14
+ deep_push!(h, path.split('.').map(&:strip).map(&:to_sym))
15
+ end
16
+ h
17
+ end
18
+
19
+ private
20
+
21
+ # @param [Hash] h
22
+ # @param [<String>] field_list
23
+ def deep_push!(h, field_list)
24
+ if field_list.size < 1
25
+ # Do nothing
26
+ return
27
+ end
28
+ f = field_list.first
29
+ if !h[f]
30
+ h[f] = {}
31
+ end
32
+ deep_push!(h[f], field_list[1..-1])
33
+ end
34
+ end
35
+ end
36
+
37
+ # @param [<String>] paths
38
+ def initialize(paths:, root:, dispatcher: nil)
39
+ @attrs_or_assocs = HashBuilder.build(paths)
40
+ @root = root
41
+ @dispatcher = dispatcher || Dispatcher::ActiveRecordDispatcher.new
42
+ end
43
+
44
+ # @reutrn [Node]
45
+ def parse
46
+ r = Node.new(name: nil)
47
+ set_attrs_and_assocs!(r, @attrs_or_assocs, @root)
48
+ r
49
+ end
50
+
51
+ private
52
+
53
+ # @param [Node] node
54
+ # @param [Hash] attrs_or_assocs
55
+ # @param [Class] klass inheriting ActiveRecord::Base
56
+ def set_attrs_and_assocs!(node, attrs_or_assocs, klass)
57
+ attrs_or_assocs.each do |name, _attrs_or_assocs|
58
+ case @dispatcher.dispatch(klass, name)
59
+ when Dispatcher::Type::ATTRIBUTE
60
+ node.push_attr(name)
61
+ when Dispatcher::Type::ASSOCIATION
62
+ n = Node.new(name: name)
63
+ node.push_assoc(n)
64
+ if _attrs_or_assocs.size > 0
65
+ _klass = get_assoc_klass(klass, name)
66
+ set_attrs_and_assocs!(n, _attrs_or_assocs, _klass)
67
+ end
68
+ when Dispatcher::Type::UNKNOWN
69
+ # Ignore invalid field
70
+ else
71
+ raise "invalid type (#{@dispatcher.dispatch(klass, name)}) with klass (#{klass}) and name (#{name})"
72
+ end
73
+ end
74
+ end
75
+
76
+ # @param [Class] parent
77
+ # @param [Symbol] name
78
+ # @return [Class]
79
+ def get_assoc_klass(parent, name)
80
+ parent.reflect_on_association(name).klass
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module FieldMaskParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "field_mask_parser/dispatcher"
2
+ require "field_mask_parser/node"
3
+ require "field_mask_parser/parser"
4
+ require "field_mask_parser/version"
5
+
6
+ module FieldMaskParser
7
+ class << self
8
+ # @param [<String>] paths
9
+ # @return [Node]
10
+ def parse(paths:, root:, dispatcher: nil)
11
+ Parser.new(paths: paths, root: root, dispatcher: dispatcher).parse
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: field_mask_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nao Minami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-28 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ description: Parser for field_mask parameter.
84
+ email:
85
+ - south37777@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - field_mask_parser.gemspec
100
+ - lib/field_mask_parser.rb
101
+ - lib/field_mask_parser/dispatcher.rb
102
+ - lib/field_mask_parser/dispatcher/active_record_dispatcher.rb
103
+ - lib/field_mask_parser/dispatcher/base.rb
104
+ - lib/field_mask_parser/dispatcher/type.rb
105
+ - lib/field_mask_parser/node.rb
106
+ - lib/field_mask_parser/parser.rb
107
+ - lib/field_mask_parser/version.rb
108
+ homepage: https://github.com/south37/field_mask_parser
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Parser for field_mask parameter.
132
+ test_files: []