tarquinn 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +15 -3
  3. data/.gitignore +9 -0
  4. data/.rubocop.yml +29 -0
  5. data/.rubocop_todo.yml +13 -0
  6. data/Dockerfile +2 -2
  7. data/Gemfile +15 -0
  8. data/README.md +5 -1
  9. data/Rakefile +5 -0
  10. data/config/check_specs.yml +3 -0
  11. data/config/rubycritc.rb +12 -0
  12. data/config/yardstick.rb +13 -0
  13. data/config/yardstick.yml +33 -0
  14. data/lib/tarquinn/class_methods.rb +43 -12
  15. data/lib/tarquinn/condition/action_checker.rb +24 -7
  16. data/lib/tarquinn/condition/method_caller.rb +25 -8
  17. data/lib/tarquinn/condition/proc_runner.rb +18 -7
  18. data/lib/tarquinn/condition.rb +57 -4
  19. data/lib/tarquinn/controller.rb +56 -13
  20. data/lib/tarquinn/redirection_config.rb +92 -0
  21. data/lib/tarquinn/redirection_handler.rb +97 -0
  22. data/lib/tarquinn/request_handler.rb +61 -0
  23. data/lib/tarquinn/request_handler_builder.rb +84 -0
  24. data/lib/tarquinn/version.rb +3 -1
  25. data/lib/tarquinn.rb +74 -8
  26. data/spec/dummy/Rakefile +8 -0
  27. data/spec/dummy/app/assets/images/.keep +0 -0
  28. data/spec/dummy/app/assets/stylesheets/application.css +1 -0
  29. data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
  30. data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  32. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  33. data/spec/dummy/app/controllers/tarquinn/dummy_controller.rb +43 -0
  34. data/spec/dummy/app/controllers/tarquinn/dummy_route_controller.rb +37 -0
  35. data/spec/dummy/app/helpers/application_helper.rb +4 -0
  36. data/spec/dummy/app/jobs/application_job.rb +9 -0
  37. data/spec/dummy/app/mailers/application_mailer.rb +6 -0
  38. data/spec/dummy/app/models/application_record.rb +5 -0
  39. data/spec/dummy/app/models/concerns/.keep +0 -0
  40. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  41. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  42. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  43. data/spec/dummy/app/views/tarquinn/dummy_route/index.html +0 -0
  44. data/spec/dummy/app/views/tarquinn/dummy_route/new.html +0 -0
  45. data/spec/dummy/bin/rails +6 -0
  46. data/spec/dummy/bin/rake +6 -0
  47. data/spec/dummy/bin/setup +35 -0
  48. data/spec/dummy/config/application.rb +24 -0
  49. data/spec/dummy/config/boot.rb +7 -0
  50. data/spec/dummy/config/cable.yml +10 -0
  51. data/spec/dummy/config/database.yml +25 -0
  52. data/spec/dummy/config/environment.rb +7 -0
  53. data/spec/dummy/config/environments/development.rb +69 -0
  54. data/spec/dummy/config/environments/production.rb +89 -0
  55. data/spec/dummy/config/environments/test.rb +62 -0
  56. data/spec/dummy/config/initializers/content_security_policy.rb +26 -0
  57. data/spec/dummy/config/initializers/filter_parameter_logging.rb +10 -0
  58. data/spec/dummy/config/initializers/inflections.rb +17 -0
  59. data/spec/dummy/config/initializers/permissions_policy.rb +12 -0
  60. data/spec/dummy/config/locales/en.yml +33 -0
  61. data/spec/dummy/config/puma.rb +45 -0
  62. data/spec/dummy/config/routes.rb +8 -0
  63. data/spec/dummy/config/storage.yml +34 -0
  64. data/spec/dummy/config.ru +8 -0
  65. data/spec/dummy/db/schema.rb +16 -0
  66. data/spec/dummy/lib/assets/.keep +0 -0
  67. data/spec/dummy/log/.keep +0 -0
  68. data/spec/dummy/public/404.html +67 -0
  69. data/spec/dummy/public/422.html +67 -0
  70. data/spec/dummy/public/500.html +66 -0
  71. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  72. data/spec/dummy/public/apple-touch-icon.png +0 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/storage/.keep +0 -0
  75. data/spec/dummy/tmp/.keep +0 -0
  76. data/spec/dummy/tmp/pids/.keep +0 -0
  77. data/spec/dummy/tmp/storage/.keep +0 -0
  78. data/spec/lib/tarquinn/condition/action_checker_spec.rb +4 -2
  79. data/spec/lib/tarquinn/condition/method_caller_spec.rb +5 -3
  80. data/spec/lib/tarquinn/condition/proc_runner_spec.rb +56 -4
  81. data/spec/lib/tarquinn/condition_spec.rb +53 -0
  82. data/spec/lib/tarquinn/controller_spec.rb +83 -10
  83. data/spec/lib/tarquinn/{config_spec.rb → redirection_config_spec.rb} +3 -1
  84. data/spec/lib/tarquinn/{handler_spec.rb → redirection_handler_spec.rb} +21 -3
  85. data/spec/lib/tarquinn/request_handler_builder_spec.rb +15 -0
  86. data/spec/lib/tarquinn/{engine_spec.rb → request_handler_spec.rb} +5 -3
  87. data/spec/lib/tarquinn_spec.rb +176 -13
  88. data/spec/spec_helper.rb +16 -6
  89. data/spec/support/shared_examples/config.rb +4 -2
  90. data/tarquinn.gemspec +8 -12
  91. metadata +73 -100
  92. data/lib/tarquinn/builder.rb +0 -28
  93. data/lib/tarquinn/concern.rb +0 -17
  94. data/lib/tarquinn/config.rb +0 -39
  95. data/lib/tarquinn/engine.rb +0 -31
  96. data/lib/tarquinn/handler.rb +0 -49
  97. data/spec/lib/tarquinn/builder_spec.rb +0 -13
  98. data/spec/support/models/tarquinn/dummy_controller.rb +0 -43
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b942f4c6eef09e4a94c12a33da00f6c878ebeb30d348dc06dad7aa2274d635b9
4
- data.tar.gz: b80da172aef6a2067b97444c53842bed6215afbbba5d885793376f8f8ff8f6a6
3
+ metadata.gz: 40a2cd722ae376e627aba7f4d4e10b69df45b74593d89dbafd68b9f6e4cb635a
4
+ data.tar.gz: 7c29d8a9763a3f0ae3e71945ea16b690628b0a3fef4f3603d891dbe791a4a0ca
5
5
  SHA512:
