roda-project 0.1.14 → 0.1.16

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e03b5fbcad7831986b704d75c798fc85ea1874b5e63a8970ef876db89841978f
4
- data.tar.gz: 1cb64c7f6c9b14c8fdcc31e0e841813c5eb92bb707899187053f4af534327136
3
+ metadata.gz: beb637fce10cb01a9d71fbcceca4b10ccc6e04aa05bac094fc6327ed1854643e
4
+ data.tar.gz: 1ac1a13838ba8a5f5e6c65409e192ab9b15fe3f5ad9ee14dbc9d5b648a195434
5
5
  SHA512:
6
- metadata.gz: 9abfe5001b17316fb5ddd00d0f568b96b7b028c96e41a49d6696523d6397965ebd46acd980c3e4d918ff69c5d9aaf6032a155b0fb7736970ed64f78342a6a709
7
- data.tar.gz: ad3464a71e558d327e0492b0c4e56f06bf82c7ae524d8767a99b03a7ca0408aaf9f814cb71a8b17c5e0077b9d06499980fe296f8302aec96bfbcc7ffc19ce362
6
+ metadata.gz: 471336f81ca6fd96226a001e599debeeb22302a284a78e8911a5bf0be02dd78c8010272b061f868d185ed82f3c943f84f6f90e2b35f44c807b2778027a24c019
7
+ data.tar.gz: 4ced8204c150d5208961c2315256151cd0eca3f2bd48905979684490450decc760f8d1b3828ac075fa1539d8e542ff4821cebfe0f70aa4df281efc0cc874e63a
@@ -12,9 +12,9 @@ class Roda
12
12
 
13
13
  puts "* creating routes"
14
14
  generate_routes
15
+ generate_nested_branch_files
15
16
  generate_views
16
17
  generate_tests
17
- print_nested_branch_reminder
18
18
  end
19
19
 
20
20
  private
@@ -37,9 +37,8 @@ class Roda
37
37
  hash_branch_header = if branch_name.include?("/")
38
38
  parts = branch_name.split("/")
39
39
  sub_path = parts[0..-2].join("/")
40
- namespace = sub_path.include?("/") ? ":\"#{sub_path}\"" : ":#{sub_path}"
41
40
  branch_segment = parts.last
42
- "hash_branch #{namespace}, \"#{branch_segment}\" do |r|"
41
+ "hash_branch #{namespace_for(sub_path)}, \"#{branch_segment}\" do |r|"
43
42
  else
44
43
  "hash_branch \"#{branch_name}\" do |r|"
45
44
  end
@@ -85,8 +84,8 @@ class Roda
85
84
  " expect(last_response.status).to eq(200)\n" \
86
85
  " end"
87
86
  else
88
- " it \"responds to #{method.upcase} /#{branch_name} do\n" \
89
- " #{method} \"/#{branch_name}\n" \
87
+ " it \"responds to #{method.upcase} /#{branch_name}\" do\n" \
88
+ " #{method} \"/#{branch_name}\"\n" \
90
89
  " expect(last_response.status).to eq(200)\n" \
91
90
  " end"
92
91
  end
@@ -103,6 +102,57 @@ class Roda
103
102
  action_success_message(test_filename)
104
103
  end
105
104
 
