action_extractor 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: a3079ac747a929a97ea87527d83a3c8414444f4e56c03aacf6ff072811b658bb
4
+ data.tar.gz: 673c5d80b0025ff6b7521b20af1dafc486c23d5eee7fb190260883d9db1424f9
5
+ SHA512:
6
+ metadata.gz: 2c365da88fd0b3e2ebcc7343a644c15734d70ba97b53567b9e518224098389c44c106fd4de8850f1c92f9c7bb84b40f84e3866d56f79eb8eb023869afed84fd1
7
+ data.tar.gz: aea46a727f69b75b5f898c0de9be950e3d9e1b49ccfcd22922bc50d0e816b8fc86f7e44d5039c3c841d3029cfedd635ef9d8bb522600823bcea7fe93e3a671ee
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 2.6
5
+
6
+ Metrics:
7
+ Enabled: false
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Style/EmptyMethod:
13
+ EnforcedStyle: expanded
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [Unreleased]
2
+
3
+ ## 0.1.0 - 2021-10-25
4
+
5
+ ### Added
6
+
7
+ - Initial release.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at r7kamura@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in action_extractor.gemspec
6
+ gemspec
7
+
8
+ gem 'rack-test'
9
+ gem 'rails', '~> 6.1'
10
+ gem 'rake', '~> 13.0'
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,180 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action_extractor (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.1.4.1)
10
+ actionpack (= 6.1.4.1)
11
+ activesupport (= 6.1.4.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.1.4.1)
15
+ actionpack (= 6.1.4.1)
16
+ activejob (= 6.1.4.1)
17
+ activerecord (= 6.1.4.1)
18
+ activestorage (= 6.1.4.1)
19
+ activesupport (= 6.1.4.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.1.4.1)
22
+ actionpack (= 6.1.4.1)
23
+ actionview (= 6.1.4.1)
24
+ activejob (= 6.1.4.1)
25
+ activesupport (= 6.1.4.1)
26
+ mail (~> 2.5, >= 2.5.4)
27
+ rails-dom-testing (~> 2.0)
28
+ actionpack (6.1.4.1)
29
+ actionview (= 6.1.4.1)
30
+ activesupport (= 6.1.4.1)
31
+ rack (~> 2.0, >= 2.0.9)
32
+ rack-test (>= 0.6.3)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
+ actiontext (6.1.4.1)
36
+ actionpack (= 6.1.4.1)
37
+ activerecord (= 6.1.4.1)
38
+ activestorage (= 6.1.4.1)
39
+ activesupport (= 6.1.4.1)
40
+ nokogiri (>= 1.8.5)
41
+ actionview (6.1.4.1)
42
+ activesupport (= 6.1.4.1)
43
+ builder (~> 3.1)
44
+ erubi (~> 1.4)
45
+ rails-dom-testing (~> 2.0)
46
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
+ activejob (6.1.4.1)
48
+ activesupport (= 6.1.4.1)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.1.4.1)
51
+ activesupport (= 6.1.4.1)
52
+ activerecord (6.1.4.1)
53
+ activemodel (= 6.1.4.1)
54
+ activesupport (= 6.1.4.1)
55
+ activestorage (6.1.4.1)
56
+ actionpack (= 6.1.4.1)
57
+ activejob (= 6.1.4.1)
58
+ activerecord (= 6.1.4.1)
59
+ activesupport (= 6.1.4.1)
60
+ marcel (~> 1.0.0)
61
+ mini_mime (>= 1.1.0)
62
+ activesupport (6.1.4.1)
63
+ concurrent-ruby (~> 1.0, >= 1.0.2)
64
+ i18n (>= 1.6, < 2)
65
+ minitest (>= 5.1)
66
+ tzinfo (~> 2.0)
67
+ zeitwerk (~> 2.3)
68
+ ast (2.4.2)
69
+ builder (3.2.4)
70
+ concurrent-ruby (1.1.9)
71
+ crass (1.0.6)
72
+ diff-lcs (1.4.4)
73
+ erubi (1.10.0)
74
+ globalid (0.5.2)
75
+ activesupport (>= 5.0)
76
+ i18n (1.8.10)
77
+ concurrent-ruby (~> 1.0)
78
+ loofah (2.12.0)
79
+ crass (~> 1.0.2)
80
+ nokogiri (>= 1.5.9)
81
+ mail (2.7.1)
82
+ mini_mime (>= 0.1.1)
83
+ marcel (1.0.2)
84
+ method_source (1.0.0)
85
+ mini_mime (1.1.2)
86
+ minitest (5.14.4)
87
+ nio4r (2.5.8)
88
+ nokogiri (1.12.5-x86_64-linux)
89
+ racc (~> 1.4)
90
+ parallel (1.21.0)
91
+ parser (3.0.2.0)
92
+ ast (~> 2.4.1)
93
+ racc (1.6.0)
94
+ rack (2.2.3)
95
+ rack-test (1.1.0)
96
+ rack (>= 1.0, < 3)
97
+ rails (6.1.4.1)
98
+ actioncable (= 6.1.4.1)
99
+ actionmailbox (= 6.1.4.1)
100
+ actionmailer (= 6.1.4.1)
101
+ actionpack (= 6.1.4.1)
102
+ actiontext (= 6.1.4.1)
103
+ actionview (= 6.1.4.1)
104
+ activejob (= 6.1.4.1)
105
+ activemodel (= 6.1.4.1)
106
+ activerecord (= 6.1.4.1)
107
+ activestorage (= 6.1.4.1)
108
+ activesupport (= 6.1.4.1)
109
+ bundler (>= 1.15.0)
110
+ railties (= 6.1.4.1)
111
+ sprockets-rails (>= 2.0.0)
112
+ rails-dom-testing (2.0.3)
113
+ activesupport (>= 4.2.0)
114
+ nokogiri (>= 1.6)
115
+ rails-html-sanitizer (1.4.2)
116
+ loofah (~> 2.3)
117
+ railties (6.1.4.1)
118
+ actionpack (= 6.1.4.1)
119
+ activesupport (= 6.1.4.1)
120
+ method_source
121
+ rake (>= 0.13)
122
+ thor (~> 1.0)
123
+ rainbow (3.0.0)
124
+ rake (13.0.6)
125
+ regexp_parser (2.1.1)
126
+ rexml (3.2.5)
127
+ rspec (3.10.0)
128
+ rspec-core (~> 3.10.0)
129
+ rspec-expectations (~> 3.10.0)
130
+ rspec-mocks (~> 3.10.0)
131
+ rspec-core (3.10.1)
132
+ rspec-support (~> 3.10.0)
133
+ rspec-expectations (3.10.1)
134
+ diff-lcs (>= 1.2.0, < 2.0)
135
+ rspec-support (~> 3.10.0)
136
+ rspec-mocks (3.10.2)
137
+ diff-lcs (>= 1.2.0, < 2.0)
138
+ rspec-support (~> 3.10.0)
139
+ rspec-support (3.10.2)
140
+ rubocop (1.22.2)
141
+ parallel (~> 1.10)
142
+ parser (>= 3.0.0.0)
143
+ rainbow (>= 2.2.2, < 4.0)
144
+ regexp_parser (>= 1.8, < 3.0)
145
+ rexml
146
+ rubocop-ast (>= 1.12.0, < 2.0)
147
+ ruby-progressbar (~> 1.7)
148
+ unicode-display_width (>= 1.4.0, < 3.0)
149
+ rubocop-ast (1.12.0)
150
+ parser (>= 3.0.1.1)
151
+ ruby-progressbar (1.11.0)
152
+ sprockets (4.0.2)
153
+ concurrent-ruby (~> 1.0)
154
+ rack (> 1, < 3)
155
+ sprockets-rails (3.2.2)
156
+ actionpack (>= 4.0)
157
+ activesupport (>= 4.0)
158
+ sprockets (>= 3.0.0)
159
+ thor (1.1.0)
160
+ tzinfo (2.0.4)
161
+ concurrent-ruby (~> 1.0)
162
+ unicode-display_width (2.1.0)
163
+ websocket-driver (0.7.5)
164
+ websocket-extensions (>= 0.1.0)
165
+ websocket-extensions (0.1.5)
166
+ zeitwerk (2.5.1)
167
+
168
+ PLATFORMS
169
+ x86_64-linux
170
+
171
+ DEPENDENCIES
172
+ action_extractor!
173
+ rack-test
174
+ rails (~> 6.1)
175
+ rake (~> 13.0)
176
+ rspec (~> 3.0)
177
+ rubocop (~> 1.21)
178
+
179
+ BUNDLED WITH
180
+ 2.2.29
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Ryo Nakamura
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,117 @@
1
+ # ActionExtractor
2
+
3
+ Arguments extractor for Rails actions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your Rails application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'action_extractor'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class ArticlesController < ApplicationController
23
+ extract(
24
+ article_id: {
25
+ from: :path,
26
+ schema: {
27
+ type: 'integer',
28
+ },
29
+ },
30
+ body: {
31
+ from: :form_data,
32
+ schema: {
33
+ type: 'string',
34
+ },
35
+ },
36
+ request_id: {
37
+ from: :header,
38
+ name: 'X-Request-Id',
39
+ schema: {
40
+ format: 'uuid',
41
+ type: 'string',
42
+ },
43
+ },
44
+ title: {
45
+ from: :form_data,
46
+ schema: {
47
+ type: 'string',
48
+ },
49
+ },
50
+ ).on \
51
+ def update(
52
+ article_id:,
53
+ body:,
54
+ request_id:,
55
+ title:
56
+ )
57
+ article = Article.find(article_id)
58
+ if article.update(
59
+ body: body,
60
+ title: title,
61
+ request_id: request_id,
62
+ )
63
+ redirect_to article
64
+ else
65
+ @article = article
66
+ render :edit, status: 400
67
+ end
68
+ end
69
+ end
70
+ ```
71
+
72
+ ### `.extract(...).on(action_name)`
73
+
74
+ By using this DSL method, we can state what kind of input value the subsequent action is expecting.
75
+
76
+ Note that the argument names passed here will also be used at the action call.
77
+
78
+ ### `:from`
79
+
80
+ This value represents where the data is comming from.
81
+
82
+ Supported values are:
83
+
84
+ - `:form_data`
85
+ - Extract data while assuming the request body is encoded from form data.
86
+ - `:header`
87
+ - Extract data from HTTP request header.
88
+ - `:path`
89
+ - Extract data from URL path parameters.
90
+ - `:query`
91
+ - Extract data from URL query parameters.
92
+
93
+ ### `:name`
94
+
95
+ This value represents what name of value to extract from the data source.
96
+
97
+ In many cases, this option is not necessary because the argument name is used as the key for extraction instead.
98
+ This is useful for the case when the names overlap or when extracting from data source with different naming convention (e.g. HTTP headers).
99
+
100
+ ### `:schema`
101
+
102
+ It's currently unfinished, but we intend to use this value for the following purposes:
103
+
104
+ - Validate input values with OpenAPI compatible implementation
105
+ - Provide metadata that can be used to generate OpenAPI document
106
+
107
+ ## Acknowledgements
108
+
109
+ This gem was inspired by the following libraries:
110
+
111
+ - [actix/actix-web](https://github.com/actix/actix-web)
112
+ - [asakusarb/action_args](https://github.com/asakusarb/action_args)
113
+ - [r7kamura/weak_parameters](https://github.com/r7kamura/weak_parameters)
114
+
115
+ ## License
116
+
117
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/action_extractor/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'action_extractor'
7
+ spec.version = ActionExtractor::VERSION
8
+ spec.authors = ['Ryo Nakamura']
9
+ spec.email = ['r7kamura@gmail.com']
10
+
11
+ spec.summary = 'Arguments extractor for Rails actions.'
12
+ spec.homepage = "https://github.com/r7kamura/#{spec.name}"
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'action_extractor'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module ActionArgumentsTakable
5
+ # @note Override.
6
+ def send_action(method_name, *arguments)
7
+ extraction = self.class.extractions[method_name]
8
+ if extraction
9
+ keyword_arguments = extraction.definitions.each_with_object({}) do |(argument_name, definition), object|
10
+ extractor = Extractors::Base.extractors[definition[:from]]
11
+ unless extractor
12
+ raise ::ArgumentError, "Unknown :from value in `.extract` arguments: `#{definition[:from].inspect}`."
13
+ end
14
+
15
+ object[argument_name] = extractor.call(
16
+ argument_name: argument_name,
17
+ controller: self,
18
+ definition: definition
19
+ )
20
+ end
21
+ super(
22
+ method_name,
23
+ *arguments,
24
+ **keyword_arguments,
25
+ )
26
+ else
27
+ super
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module ControllerDsl
5
+ def extract(**definitions)
6
+ Extraction.new(
7
+ controller_class: self,
8
+ definitions: definitions
9
+ )
10
+ end
11
+
12
+ # @return [Hash]
13
+ def extractions
14
+ @extractions ||= {}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ class Extraction
5
+ # @return [Hash]
6
+ attr_reader :definitions
7
+
8
+ # @param [Class] controller_class
9
+ # @param [Hash] definitions
10
+ def initialize(
11
+ controller_class:,
12
+ definitions:
13
+ )
14
+ @controller_class = controller_class
15
+ @definitions = definitions
16
+ end
17
+
18
+ # @param [Symbol] action_name
19
+ def on(action_name)
20
+ @controller_class.extractions[action_name.to_s] = self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module Extractors
5
+ class Base
6
+ class << self
7
+ # @param [Symbol] argument_name
8
+ # @param [ActionController::Base] controller
9
+ # @param [Hash] definition
10
+ def call(
11
+ argument_name:,
12
+ controller:,
13
+ definition:
14
+ )
15
+ new(
16
+ argument_name: argument_name,
17
+ controller: controller,
18
+ definition: definition
19
+ ).call
20
+ end
21
+
22
+ # @return [Hash{Symbol => #call}]
23
+ def extractors
24
+ @extractors ||= {}
25
+ end
26
+
27
+ # @note Override.
28
+ def inherited(child)
29
+ super
30
+ extractors[child.to_s.split('::').last.underscore.to_sym] = child
31
+ end
32
+ end
33
+
34
+ # @param [Symbol] argument_name
35
+ # @param [ActionController::Base] controller
36
+ # @param [Hash] definition
37
+ def initialize(
38
+ argument_name:,
39
+ controller:,
40
+ definition:
41
+ )
42
+ @argument_name = argument_name
43
+ @controller = controller
44
+ @definition = definition
45
+ end
46
+
47
+ # @return [String]
48
+ def name
49
+ @definition[:name] || @argument_name.to_s
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module Extractors
5
+ class FormData < Base
6
+ def call
7
+ @controller.request.request_parameters[name.to_sym]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module Extractors
5
+ class Header < Base
6
+ def call
7
+ @controller.request.headers[name]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module Extractors
5
+ class Path < Base
6
+ def call
7
+ @controller.request.path_parameters[name.to_sym]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ module Extractors
5
+ class Query < Base
6
+ def call
7
+ @controller.request.query_parameters[name.to_sym]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_extractor/extractors/base'
4
+ require 'action_extractor/extractors/header'
5
+ require 'action_extractor/extractors/form_data'
6
+ require 'action_extractor/extractors/path'
7
+ require 'action_extractor/extractors/query'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionExtractor
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'action_extractor/version'
4
+
5
+ module ActionExtractor
6
+ autoload :ActionArgumentsTakable, 'action_extractor/action_arguments_takable'
7
+ autoload :ControllerDsl, 'action_extractor/controller_dsl'
8
+ autoload :Extraction, 'action_extractor/extraction'
9
+ autoload :Extractors, 'action_extractor/extractors'
10
+ end
11
+
12
+ AbstractController::Base.prepend ActionExtractor::ActionArgumentsTakable
13
+
14
+ ActiveSupport.on_load(:action_controller) do
15
+ extend ActionExtractor::ControllerDsl
16
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_extractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - r7kamura@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - action_extractor.gemspec
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/action_extractor.rb
33
+ - lib/action_extractor/action_arguments_takable.rb
34
+ - lib/action_extractor/controller_dsl.rb
35
+ - lib/action_extractor/extraction.rb
36
+ - lib/action_extractor/extractors.rb
37
+ - lib/action_extractor/extractors/base.rb
38
+ - lib/action_extractor/extractors/form_data.rb
39
+ - lib/action_extractor/extractors/header.rb
40
+ - lib/action_extractor/extractors/path.rb
41
+ - lib/action_extractor/extractors/query.rb
42
+ - lib/action_extractor/version.rb
43
+ homepage: https://github.com/r7kamura/action_extractor
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/r7kamura/action_extractor
48
+ source_code_uri: https://github.com/r7kamura/action_extractor
49
+ changelog_uri: https://github.com/r7kamura/action_extractor/blob/main/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.6.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.15
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Arguments extractor for Rails actions.
69
+ test_files: []