bullet_train-super_scaffolding 1.0.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: ff4575c2ac8fe4254a40902558258c4b6987ced04ee50bc7506a829c43c0150a
4
+ data.tar.gz: b64e648b62b67455633ea076b597c1cf9689d8a044755f1c28114622ea48dbe3
5
+ SHA512:
6
+ metadata.gz: c9ca623c19afc3d40a8d235f976c47c4d84eb91e230430c88c0f020109a77dee26f1a82f8b81932a79472947d42c3bec24e14c049227c0253e89c76557a1f9cb
7
+ data.tar.gz: 6aa2b5a0b6792eb701fb81926280eda270787773a11c053e433edb1a58e61c4f4c7eb7a1c0d474b0766832c5679607cbcc10e5dc6f4f32f852bdcc54a0ba17b6
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::SuperScaffolding
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-super_scaffolding"
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-super_scaffolding
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,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,6 @@
1
+ module BulletTrain
2
+ module SuperScaffolding
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module BulletTrain
2
+ module SuperScaffolding
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "bullet_train/super_scaffolding/version"
2
+ require "bullet_train/super_scaffolding/engine"
3
+
4
+ module BulletTrain
5
+ module SuperScaffolding
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,190 @@
1
+ class Scaffolding::ClassNamesTransformer
2
+ attr_accessor :child, :parent, :namespace
3
+
4
+ def initialize(child, parent, namespace = "account")
5
+ self.child = child
6
+ self.parent = parent
7
+ self.namespace = namespace
8
+ end
9
+
10
+ def belongs_to_needs_class_definition?
11
+ return false if parent_namespace_parts.empty?
12
+ return false if parent_namespace_parts == namespace_parts
13
+ namespace_parts.first(parent_namespace_parts.count) != parent_namespace_parts
14
+ end
15
+
16
+ def namespace_parts
17
+ parts[0..-2]
18
+ end
19
+
20
+ def parent_namespace_parts
21
+ parent_parts[0..-2]
22
+ end
23
+
24
+ def parts
25
+ child.split("::")
26
+ end
27
+
28
+ def parent_parts
29
+ parent.empty? ? [""] : parent.split("::")
30
+ end
31
+
32
+ def parent_in_namespace_class_name
33
+ parent_parts.last
34
+ end
35
+
36
+ def in_namespace_class_name
37
+ parts.last
38
+ end
39
+
40
+ def all_parts_in_context
41
+ working_parts = parts
42
+ working_parent_parts = parent_parts
43
+
44
+ # e.g. Webhooks::Incoming::Event vs. Webhooks::Incoming::Delivery
45
+ while working_parts.first == working_parent_parts.first
46
+ # get rid of 'Webhooks::' and 'Incoming::'
47
+ working_parts.shift
48
+ working_parent_parts.shift
49
+ end
50
+
51
+ # e.g. Conversation vs. Conversations::Subscription
52
+ while working_parts.first == working_parent_parts.first.pluralize
53
+ # get rid of 'Conversations::'
54
+ working_parts.shift
55
+ end
56
+
57
+ [working_parts, working_parent_parts]
58
+ end
59
+
60
+ def parts_in_context
61
+ all_parts_in_context.first
62
+ end
63
+
64
+ def parent_parts_in_context
65
+ all_parts_in_context.last
66
+ end
67
+
68
+ def class_name_in_parent_context
69
+ parts_in_context.join("::")
70
+ end
71
+
72
+ def parent_class_name_in_context
73
+ parent_parts_in_context.join("::")
74
+ end
75
+
76
+ def parent_variable_name_in_context
77
+ parent_parts_in_context.join("::").underscore.tr("/", "_")
78
+ end
79
+
80
+ def parent_table_name
81
+ parent.underscore.tr("/", "_").pluralize
82
+ end
83
+
84
+ def table_name
85
+ child.underscore.tr("/", "_").pluralize
86
+ end
87
+
88
+ def replacement_for(string)
89
+ case string
90
+
91
+ when "Scaffolding::AbsolutelyAbstract::CreativeConcepts"
92
+ parent.pluralize
93
+ when "Scaffolding::CompletelyConcrete::TangibleThings"
94
+ child.pluralize
95
+ when "scaffolding/absolutely_abstract/creative_concepts"
96
+ parent.underscore.pluralize
97
+ when "scaffolding/completely_concrete/tangible_things"
98
+ child.underscore.pluralize
99
+ when "scaffolding/completely_concrete/_tangible_things"
100
+ parts = child.underscore.split("/")
101
+ parts.push "_#{parts.pop}"
102
+ parts.join("/").pluralize
103
+ when "scaffolding_absolutely_abstract_creative_concepts"
104
+ parent.underscore.tr("/", "_").pluralize
105
+ when "scaffolding_completely_concrete_tangible_things"
106
+ child.underscore.tr("/", "_").pluralize
107
+ when "scaffolding-absolutely-abstract-creative-concepts"
108
+ parent.underscore.gsub(/[\/_]/, "-").pluralize
109
+ when "scaffolding-completely-concrete-tangible-things"
110
+ child.underscore.gsub(/[\/_]/, "-").pluralize
111
+
112
+ when "Scaffolding::AbsolutelyAbstract::CreativeConcept"
113
+ parent
114
+ when "Scaffolding::CompletelyConcrete::TangibleThing"
115
+ child
116
+ when "scaffolding/absolutely_abstract/creative_concept"
117
+ parent.underscore
118
+ when "scaffolding/completely_concrete/tangible_thing"
119
+ child.underscore
120
+ when "scaffolding_absolutely_abstract_creative_concept"
121
+ parent.underscore.tr("/", "_")
122
+ when "scaffolding_completely_concrete_tangible_thing"
123
+ child.underscore.tr("/", "_")
124
+ when "scaffolding-absolutely-abstract-creative-concept"
125
+ parent.underscore.gsub(/[\/_]/, "-")
126
+ when "scaffolding-completely-concrete-tangible-thing"
127
+ child.underscore.gsub(/[\/_]/, "-")
128
+
129
+ when "absolutely_abstract_creative_concepts"
130
+ parent_class_name_in_context.underscore.tr("/", "_").pluralize
131
+ when "completely_concrete_tangible_things"
132
+ class_name_in_parent_context.underscore.tr("/", "_").pluralize
133
+ when "absolutely_abstract/creative_concepts"
134
+ parent_class_name_in_context.underscore.pluralize
135
+ when "completely_concrete/tangible_things"
136
+ class_name_in_parent_context.underscore.pluralize
137
+ when "absolutely-abstract-creative-concepts"
138
+ parent.underscore.gsub(/[\/_]/, "-").pluralize
139
+ when "completely-concrete-tangible-things"
140
+ child.underscore.gsub(/[\/_]/, "-").pluralize
141
+
142
+ when "absolutely_abstract_creative_concept"
143
+ parent_class_name_in_context.underscore.tr("/", "_")
144
+ when "completely_concrete_tangible_thing"
145
+ class_name_in_parent_context.underscore.tr("/", "_")
146
+ when "absolutely_abstract/creative_concept"
147
+ parent_class_name_in_context.underscore
148
+ when "completely_concrete/tangible_thing"
149
+ class_name_in_parent_context.underscore
150
+ when "absolutely-abstract-creative-concept"
151
+ parent.underscore.gsub(/[\/_]/, "-")
152
+ when "completely-concrete-tangible-thing"
153
+ child.underscore.gsub(/[\/_]/, "-")
154
+
155
+ when "creative_concepts"
156
+ parent_in_namespace_class_name.underscore.pluralize
157
+ when "tangible_things"
158
+ in_namespace_class_name.underscore.pluralize
159
+ when "Creative Concepts"
160
+ parent_in_namespace_class_name.titlecase.pluralize
161
+ when "Tangible Things"
162
+ in_namespace_class_name.titlecase.pluralize
163
+ when "creative-concepts"
164
+ parent_in_namespace_class_name.underscore.gsub(/[\/_]/, "-").pluralize
165
+ when "tangible-things"
166
+ in_namespace_class_name.underscore.gsub(/[\/_]/, "-").pluralize
167
+
168
+ when "creative_concept"
169
+ parent_in_namespace_class_name.underscore
170
+ when "tangible_thing"
171
+ in_namespace_class_name.underscore
172
+ when "Creative Concept"
173
+ parent_in_namespace_class_name.titlecase
174
+ when "Tangible Thing"
175
+ in_namespace_class_name.titlecase
176
+ when "creative-concept"
177
+ parent_in_namespace_class_name.underscore.gsub(/[\/_]/, "-")
178
+ when "tangible-thing"
179
+ in_namespace_class_name.underscore.gsub(/[\/_]/, "-")
180
+
181
+ when ":account"
182
+ ":#{namespace}"
183
+ when "/account/"
184
+ "/#{namespace}/"
185
+
186
+ else
187
+ "🛑"
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,107 @@
1
+ def encode_double_replacement_fix(string)
2
+ string.chars.join("~!@BT@!~")
3
+ end
4
+
5
+ def decode_double_replacement_fix(string)
6
+ string.gsub("~!@BT@!~", "")
7
+ end
8
+
9
+ def oauth_scaffold_directory(directory, options)
10
+ begin
11
+ Dir.mkdir(oauth_transform_string(directory, options))
12
+ rescue
13
+ nil
14
+ end
15
+ Dir.foreach(directory) do |file|
16
+ file = "#{directory}/#{file}"
17
+ unless File.directory?(file)
18
+ oauth_scaffold_file(file, options)
19
+ end
20
+ end
21
+ end
22
+
23
+ # there is a bunch of stuff duplicate here, but i'm OK with that for now.
24
+ def oauth_scaffold_file(file, options)
25
+ transformed_file_name = oauth_transform_string(file, options)
26
+ transformed_file_content = []
27
+
28
+ skipping = false
29
+ File.open(file).each_line do |line|
30
+ if line.include?("# 🚅 skip when scaffolding.")
31
+ next
32
+ end
33
+
34
+ if line.include?("# 🚅 skip this section when scaffolding.")
35
+ skipping = true
36
+ next
37
+ end
38
+
39
+ if line.include?("# 🚅 stop any skipping we're doing now.")
40
+ skipping = false
41
+ next
42
+ end
43
+
44
+ if skipping
45
+ next
46
+ end
47
+
48
+ # remove lines with 'remove in scaffolded files.'
49
+ unless line.include?("remove in scaffolded files.")
50
+
51
+ # only transform it if it doesn't have the lock emoji.
52
+ if line.include?("🔒")
53
+ # remove any comments that start with a lock.
54
+ line.gsub!(/\s+?#\s+🔒.*/, "")
55
+ else
56
+ line = oauth_transform_string(line, options)
57
+ end
58
+
59
+ transformed_file_content << line
60
+
61
+ end
62
+ end
63
+ transformed_file_content = transformed_file_content.join
64
+
65
+ transformed_directory_name = File.dirname(transformed_file_name)
66
+ unless File.directory?(transformed_directory_name)
67
+ FileUtils.mkdir_p(transformed_directory_name)
68
+ end
69
+
70
+ puts "Writing '#{transformed_file_name}'."
71
+
72
+ File.write(transformed_file_name, transformed_file_content)
73
+ end
74
+
75
+ def oauth_transform_string(string, options)
76
+ name = options[:our_provider_name]
77
+
78
+ # get these out of the way first.
79
+ string = string.gsub("stripe_connect: Stripe", encode_double_replacement_fix(options[:gems_provider_name] + ": " + name.titleize))
80
+ string = string.gsub("ti-money", encode_double_replacement_fix(options[:icon])) if options[:icon]
81
+ string = string.gsub("omniauth-stripe-connect", encode_double_replacement_fix(options[:omniauth_gem]))
82
+ string = string.gsub("stripe_connect", encode_double_replacement_fix(options[:gems_provider_name]))
83
+ string = string.gsub("STRIPE_CLIENT_ID", encode_double_replacement_fix(options[:api_key]))
84
+ string = string.gsub("STRIPE_SECRET_KEY", encode_double_replacement_fix(options[:api_secret]))
85
+
86
+ # then try for some matches that give us a little more context on what they're looking for.
87
+ string = string.gsub("stripe-account", encode_double_replacement_fix(name.underscore.dasherize + "_account"))
88
+ string = string.gsub("stripe_account", encode_double_replacement_fix(name.underscore + "_account"))
89
+ string = string.gsub("StripeAccount", encode_double_replacement_fix(name + "Account"))
90
+ string = string.gsub("Stripe Account", encode_double_replacement_fix(name.titleize + " Account"))
91
+ string = string.gsub("Stripe account", encode_double_replacement_fix(name.titleize + " account"))
92
+ string = string.gsub("with Stripe", encode_double_replacement_fix("with " + name.titleize))
93
+
94
+ # finally, just do the simplest string replace. it's possible this can produce weird results.
95
+ # if they do, try adding more context aware replacements above, e.g. what i did with 'with'.
96
+ string = string.gsub("stripe", encode_double_replacement_fix(name.underscore))
97
+ string = string.gsub("Stripe", encode_double_replacement_fix(name))
98
+
99
+ decode_double_replacement_fix(string)
100
+ end
101
+
102
+ def oauth_scaffold_add_line_to_file(file, content, after, options, additional_options = {})
103
+ file = oauth_transform_string(file, options)
104
+ content = oauth_transform_string(content, options)
105
+ after = oauth_transform_string(after, options)
106
+ legacy_add_line_to_file(file, content, after, nil, nil, additional_options)
107
+ end