105
+ def generate_nested_branch_files
106
+ return unless branch_name.include?("/")
107
+
108
+ parts = branch_name.split("/")
109
+ parts[0..-2].each_with_index do |_segment, index|
110
+ prefix = parts[0..index].join("/")
111
+ filename = File.join("app/routes", "#{prefix}.rb")
112
+
113
+ if File.exist?(filename)
114
+ ensure_hash_branches_line(filename, namespace_for(prefix))
115
+ next
116
+ end
117
+
118
+ dir = File.dirname(filename)
119
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
120
+ File.write(filename, nested_branch_file_content(prefix, index))
121
+ action_success_message(filename)
122
+ end
123
+ end
124
+
125
+ def nested_branch_file_content(prefix, index)
126
+ parts = prefix.split("/")
127
+ segment = parts.last
128
+ header = if index.zero?
129
+ "hash_branch \"#{segment}\" do |r|"
130
+ else
131
+ parent_path = parts[0..-2].join("/")
132
+ "hash_branch #{namespace_for(parent_path)}, \"#{segment}\" do |r|"
133
+ end
134
+
135
+ <<~RUBY
136
+ class #{@context.const_project_name}
137
+ #{header} # #{prefix} branch
138
+ r.hash_branches(#{namespace_for(prefix)}) # #{prefix}/ +1 routes
139
+ end
140
+ end
141
+ RUBY
142
+ end
143
+
144
+ def ensure_hash_branches_line(filename, namespace)
145
+ expected_line = "r.hash_branches(#{namespace})"
146
+ return if File.read(filename).include?(expected_line)
147
+
148
+ puts " warning: #{filename} already exists — please add:"
149
+ puts " #{expected_line}"
150
+ end
151
+
152
+ def namespace_for(sub_path)
153
+ sub_path.include?("/") ? ":\"#{sub_path}\"" : ":#{sub_path}"
154
+ end
155
+
106
156
  def must_generate_views?
107
157
  @options[:views] && Dir.exist?("app/views")
108
158
  end
@@ -120,20 +170,6 @@ class Roda
120
170
  def branch_name
121
171
  @branch_name ||= @args[0]
122
172
  end
123
-
124
- def print_nested_branch_reminder
125
- return unless branch_name.include?("/")
126
-
127
- parts = branch_name.split("/")
128
- sub_path = parts[0..-2].join("/")
129
- namespace = sub_path.include?("/") ? ":\"#{sub_path}\"" : ":#{sub_path}"
130
-
131
- puts "\ndont forget to add:\n\n" \
132
- "autoload_hash_branch_dir(#{namespace}, \"./app/routes/#{sub_path}\")\n\n" \
133
- "route do |r|\n" \
134
- " r.on(\"#{sub_path}\") { r.hash_branches(#{namespace}) }\n" \
135
- "end\n"
136
- end
137
173
  end
138
174
  end
139
175
  end
@@ -96,12 +96,10 @@ class Roda
96
96
  end
97
97
 
98
98
  def parse_args
99
- @parse_args ||= begin
100
- if %w[module class].include?(@args[0].to_s.downcase)
101
- { type: @args[0].to_s.downcase, name: @args[1].to_s }
102
- else
103
- { type: "class", name: @args[0].to_s }
104
- end
99
+ @parse_args ||= if %w[module class].include?(@args[0].to_s.downcase)
100
+ {type: @args[0].to_s.downcase, name: @args[1].to_s}
101
+ else
102
+ {type: "class", name: @args[0].to_s}
105
103
  end
106
104
  end
107
105
 
@@ -1,8 +1,9 @@
1
1
  class <%= context.const_project_name %> < Roda
2
2
  # Routing<% if context.fullstack? %>
3
- plugin :hash_branch_view_namespace<% end %>
3
+ plugin :hash_branch_view_subdir<% end %>
4
4
  plugin :autoload_hash_branches
5
- autoload_hash_branch_dir("./app/routes")
5
+ HASH_BRANCHES.each { |args| autoload_hash_branch_dir(*args) }
6
+
6
7
  plugin :all_verbs
7
8
  plugin :not_found
8
9
  <% if context.fullstack? %>
@@ -21,8 +21,8 @@ loader.collapse("#{__dir__}/services")
21
21
  # end
22
22
  loader.setup
23
23
 
24
- require "debug" if NOT_PRODUCTION
25
-
26
24
  Oj.mimic_JSON
27
25
 
26
+ require "debug" if NOT_PRODUCTION
27
+
28
28
  require_relative "app"
@@ -23,7 +23,6 @@ gem "puma"
23
23
 
24
24
  # Views
25
25
  gem 'roda-assets_manifest'
26
- gem 'roda-hash_branch_view_namespace'
27
26
  <% end %>
28
27
  # Performance
29
28
  gem "oj"
@@ -22,7 +22,6 @@ loader.collapse("#{__dir__}/app/models")<% end %>
22
22
  # loader.push_dir("#{__dir__}/app/#{path}")
23
23
  # end
24
24
  loader.setup
25
- require "debug" if Config.not_production?
26
25
 
27
26
  Oj.mimic_JSON
28
27
 
@@ -32,10 +31,23 @@ Providers::Mailer.boot
32
31
  Providers::Logger.boot
33
32
 
34
33
  # Consts<% if context.database %>
35
- DB = Providers::DB::Conn.get
34
+ DB = Providers::DB::Conn.get<% end %>
35
+ ROUTES_DIR = "./app/routes"
36
+ base_path = Pathname.new(ROUTES_DIR)
37
+ HASH_BRANCHES = []
38
+ HASH_BRANCHES << [base_path]
39
+ Dir.glob(File.join(ROUTES_DIR, "**/*")).each do |dir_path|
40
+ next unless File.directory?(dir_path)
41
+
42
+ path = Pathname.new(dir_path)
43
+ relative_path = path.relative_path_from(base_path).to_s
44
+
45
+ HASH_BRANCHES << [relative_path.to_sym, path.to_s]
46
+ end
36
47
 
37
- # Automatic models annotations in dev environment
38
48
  if Config.not_production?
49
+ require "debug"<% if context.database %>
50
+ # Automatic models annotations in dev environment
39
51
  require 'sequel/annotate'
40
- Sequel::Annotate.annotate(Dir['app/models/*.rb'], position: :before, border: true) rescue nil
41
- end<% end %>
52
+ Sequel::Annotate.annotate(Dir['app/models/*.rb'], position: :before, border: true) rescue nil<% end %>
53
+ end
@@ -2,13 +2,13 @@ require_relative "../spec_helper"
2
2
 
3
3
  describe "hello" do
4
4
  it "test requests" do
5
- get "/"<% if context.rodauth? %>
6
- _(last_response.status).must_equal 302<% else %>
5
+ get "/"
7
6
  _(last_response.status).must_equal 200<% if context.fullstack? %>
8
- _(last_response.body).must_include "Hello world"<% end %><% end %>
7
+ _(last_response.body).must_include "Hello world"<% end %>
9
8
  end
10
9
  <% if context.rodauth? %>
11
10
  it "test models" do
11
+ # run "RACK_ENV=test bin/roda db migrate" to make this spec pass
12
12
  Account.create(email: "foo@bar.com")
13
13
 
14
14
  _(Account.count).must_equal 1
@@ -0,0 +1,9 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe "Routes for foo" do
4
+ it "responds to GET /foo/bar" do
5
+ get "/foo/bar"<% if context.rodauth? %>
6
+ _(last_response.status).must_equal 302<% else %>
7
+ _(last_response.status).must_equal 200<% end %>
8
+ end
9
+ end
@@ -11,7 +11,7 @@ class Minitest::HooksSpec
11
11
  include Rack::Test::Methods
12
12
 
13
13
  def app
14
- <%= context.const_project_name %>.freeze.app
14
+ <%= context.const_project_name %>.app
15
15
  end
16
16
  end
17
17
 
@@ -2,12 +2,12 @@ require_relative "../spec_helper"
2
2
 
3
3
  describe "hello" do
4
4
  it "test requests" do
5
- get "/"<% if context.rodauth? %>
6
- expect(last_response.status).to eq(302)<% else %>
5
+ get "/"
7
6
  expect(last_response.status).to eq(200)<% if context.fullstack? %>
8
- expect(last_response.body).to include("Hello world")<% end %><% end %>
7
+ expect(last_response.body).to include("Hello world")<% end %>
9
8
  end
10
9
  <% if context.rodauth? %>
10
+ # run "RACK_ENV=test bin/roda db migrate" to make this spec pass
11
11
  it "test models" do
12
12
  Account.create(email: "foo@bar.com")
13
13
 
@@ -0,0 +1,9 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe "Routes for foo" do
4
+ it "responds to GET /foo/bar" do
5
+ get "/foo/bar"<% if context.rodauth? %>
6
+ expect(last_response.status).to eq(302)<% else %>
7
+ expect(last_response.status).to eq(200)<% end %>
8
+ end
9
+ end
@@ -14,6 +14,6 @@ RSpec.configure do |config|
14
14
  end
15
15
  <% end %>
16
16
  def app
17
- <%= context.const_project_name %>.freeze.app
17
+ <%= context.const_project_name %>.app
18
18
  end
19
19
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Roda
4
4
  module Project
5
- VERSION = "0.1.14"
5
+ VERSION = "0.1.16"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-project
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique F. Teixeira
@@ -142,12 +142,12 @@ files:
142
142
  - lib/roda/project/templates/tests/minimal/rspec/spec/app_spec.rb
143
143
  - lib/roda/project/templates/tests/minimal/rspec/spec/spec_helper.rb
144
144
  - lib/roda/project/templates/tests/minitest/spec/app/app_spec.rb.erb
145
- - lib/roda/project/templates/tests/minitest/spec/app/routes/foo_spec.rb
145
+ - lib/roda/project/templates/tests/minitest/spec/app/routes/foo_spec.rb.erb
146
146
  - lib/roda/project/templates/tests/minitest/spec/app/routes/test_branch_spec.rb
147
147
  - lib/roda/project/templates/tests/minitest/spec/app/views/html_spec.rb
148
148
  - lib/roda/project/templates/tests/minitest/spec/spec_helper.rb.erb
149
149
  - lib/roda/project/templates/tests/rspec/spec/app/app_spec.rb.erb
150
- - lib/roda/project/templates/tests/rspec/spec/app/routes/foo_spec.rb
150
+ - lib/roda/project/templates/tests/rspec/spec/app/routes/foo_spec.rb.erb
151
151
  - lib/roda/project/templates/tests/rspec/spec/app/routes/test_branch_spec.rb
152
152
  - lib/roda/project/templates/tests/rspec/spec/app/views/html_spec.rb
153
153
  - lib/roda/project/templates/tests/rspec/spec/spec_helper.rb.erb
@@ -1,8 +0,0 @@
1
- require_relative "../../spec_helper"
2
-
3
- describe "Routes for foo" do
4
- it "responds to GET /foo/bar" do
5
- get "/foo/bar"
6
- _(last_response.status).must_equal 200
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- require_relative "../../spec_helper"
2
-
3
- describe "Routes for foo" do
4
- it "responds to GET /foo/bar" do
5
- get "/foo/bar"
6
- expect(last_response.status).to eq(200)
7
- end
8
- end