bullet_train-routes 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8d9eed745b71e644f5250ac838c9b7b90bc406517d519e88d756000817d8091
4
+ data.tar.gz: a02b34b728eead3aa580902870f2f043e83478ce76e1d270352d555ce6ffdcb1
5
+ SHA512:
6
+ metadata.gz: 4f10d82cb88ab53614610db11fa0c661ff71961cca0f50733af2c19296e1d7b2d3f86b776644eafed73a41d60303536e158e40f0c74b033dd698fafe4e4ca800
7
+ data.tar.gz: 74c433463a89fe78543ca3ba7497f1004e3039aace672c0d7bfc2f1b8a23beaa984b9963741df0c0adaef90ded23b36a04cc85ebcadbb2a94f8fb76b92a24983
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Andrew Culver
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # BulletTrain::Routes
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "bullet_train-routes"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install bullet_train-routes
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,109 @@
1
+ module BulletTrain
2
+ module Routes
3
+ module MapperExtensions
4
+ def current_namespaces
5
+ @current_namespaces ||= []
6
+ end
7
+
8
+ def current_models
9
+ @current_models ||= []
10
+ end
11
+
12
+ def namespaces(namespaces, &block)
13
+ if namespaces.any?
14
+ current_namespaces << {namespace_name: namespaces.shift, pending_blocks: []}
15
+ namespace current_namespaces.last[:namespace_name], &block
16
+ current_namespaces.pop[:pending_blocks].each do |block|
17
+ block.call
18
+ end
19
+ else
20
+ yield block
21
+ end
22
+ end
23
+
24
+ def deduplicate_namespaces(namespace_names)
25
+ still_needed = []
26
+
27
+ while namespace_names.any?
28
+ if current_namespaces.last(namespace_names.count).map { |namespace| namespace[:namespace_name] } == namespace_names
29
+ return still_needed
30
+ else
31
+ still_needed.unshift namespace_names.pop
32
+ end
33
+ end
34
+
35
+ still_needed
36
+ end
37
+
38
+ def namespaces_to_eject(namespace_names)
39
+ return [] unless current_models[-2]
40
+
41
+ # E.g. `Common::Projects::Deliverable` and `Common::Objective` should be 1.
42
+ # E.g. `Common::Projects::Deliverable` and `Common::Other::Objective` should be 1.
43
+ working_namespace_names = namespace_names.dup
44
+ parent_namespace_names = current_models[-2][:model_name][0..-2]
45
+
46
+ while parent_namespace_names.any? && working_namespace_names.first == parent_namespace_names.first
47
+ working_namespace_names.shift
48
+ parent_namespace_names.shift
49
+ end
50
+
51
+ parent_namespace_names
52
+ end
53
+
54
+ def model(*parameters, &block)
55
+ namespace_names = parameters[0].underscore.split("/")
56
+ current_models << {model_name: namespace_names.dup, pending_blocks: []}
57
+ model_name = namespace_names.pop
58
+
59
+ original_namespace_names = namespace_names.dup
60
+ namespace_names = deduplicate_namespaces(namespace_names)
61
+
62
+ # E.g. `Project` vs. `Projects::Deliverable`.
63
+ if current_models[-2] && namespace_names.first == current_models[-2][:model_name].last.pluralize
64
+ current_namespaces << {namespace_name: namespace_names.shift, pending_blocks: []}
65
+
66
+ scope module: current_namespaces.last[:namespace_name] do
67
+ namespaces namespace_names do
68
+ # TODO We need people to be able to specify more than just [:index, :new, :create] for collection actions.
69
+ resources model_name.pluralize.to_sym, (parameters[1] || {}).merge(only: [:index, :new, :create]), &block
70
+ end
71
+ end
72
+
73
+ current_namespaces_last = current_namespaces.last[:namespace_name]
74
+
75
+ current_models.last[:pending_blocks] << proc do
76
+ # This needs to be registered to run _after_ we leave the `resources` block of the parent.
77
+ namespace current_namespaces_last.to_sym do
78
+ resources model_name.pluralize.to_sym, (parameters[1] || {}).merge(except: [:index, :new, :create]), &block
79
+ end
80
+ end
81
+
82
+ current_namespaces.pop[:pending_blocks].each do |block|
83
+ block.call
84
+ end
85
+ # E.g. `Projects::Deliverable` and `Objective`
86
+ elsif (ejection_count = namespaces_to_eject(original_namespace_names).count) > 0
87
+ resource_symbol = current_models[-2][:model_name][((ejection_count + 1) * -1)..-1].join("_").to_sym
88
+ resource_path = current_models[-2][:model_name][((ejection_count + 1) * -1)..-1].join("/")
89
+
90
+ current_namespaces[ejection_count * -1][:pending_blocks] << proc do
91
+ resources resource_symbol, path: resource_path do
92
+ namespaces namespace_names do
93
+ resources model_name.pluralize.to_sym, parameters[1] || {}, &block
94
+ end
95
+ end
96
+ end
97
+ else
98
+ namespaces namespace_names do
99
+ resources model_name.pluralize.to_sym, parameters[1] || {}, &block
100
+ end
101
+ end
102
+
103
+ current_models.pop[:pending_blocks].each do |block|
104
+ block.call
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,6 @@
1
+ module BulletTrain
2
+ module Routes
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module BulletTrain
2
+ module Routes
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require "bullet_train/routes/version"
2
+ require "bullet_train/routes/railtie"
3
+ require "bullet_train/routes/mapper_extensions"
4
+
5
+ module BulletTrain
6
+ module Routes
7
+ end
8
+ end
9
+
10
+ ActiveSupport.on_load(:action_controller) do
11
+ ActionDispatch::Routing::Mapper.include(BulletTrain::Routes::MapperExtensions)
12
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bullet_train_routes do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bullet_train-routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Culver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ description: Black magic for defining model-driven routes.
28
+ email:
29
+ - andrew.culver@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/bullet_train/routes.rb
38
+ - lib/bullet_train/routes/mapper_extensions.rb
39
+ - lib/bullet_train/routes/railtie.rb
40
+ - lib/bullet_train/routes/version.rb
41
+ - lib/tasks/bullet_train/routes_tasks.rake
42
+ homepage: https://github.com/bullet-train-co/bullet_train-routes
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ homepage_uri: https://github.com/bullet-train-co/bullet_train-routes
47
+ source_code_uri: https://github.com/bullet-train-co/bullet_train-routes
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.3.7
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Bullet Train Routes
67
+ test_files: []