6
- metadata.gz: cbb0a21142829270e9ac2590ff071ed5dfada57c2bf6ba116543b9c8c735eb88f8170a6550b3badbc56506dbae2dfc109f76c88c7e40efaa90c94a3b3bb61629
7
- data.tar.gz: 1b7fe947a50f0ee13e0ef2cdd1842b18f19af51c347d494442f34abf2eb4195f5335fcbdec8794933465652af544fa463b8d3846a49ff4c12835d17ace565d03
6
+ metadata.gz: 5b63bfd5ba62e690b145bc4394e1913f28e5756b325b8301fb1b467d1386b282950095d36638ee407c2404e30d098a79de7868285a390b643fb2e84b275f16c6
7
+ data.tar.gz: '0286cd856bbaa74643cf9174952aff220ed75e3b81ff22f07c451b95bf226a78919ac8e306bbde78b9ab2f0577cd7b707bf4b0f4d25da69830b5340d58bfc5d9'
data/.circleci/config.yml CHANGED
@@ -22,7 +22,7 @@ workflows:
22
22
  jobs:
23
23
  test:
24
24
  docker:
25
- - image: darthjee/circleci_rails_gems:0.6.2
25
+ - image: darthjee/circleci_ruby_331:1.0.1
26
26
  environment:
27
27
  PROJECT: tarquinn
