tarquinn 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +85 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +29 -0
- data/.rubocop_todo.yml +13 -0
- data/Dockerfile +21 -0
- data/Gemfile +16 -2
- data/README.md +16 -0
- data/Rakefile +8 -3
- data/config/check_specs.yml +3 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.rb +13 -0
- data/config/yardstick.yml +33 -0
- data/docker-compose.yml +23 -0
- data/lib/tarquinn/class_methods.rb +43 -12
- data/lib/tarquinn/condition/action_checker.rb +24 -7
- data/lib/tarquinn/condition/method_caller.rb +25 -8
- data/lib/tarquinn/condition/proc_runner.rb +18 -7
- data/lib/tarquinn/condition.rb +57 -4
- data/lib/tarquinn/controller.rb +56 -13
- data/lib/tarquinn/redirection_config.rb +92 -0
- data/lib/tarquinn/redirection_handler.rb +97 -0
- data/lib/tarquinn/request_handler.rb +61 -0
- data/lib/tarquinn/request_handler_builder.rb +84 -0
- data/lib/tarquinn/version.rb +3 -1
- data/lib/tarquinn.rb +74 -8
- data/spec/dummy/Rakefile +8 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +1 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/tarquinn/dummy_controller.rb +43 -0
- data/spec/dummy/app/controllers/tarquinn/dummy_route_controller.rb +37 -0
- data/spec/dummy/app/helpers/application_helper.rb +4 -0
- data/spec/dummy/app/jobs/application_job.rb +9 -0
- data/spec/dummy/app/mailers/application_mailer.rb +6 -0
- data/spec/dummy/app/models/application_record.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/app/views/tarquinn/dummy_route/index.html +0 -0
- data/spec/dummy/app/views/tarquinn/dummy_route/new.html +0 -0
- data/spec/dummy/bin/rails +6 -0
- data/spec/dummy/bin/rake +6 -0
- data/spec/dummy/bin/setup +35 -0
- data/spec/dummy/config/application.rb +24 -0
- data/spec/dummy/config/boot.rb +7 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +7 -0
- data/spec/dummy/config/environments/development.rb +69 -0
- data/spec/dummy/config/environments/production.rb +89 -0
- data/spec/dummy/config/environments/test.rb +62 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +26 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +10 -0
- data/spec/dummy/config/initializers/inflections.rb +17 -0
- data/spec/dummy/config/initializers/permissions_policy.rb +12 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +45 -0
- data/spec/dummy/config/routes.rb +8 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +8 -0
- data/spec/dummy/db/schema.rb +16 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/storage/.keep +0 -0
- data/spec/dummy/tmp/.keep +0 -0
- data/spec/dummy/tmp/pids/.keep +0 -0
- data/spec/dummy/tmp/storage/.keep +0 -0
- data/spec/lib/tarquinn/condition/action_checker_spec.rb +4 -2
- data/spec/lib/tarquinn/condition/method_caller_spec.rb +5 -3
- data/spec/lib/tarquinn/condition/proc_runner_spec.rb +56 -4
- data/spec/lib/tarquinn/condition_spec.rb +53 -0
- data/spec/lib/tarquinn/controller_spec.rb +83 -10
- data/spec/lib/tarquinn/{config_spec.rb → redirection_config_spec.rb} +3 -1
- data/spec/lib/tarquinn/{handler_spec.rb → redirection_handler_spec.rb} +21 -3
- data/spec/lib/tarquinn/request_handler_builder_spec.rb +15 -0
- data/spec/lib/tarquinn/{engine_spec.rb → request_handler_spec.rb} +5 -3
- data/spec/lib/tarquinn_spec.rb +176 -13
- data/spec/spec_helper.rb +17 -7
- data/spec/support/shared_examples/config.rb +5 -4
- data/tarquinn.gemspec +10 -14
- data/tarquinn.jpg +0 -0
- metadata +83 -107
- data/Gemfile.lock +0 -58
- data/lib/tarquinn/builder.rb +0 -28
- data/lib/tarquinn/concern.rb +0 -17
- data/lib/tarquinn/config.rb +0 -39
- data/lib/tarquinn/engine.rb +0 -31
- data/lib/tarquinn/handler.rb +0 -49
- data/spec/lib/tarquinn/builder_spec.rb +0 -13
- data/spec/support/models/tarquinn/dummy_controller.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 40a2cd722ae376e627aba7f4d4e10b69df45b74593d89dbafd68b9f6e4cb635a
|
4
|
+
data.tar.gz: 7c29d8a9763a3f0ae3e71945ea16b690628b0a3fef4f3603d891dbe791a4a0ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b63bfd5ba62e690b145bc4394e1913f28e5756b325b8301fb1b467d1386b282950095d36638ee407c2404e30d098a79de7868285a390b643fb2e84b275f16c6
|
7
|
+
data.tar.gz: '0286cd856bbaa74643cf9174952aff220ed75e3b81ff22f07c451b95bf226a78919ac8e306bbde78b9ab2f0577cd7b707bf4b0f4d25da69830b5340d58bfc5d9'
|
@@ -0,0 +1,85 @@
|
|
1
|
+
version: 2
|
2
|
+
workflows:
|
3
|
+
version: 2
|
4
|
+
test-and-build:
|
5
|
+
jobs:
|
6
|
+
- test:
|
7
|
+
filters:
|
8
|
+
tags:
|
9
|
+
only: /.*/
|
10
|
+
- checks:
|
11
|
+
filters:
|
12
|
+
tags:
|
13
|
+
only: /.*/
|
14
|
+
- build-and-release:
|
15
|
+
requires: [test, checks]
|
16
|
+
filters:
|
17
|
+
tags:
|
18
|
+
only: /\d+\.\d+\.\d+/
|
19
|
+
branches:
|
20
|
+
only:
|
21
|
+
- master
|
22
|
+
jobs:
|
23
|
+
test:
|
24
|
+
docker:
|
25
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
26
|
+
environment:
|
27
|
+
PROJECT: tarquinn
|
28
|
+
steps:
|
29
|
+
- checkout
|
30
|
+
- run:
|
31
|
+
name: Prepare Coverage Test Report
|
32
|
+
command: cc-test-reporter before-build
|
33
|
+
- run:
|
34
|
+
name: Bundle Install
|
35
|
+
command: bundle install
|
36
|
+
- run:
|
37
|
+
name: RSpec
|
38
|
+
command: bundle exec rspec
|
39
|
+
- run:
|
40
|
+
name: Coverage Test Report
|
41
|
+
command: cc-test-reporter after-build --exit-code $?
|
42
|
+
checks:
|
43
|
+
docker:
|
44
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
45
|
+
environment:
|
46
|
+
PROJECT: tarquinn
|
47
|
+
steps:
|
48
|
+
- checkout
|
49
|
+
- run:
|
50
|
+
name: Bundle Install
|
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
|
58
|
+
- run:
|
59
|
+
name: Check version documentation
|
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
|
67
|
+
build-and-release:
|
68
|
+
docker:
|
69
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
70
|
+
environment:
|
71
|
+
PROJECT: tarquinn
|
72
|
+
steps:
|
73
|
+
- checkout
|
74
|
+
- run:
|
75
|
+
name: Bundle Install
|
76
|
+
command: bundle install
|
77
|
+
- run:
|
78
|
+
name: Signin
|
79
|
+
command: build_gem.sh signin
|
80
|
+
- run:
|
81
|
+
name: Build Gem
|
82
|
+
command: build_gem.sh build
|
83
|
+
- run:
|
84
|
+
name: Push Gem
|
85
|
+
command: build_gem.sh push
|
data/.gitignore
CHANGED
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
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
FROM darthjee/scripts:0.4.2 as scripts
|
2
|
+
|
3
|
+
FROM darthjee/ruby_331:1.0.1 as base
|
4
|
+
|
5
|
+
COPY --chown=app:app ./ /home/app/app/
|
6
|
+
|
7
|
+
######################################
|
8
|
+
|
9
|
+
FROM base as builder
|
10
|
+
|
11
|
+
COPY --chown=app:app --from=scripts /home/scripts/builder/bundle_builder.sh /usr/local/sbin/bundle_builder.sh
|
12
|
+
|
13
|
+
ENV HOME_DIR /home/app
|
14
|
+
RUN bundle_builder.sh
|
15
|
+
|
16
|
+
#######################
|
17
|
+
#FINAL IMAGE
|
18
|
+
FROM base
|
19
|
+
|
20
|
+
COPY --chown=app:app --from=builder /home/app/bundle/ /usr/local/bundle/
|
21
|
+
RUN bundle install
|
data/Gemfile
CHANGED
@@ -1,4 +1,18 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
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'
|
4
17
|
|
18
|
+
gemspec
|
data/README.md
CHANGED
@@ -1,8 +1,24 @@
|
|
1
1
|
Tarquinn
|
2
2
|
========
|
3
|
+
[![Build Status](https://circleci.com/gh/darthjee/tarquinn.svg?style=shield)](https://circleci.com/gh/darthjee/tarquinn)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/darthjee/tarquinn/badges/gpa.svg)](https://codeclimate.com/github/darthjee/tarquinn)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/darthjee/tarquinn/badges/coverage.svg)](https://codeclimate.com/github/darthjee/tarquinn/coverage)
|
6
|
+
[![Issue Count](https://codeclimate.com/github/darthjee/tarquinn/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/tarquinn)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/tarquinn.svg)](https://badge.fury.io/rb/tarquinn)
|
8
|
+
[![Inline docs](http://inch-ci.org/github/darthjee/tarquinn.svg)](http://inch-ci.org/github/darthjee/tarquinn)
|
9
|
+
|
10
|
+
![tarquinn](https://raw.githubusercontent.com/darthjee/tarquinn/master/tarquinn.jpg)
|
11
|
+
|
12
|
+
Yard Documentation
|
13
|
+
-------------------
|
14
|
+
[https://www.rubydoc.info/gems/tarquinn/0.3.0](https://www.rubydoc.info/gems/tarquinn/0.3.0)
|
3
15
|
|
4
16
|
This gem makes easier to controll generic redirection
|
5
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
|
+
|
6
22
|
Getting started
|
7
23
|
---------------
|
8
24
|
1. Add Tarquinn to your `Gemfile` and `bundle install`:
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
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
|
|
6
|
-
task :
|
11
|
+
task default: :spec
|
7
12
|
task test: :spec
|
data/config/rubycritc.rb
ADDED
data/config/yardstick.rb
ADDED
@@ -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: []
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
version: '3'
|
2
|
+
services:
|
3
|
+
base: &base
|
4
|
+
image: tarquinn
|
5
|
+
working_dir: /home/app/app
|
6
|
+
volumes:
|
7
|
+
- .:/home/app/app
|
8
|
+
|
9
|
+
base_build:
|
10
|
+
<<: *base
|
11
|
+
build: .
|
12
|
+
command: echo done
|
13
|
+
|
14
|
+
tarquinn:
|
15
|
+
<<: *base
|
16
|
+
container_name: tarquinn
|
17
|
+
depends_on: [base_build]
|
18
|
+
command: /bin/bash -c 'rspec'
|
19
|
+
|
20
|
+
test_all:
|
21
|
+
<<: *base
|
22
|
+
depends_on: [base_build]
|
23
|
+
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
@@ -1,17 +1,48 @@
|
|
1
|
-
|
2
|
-
def skip_redirection(redirection, *actions)
|
3
|
-
redirector_builder.add_skip_action(redirection, *actions)
|
4
|
-
end
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
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
|
-
|
2
|
-
attr_accessor :routes
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
25
|
+
attr_reader :routes
|
26
|
+
end
|
10
27
|
end
|
11
28
|
end
|
@@ -1,13 +1,30 @@
|
|
1
|
-
|
2
|
-
attr_accessor :methods
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
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
|
-
|
2
|
-
attr_reader :block
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
19
|
+
attr_reader :block
|
20
|
+
end
|
10
21
|
end
|
11
22
|
end
|
data/lib/tarquinn/condition.rb
CHANGED
@@ -1,5 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
data/lib/tarquinn/controller.rb
CHANGED
@@ -1,19 +1,62 @@
|
|
1
|
-
|
2
|
-
attr_reader :controller
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
12
|
+
# Returns request parameters
|
13
|
+
#
|
14
|
+
# @return [ActionController::Parameters]
|
15
|
+
def params
|
16
|
+
@params ||= controller.send(:params)
|
17
|
+
end
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
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
|