28
28
  steps:
@@ -41,7 +41,7 @@ jobs:
41
41
  command: cc-test-reporter after-build --exit-code $?
42
42
  checks:
43
43
  docker:
44
- - image: darthjee/circleci_rails_gems:0.6.2
44
+ - image: darthjee/circleci_ruby_331:1.0.1
45
45
  environment:
46
46
  PROJECT: tarquinn
47
47
  steps:
@@ -49,12 +49,24 @@ jobs:
49
49
  - run:
50
50
  name: Bundle Install
51
51
  command: bundle install
52
+ - run:
53
+ name: Rubocop
54
+ command: rubocop
55
+ - run:
56
+ name: Yardstick coverage check
57
+ command: bundle exec rake verify_measurements
52
58
  - run:
53
59
  name: Check version documentation
54
60
  command: check_readme.sh
61
+ - run:
62
+ name: Rubycritcs check
63
+ command: rubycritic.sh
64
+ - run:
65
+ name: Check unit tests
66
+ command: check_specs
55
67
  build-and-release:
56
68
  docker:
57
- - image: darthjee/circleci_rails_gems:0.6.2
69
+ - image: darthjee/circleci_ruby_331:1.0.1
58
70
  environment:
59
71
  PROJECT: tarquinn
60
72
  steps:
data/.gitignore CHANGED
@@ -1,3 +1,12 @@
1
1
  /coverage
2
2
  /pkg
3
3
  Gemfile.lock
4
+ **/*.swp
5
+ **/*.swo
6
+ spec/dummy/log/*
7
+ spec/dummy/tmp/*
8
+ **/*.sqlite3
9
+ doc
10
+ .yardoc
11
+ measurement
12
+ rubycritic
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 3.3
5
+ NewCops: enable
6
+
7
+ Lint/BooleanSymbol:
8
+ Enabled: false
9
+
10
+ Lint/EmptyBlock:
11
+ Exclude:
12
+ - spec/dummy/db/schema.rb
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - spec/**/*_spec.rb
17
+
18
+ Lint/MissingSuper:
19
+ Enabled: false
20
+
21
+ Lint/ScriptPermission:
22
+ Exclude:
23
+ - spec/dummy/bin/*
24
+
25
+ Naming/BlockForwarding:
26
+ Enabled: false
27
+
28
+ Style/ArgumentsForwarding:
29
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,13 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2024-07-10 16:10:57 UTC using RuboCop version 1.59.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 9
10
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
11
+ # AllowedMethods: refine
12
+ Metrics/BlockLength:
13
+ Max: 87
data/Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
- FROM darthjee/scripts:0.1.8 as scripts
1
+ FROM darthjee/scripts:0.4.2 as scripts
2
2
 
3
- FROM darthjee/rails_gems:0.6.2 as base
3
+ FROM darthjee/ruby_331:1.0.1 as base
4
4
 
5
5
  COPY --chown=app:app ./ /home/app/app/
6
6
 
data/Gemfile CHANGED
@@ -1,3 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
5
+ gem 'bundler', '~> 2.5.13'
6
+ gem 'pry', '~> 0.14.2'
7
+ gem 'pry-nav', '~> 1.0.0'
8
+ gem 'rails', '7.0.4.3'
9
+ gem 'rake', '~> 13.1.0'
10
+ gem 'rspec', '~> 3.12.0'
11
+ gem 'rspec-rails', '6.0.2'
12
+ gem 'rubycritic', '4.9.1'
13
+ gem 'simplecov', '~> 0.22.0'
14
+ gem 'sqlite3', '1.4.2'
15
+ gem 'yard', '0.9.36'
16
+ gem 'yardstick', '0.9.9'
17
+
3
18
  gemspec
data/README.md CHANGED
@@ -11,10 +11,14 @@ Tarquinn
11
11
 
12
12
  Yard Documentation
13
13
  -------------------
14
- [https://www.rubydoc.info/gems/tarquinn/0.2.0](https://www.rubydoc.info/gems/tarquinn/0.2.0)
14
+ [https://www.rubydoc.info/gems/tarquinn/0.3.0](https://www.rubydoc.info/gems/tarquinn/0.3.0)
15
15
 
16
16
  This gem makes easier to controll generic redirection
17
17
 
18
+ Current Release: [0.3.0](https://github.com/darthjee/tarquinn/tree/0.3.0)
19
+
20
+ [Next release](https://github.com/darthjee/tarquinn/compare/0.3.0...master)
21
+
18
22
  Getting started
19
23
  ---------------
20
24
  1. Add Tarquinn to your `Gemfile` and `bundle install`:
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
5
+ require 'yardstick/rake/measurement'
6
+ require './config/yardstick'
7
+ require './config/rubycritc'
3
8
 
4
9
  RSpec::Core::RakeTask.new
5
10
 
@@ -0,0 +1,3 @@
1
+ ignore:
2
+ - lib/tarquinn/class_methods.rb
3
+ - lib/tarquinn/version.rb
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubycritic/rake_task'
4
+
5
+ RubyCritic::RakeTask.new do |task|
6
+ options = %w[
7
+ --path rubycritic/
8
+ --no-browser
9
+ ]
10
+ task.options = options.join(' ')
11
+ task.paths = %w[lib]
12
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yardstick/rake/measurement'
4
+ require 'yardstick/rake/verify'
5
+ require 'yaml'
6
+
7
+ options = YAML.load_file('config/yardstick.yml')
8
+
9
+ Yardstick::Rake::Measurement.new(:yardstick_measure, options) do |measurement|
10
+ measurement.output = 'measurement/report.txt'
11
+ end
12
+
13
+ Yardstick::Rake::Verify.new(:verify_measurements, options)
@@ -0,0 +1,33 @@
1
+ threshold: 88.1
2
+ require_exact_threshold: false
3
+ rules:
4
+ ApiTag::Presence:
5
+ enabled: true
6
+ exclude: []
7
+ ApiTag::Inclusion:
8
+ enabled: true
9
+ exclude: []
10
+ ApiTag::ProtectedMethod:
11
+ enabled: true
12
+ exclude: []
13
+ ApiTag::PrivateMethod:
14
+ enabled: true
15
+ exclude: []
16
+ ExampleTag:
17
+ enabled: true
18
+ exclude: []
19
+ ReturnTag:
20
+ enabled: true
21
+ exclude: []
22
+ Summary::Presence:
23
+ enabled: true
24
+ exclude: []
25
+ Summary::Length:
26
+ enabled: true
27
+ exclude: []
28
+ Summary::Delimiter:
29
+ enabled: true
30
+ exclude: []
31
+ Summary::SingleLine:
32
+ enabled: true
33
+ exclude: []
@@ -1,17 +1,48 @@
1
- module Tarquinn::ClassMethods
2
- def skip_redirection(redirection, *actions)
3
- redirector_builder.add_skip_action(redirection, *actions)
4
- end
1
+ # frozen_string_literal: true
5
2
 
6
- def redirection_rule(redirection, *methods, &block)
7
- redirector_builder.add_redirection_config(redirection, *methods, block)
8
- end
3
+ module Tarquinn
4
+ # @api public
5
+ #
6
+ # Methods added by Tarquinn
7
+ module ClassMethods
8
+ # Creates a redirection rule
9
+ #
10
+ # The rule name defines which method will be called when checking the path of redirection
11
+ #
12
+ # @param (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
13
+ # @return (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
14
+ def redirection_rule(redirection, *methods, &)
15
+ redirector_builder.add_redirection_config(redirection, *methods, &)
16
+ end
9
17
 
10
- def skip_redirection_rule(redirection, *methods, &block)
11
- redirector_builder.add_skip_config(redirection, *methods, block)
12
- end
18
+ # Attaches a condition to skip a redirection based on route (controller action)
19
+ #
20
+ # When any of the skip rules is met the redirection is skipped
21
+ #
22
+ # @param (see Tarquinn::RequestHandlerBuilder#add_skip_action)
23
+ # @return (see Tarquinn::RequestHandlerBuilder#add_skip_action)
24
+ def skip_redirection(redirection, *actions)
25
+ redirector_builder.add_skip_action(redirection, *actions)
26
+ end
27
+
28
+ # Attaches conditions to skip a redirection
29
+ #
30
+ # Methods and blocks are ran and if any returns true, the redirec is skipped
31
+ #
32
+ # @param (see Tarquinn::RequestHandlerBuilder#add_skip_config)
33
+ # @return (see Tarquinn::RequestHandlerBuilder#add_skip_config)
34
+ def skip_redirection_rule(redirection, *methods, &block)
35
+ redirector_builder.add_skip_config(redirection, *methods, block)
36
+ end
13
37
 
14
- def redirector_builder
15
- @redirector_builder ||= Tarquinn::Builder.new
38
+ # Retruns the RequestHandlerBuilder
39
+ #
40
+ # RequestHandlerBuilder will Carry all the configurations and will create
41
+ # one {RequestHandler} for each request
42
+ #
43
+ # @return [RequestHandlerBuilder]
44
+ def redirector_builder
45
+ @redirector_builder ||= Tarquinn::RequestHandlerBuilder.new
46
+ end
16
47
  end
17
48
  end
@@ -1,11 +1,28 @@
1
- class Tarquinn::Condition::ActionChecker
2
- attr_accessor :routes
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(routes)
5
- @routes = [routes].flatten.map(&:to_s)
6
- end
3
+ module Tarquinn
4
+ class Condition
5
+ # @api private
6
+ #
7
+ # Checks condition based on route action
8
+ class ActionChecker < Tarquinn::Condition
9
+ def initialize(routes)
10
+ @routes = [routes].flatten.map(&:to_s)
11
+ end
12
+
13
+ def check?(controller)
14
+ routes.include? controller.params[:action]
15
+ end
16
+
17
+ def ==(other)
18
+ return false unless other.class == self.class
19
+
20
+ other.routes == routes
21
+ end
22
+
23
+ protected
7
24
 
8
- def check?(controller)
9
- routes.include? controller.params[:action]
25
+ attr_reader :routes
26
+ end
10
27
  end
11
28
  end
@@ -1,13 +1,30 @@
1
- class Tarquinn::Condition::MethodCaller
2
- attr_accessor :methods
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(methods)
5
- @methods = [methods].flatten
6
- end
3
+ module Tarquinn
4
+ class Condition
5
+ # @api private
6
+ #
7
+ # Checks condition based on result of method call from controller
8
+ class MethodCaller < Tarquinn::Condition
9
+ def initialize(methods)
10
+ @methods = [methods].flatten
11
+ end
12
+
13
+ def check?(controller)
14
+ methods.any? do |method|
15
+ controller.call(method)
16
+ end
17
+ end
18
+
19
+ def ==(other)
20
+ return false unless other.class == self.class
21
+
22
+ other.methods == methods
23
+ end
24
+
25
+ protected
7
26
 
8
- def check?(controller)
9
- methods.any? do |method|
10
- controller.call(method)
27
+ attr_reader :methods
11
28
  end
12
29
  end
13
30
  end
@@ -1,11 +1,22 @@
1
- class Tarquinn::Condition::ProcRunner
2
- attr_reader :block
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(&block)
5
- @block = block
6
- end
3
+ module Tarquinn
4
+ class Condition
5
+ # @api private
6
+ #
7
+ # Checks condition based on a given block
8
+ class ProcRunner < Tarquinn::Condition
9
+ def initialize(&block)
10
+ @block = block
11
+ end
12
+
13
+ def check?(controller)
14
+ controller.run(&block)
15
+ end
16
+
17
+ private
7
18
 
8
- def check?(controller)
9
- block.yield(controller)
19
+ attr_reader :block
20
+ end
10
21
  end
11
22
  end
@@ -1,5 +1,58 @@
1
- module Tarquinn::Condition
2
- require 'tarquinn/condition/action_checker'
3
- require 'tarquinn/condition/method_caller'
4
- require 'tarquinn/condition/proc_runner'
1
+ # frozen_string_literal: true
2
+
3
+ module Tarquinn
4
+ # @api private
5
+ # @abstract
6
+ #
7
+ # Redirection condition
8
+ class Condition
9
+ autoload :ActionChecker, 'tarquinn/condition/action_checker'
10
+ autoload :MethodCaller, 'tarquinn/condition/method_caller'
11
+ autoload :ProcRunner, 'tarquinn/condition/proc_runner'
12
+
13
+ class << self
14
+ # Creates a method caller condition
15
+ #
16
+ # @param methods [Array<Symbol>] list of methods to be called for condition
17
+ #
18
+ # @return [Tarquinn::Condition::MethodCaller] when methods are given
19
+ # @return [NilClass] when no methods are given
20
+ def method_caller(methods)
21
+ return if methods.empty?
22
+
23
+ Tarquinn::Condition::MethodCaller.new(methods)
24
+ end
25
+
26
+ # Creates an action checker condition
27
+ #
28
+ # @param routes [Array<Symbol>] controller actions that will match the condition
29
+ #
30
+ # @return [Tarquinn::Condition::ActionChecker]
31
+ def action_checker(routes)
32
+ Tarquinn::Condition::ActionChecker.new(routes)
33
+ end
34
+
35
+ # Cretes a proc checker
36
+ #
37
+ # @param block [Proc] block to be ran when condition is checked
38
+ #
39
+ # @return [Tarquinn::Condition::ProcRunner] When a block is given
40
+ # @return [NilClass] when no block is given
41
+ def proc_runner(&block)
42
+ return unless block
43
+
44
+ Tarquinn::Condition::ProcRunner.new(&block)
45
+ end
46
+ end
47
+
48
+ # Checks if a condition is matched
49
+ #
50
+ # @param _controller [Tarquinn::Controller] the controller with the request params
51
+ #
52
+ # @return [TrueClass] When it is a match
53
+ # @return [FalseClass] When it is not a match
54
+ def check?(_controller)
55
+ raise NotImplementedError, 'Needs to be implemented in child class'
56
+ end
57
+ end
5
58
  end
@@ -1,19 +1,62 @@
1
- class Tarquinn::Controller
2
- attr_reader :controller
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(controller)
5
- @controller = controller
6
- end
3
+ module Tarquinn
4
+ # @api private
5
+ #
6
+ # Controller interface
7
+ class Controller
8
+ def initialize(controller)
9
+ @controller = controller
10
+ end
7
11
 
8
- def params
9
- @params ||= controller.send(:params)
10
- end
12
+ # Returns request parameters
13
+ #
14
+ # @return [ActionController::Parameters]
15
+ def params
16
+ @params ||= controller.send(:params)
17
+ end
11
18
 
12
- def call(method, *args)
13
- controller.send(method, *args)
14
- end
19
+ # Calls a method from the controller
20
+ #
21
+ # @param method [Symbol] method name to be called
22
+ # @param args [Array<Symbol>] Method arguments
23
+ # @param opts [Hash<Symbol,Object>] Method arguments
24
+ # @param block [Proc] block to be given to the method
25
+ #
26
+ # @return [Object]
27
+ def call(method, *args, **opts, &block)
28
+ controller.send(method, *args, **opts, &block)
29
+ end
30
+
31
+ # Run a block in teh context of the controller
32
+ #
33
+ # @param block [Proc] block to be ran
34
+ #
35
+ # @return [Object] the return of the block
36
+ def run(&block)
37
+ controller.send(:instance_eval, &block)
38
+ end
39
+
40
+ # Checks if the controller responds to a method
41
+ #
42
+ # @param method [Symbol] method name
43
+ #
44
+ # @return [TrueClass] the controller responds to this method
45
+ # @return [FalseClass] the controller does not responds to this method
46
+ def method?(method)
47
+ controller.respond_to?(method, true)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :controller
15
53
 
16
- def has_method?(method)
17
- controller.respond_to?(method, true)
54
+ # @method controller
55
+ # @api private
56
+ # @private
57
+ #
58
+ # Returns the controller handling the request
59
+ #
60
+ # @return [ActionController::Base]
18
61
  end
19
62
  end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tarquinn
4
+ # @api private
5
+ #
6
+ # Redirection configuration
7
+ #
8
+ # @see Tarquinn::RequestHandler
9
+ class RedirectionConfig
10
+ attr_reader :redirect
11
+
12
+ # @param redirect [Symbol] redirection name and redirection method
13
+ def initialize(redirect)
14
+ @redirect = redirect
15
+ end
16
+
17
+ # Adds conditions to the rule
18
+ #
19
+ # The rule name defines which method will be called when checking the path of redirection
20
+ #
21
+ # @param methods [Array<Symbol>] Methods that tell that a redirection should be applied
22
+ # @param block [Proc] block that tells if a the redirection should be applied
23
+ #
24
+ # @return [Array<Tarquinn::Condition>] Current registered conditions
25
+ def add_redirection_rules(*methods, &block)
26
+ redirect_on method_caller(methods)
27
+ redirect_on proc_runner(&block)
28
+ end
29
+
30
+ # Add rule for skipping on some actions / routes
31
+ #
32
+ # @param actions [Array<Symbol>] actions / routes to be skipped by redirection rule
33
+ #
34
+ # @return [Array<Tarquinn::Condition>]
35
+ def add_skip_action(*actions)
36
+ skip action_checker(actions)
37
+ end
38
+
39
+ # Attaches conditions to skip a redirection
40
+ #
41
+ # Methods and blocks are ran and if any returns true, the redirec is skipped
42
+ #
43
+ # @param methods [Array<Symbol>] Methods that tell that a redirection should be skipped
44
+ # @param block [Proc] block that tells if a the redirection should be skipped
45
+ #
46
+ # @return [Array] Current registered conditions
47
+ def add_skip_rules(*methods, &block)
48
+ skip method_caller(methods)
49
+ skip proc_runner(&block)
50
+ end
51
+
52
+ # All blocks that indicate a redirection
53
+ #
54
+ # @return [Array<Tarquinn::Condition>]
55
+ def redirection_blocks
56
+ @redirection_blocks ||= []
57
+ end
58
+
59
+ # All blocks that indicate a redirection should be skipped
60
+ #
61
+ # @return [Array<Tarquinn::Condition>]
62
+ def skip_blocks
63
+ @skip_blocks ||= []
64
+ end
65
+
66
+ private
67
+
68
+ delegate :method_caller, :action_checker, :proc_runner, to: Tarquinn::Condition
69
+
70
+ # @private
71
+ #
72
+ # Adds a condition to skip a redirection
73
+ #
74
+ # @return (see #skip_blocks)
75
+ def skip(condition)
76
+ return skip_blocks unless condition
77
+
78
+ skip_blocks << condition
79
+ end
80
+
81
+ # @private
82
+ #
83
+ # Adds a condition to a redirection
84
+ #
85
+ # @return (see #redirection_blocks)
86
+ def redirect_on(condition)
87
+ return redirection_blocks unless condition
88
+
89
+ redirection_blocks << condition
90
+ end
91
+ end
92
+ end