camille 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5911a8f4d1519a2a373ebee118bce4f110d64f5eaeafece5c8c8e46580b5c20
4
- data.tar.gz: b59a4600bd145350d1217219b0074ffb6e80d8cdc97318995b6ba2b0bffe7450
3
+ metadata.gz: 5779c76d0ca3c695e1e15d0870d94be5739449f7ad1b9422ab9944d139f3a6d1
4
+ data.tar.gz: 855906b19324060e4fb8578451e1fd77f692918f6722a81b9f3fbdc5cbf48fd3
5
5
  SHA512:
6
- metadata.gz: 3088068a948ca92e1a6bc90690c5f27d5e2c788c3f76e2ba40b5ebf62b7c3fdffac809411941a3f05a77ace636ce16bf7742c12c1ae5700c4b45d7bfb9fdddfb
7
- data.tar.gz: c6f0a0b72216805bf591bc4289172e69382f702ccc14d161a3b63267dc83816a47045e9d61884029349543aac5ecf5aeb43bbc82f0c95ef23ff7df569037b8b6
6
+ metadata.gz: e555aebf740ecd06dcf96928a3aeafe414430a7d439a83679da90d2e60e23a97cd28bc583a473ddab76c60275e31668b76f9220ca2278c21ecceb8f2f11743fa
7
+ data.tar.gz: 00bf823aa9f394c1198cd492f42cd716b8bd59c08fe7e704df64aa92cdb927198ad87761f867de9b59153cba96490190292296755ef1642bd9c759820b00ecc0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.1.0)
4
+ camille (0.3.0)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
@@ -0,0 +1,23 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'install camille'
8
+
9
+ def copy_configuration_file
10
+ copy_file "configuration.rb", "config/camille/configuration.rb"
11
+ end
12
+
13
+ def copy_type_example
14
+ copy_file "type_example.rb", "config/camille/types/example.rb"
15
+ end
16
+
17
+ def copy_schema_example
18
+ copy_file "schema_example.rb", "config/camille/schemas/examples.rb"
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class SchemaGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'generate camille schema'
8
+
9
+ def generate_type_file
10
+ template 'schema_template.erb', "config/camille/schemas/#{@name}.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ Camille.configure do |config|
2
+ # Contents to be placed at the beginning of the output TypeScript file.
3
+ config.ts_header = <<~EOF
4
+ import request from './request'
5
+ EOF
6
+ end
@@ -0,0 +1,35 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Schemas::Examples < Camille::Schema
4
+ include Camille::Types
5
+
6
+ get :find do
7
+ params(
8
+ id: Number
9
+ )
10
+
11
+ response(
12
+ example?: Example
13
+ )
14
+ end
15
+
16
+ get :list do
17
+ response(
18
+ examples: Example[]
19
+ )
20
+ end
21
+
22
+ post :update do
23
+ params(
24
+ id: Number,
25
+ example: Example
26
+ )
27
+
28
+ response(
29
+ success: Boolean,
30
+ errors: {
31
+ message: String
32
+ }[]
33
+ )
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Schemas::<%= class_name %> < Camille::Schema
4
+ include Camille::Types
5
+
6
+ # get :data do
7
+ # params(
8
+ # id: Number
9
+ # )
10
+ # response(
11
+ # name: String
12
+ # )
13
+ # end
14
+ end
@@ -0,0 +1,10 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Types::Example < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(
7
+ id: Number,
8
+ name: String,
9
+ )
10
+ end
@@ -0,0 +1,9 @@
1
+ using Camille::CoreExt
2
+
3
+ class Camille::Types::<%= class_name %> < Camille::Type
4
+ include Camille::Types
5
+
6
+ alias_of(
7
+ # id: Number
8
+ )
9
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Camille
3
+ module Generators
4
+ class TypeGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ desc 'generate camille type'
8
+
9
+ def generate_type_file
10
+ template 'type_template.erb', "config/camille/types/#{@name}.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -10,8 +10,25 @@ module Camille
10
10
  synchronize do
11
11
  loader = Zeitwerk::Loader.new
12
12
  loader.enable_reloading if !app.config.cache_classes
13
- loader.push_dir "#{app.root}/config/camille/types", namespace: Camille::Types
14
- loader.push_dir "#{app.root}/config/camille/schemas", namespace: Camille::Schemas
13
+
14
+ types_dir = "#{app.root}/config/camille/types"
15
+ schemas_dir = "#{app.root}/config/camille/schemas"
16
+
17
+ if Dir.exist? types_dir
18
+ loader.push_dir types_dir, namespace: Camille::Types
19
+ else
20
+ unless inside_generator?
21
+ puts "[Camille Warning] Expected folder `config/camille/types`. Run `rails g camille:install` to fix it."
22
+ end
23
+ end
24
+
25
+ if Dir.exist? schemas_dir
26
+ loader.push_dir schemas_dir, namespace: Camille::Schemas
27
+ else
28
+ unless inside_generator?
29
+ puts "[Camille Warning] Expected folder `config/camille/schemas`. Run `rails g camille:install` to fix it."
30
+ end
31
+ end
15
32
 
16
33
  loader.setup
17
34
  @zeitwerk_loader = loader
@@ -22,17 +39,6 @@ module Camille
22
39
  end
23
40
  end
24
41
 
25
- def eager_load
26
- @eager_loading = true
27
- load @configuration_location
28
- @zeitwerk_loader.eager_load
29
- @eager_loading = false
30
- end
31
-
32
- def eager_loading?
33
- @eager_loading
34
- end
35
-
36
42
  def reload_types_and_schemas
37
43
  synchronize do
38
44
  Camille::Loader.loaded_types.clear
@@ -69,22 +75,32 @@ module Camille
69
75
  end
70
76
  end
71
77
 
72
- def construct_controller_name_to_schema_map
73
- synchronize do
74
- controller_name_to_schema_map.clear
75
- loaded_schemas.each do |schema|
76
- controller_class_name = "#{schema.klass_name}Controller"
77
- controller_name_to_schema_map[controller_class_name] = schema
78
+ private
79
+ def eager_load
80
+ if File.exist?(@configuration_location)
81
+ load @configuration_location
82
+ else
83
+ unless inside_generator?
84
+ puts "[Camille Warning] Expected file `config/camille/configuration.rb`. Run `rails g camille:install` to fix it."
85
+ end
78
86
  end
87
+ @zeitwerk_loader.eager_load
79
88
  end
80
- end
81
89
 
82
- def reload
83
- synchronize do
84
- reload_types_and_schemas
85
- Rails.application.reload_routes!
90
+ def construct_controller_name_to_schema_map
91
+ synchronize do
92
+ controller_name_to_schema_map.clear
93
+ loaded_schemas.each do |schema|
94
+ controller_class_name = "#{schema.klass_name}Controller"
95
+ controller_name_to_schema_map[controller_class_name] = schema
96
+ end
97
+ end
98
+ end
99
+
100
+ def inside_generator?
101
+ # https://stackoverflow.com/a/53963584
102
+ !(Rails.const_defined?(:Server) || Rails.const_defined?(:Console))
86
103
  end
87
- end
88
104
 
89
105
  end
90
106
 
@@ -3,11 +3,7 @@ require 'action_controller'
3
3
  module Camille
4
4
  class MainController < ActionController::Base
5
5
  def endpoints_ts
6
- if Rails.env.development?
7
- render plain: Camille::CodeGenerator.generate_ts
8
- else
9
- head 404
10
- end
6
+ render plain: Camille::CodeGenerator.generate_ts
11
7
  end
12
8
  end
13
9
  end
@@ -10,14 +10,17 @@ module Camille
10
10
  Camille::Loader.setup_zeitwerk_loader(app)
11
11
 
12
12
  app.routes.prepend do
13
- get '/camille/endpoints.ts' => 'camille/main#endpoints_ts'
13
+ if Rails.env.development?
14
+ get '/camille/endpoints.ts' => 'camille/main#endpoints_ts'
15
+ end
16
+
14
17
  Camille::Loader.register_routes(self)
15
18
  end
16
19
 
17
20
  dir = "#{Rails.root}/config/camille"
18
21
 
19
22
  update_checker = ActiveSupport::FileUpdateChecker.new([], {dir => ['rb']}) do
20
- Camille::Loader.reload
23
+ Camille::Loader.reload_types_and_schemas
21
24
  end
22
25
 
23
26
  app.reloaders << update_checker
@@ -2,20 +2,10 @@ module Camille
2
2
  class Schema
3
3
  class AlreadyDefinedError < ::ArgumentError; end
4
4
 
5
- include Camille::Types
6
-
7
5
  def self.endpoints
8
6
  @endpoints ||= {}
9
7
  end
10
8
 
11
- def self.const_missing name
12
- if Camille::Loader.eager_loading?
13
- Camille::Types.const_get(name)
14
- else
15
- super
16
- end
17
- end
18
-
19
9
  def self.path
20
10
  "/#{ActiveSupport::Inflector.underscore klass_name}"
21
11
  end
data/lib/camille/type.rb CHANGED
@@ -2,7 +2,6 @@ module Camille
2
2
  # This class represents all custom types defined by the user.
3
3
  class Type < BasicType
4
4
  class NotImplementedError < ::NotImplementedError; end
5
- include Camille::Types
6
5
 
7
6
  attr_reader :underlying
8
7
 
@@ -30,14 +29,6 @@ module Camille
30
29
  self.class.klass_name.gsub(/::/, '_')
31
30
  end
32
31
 
33
- def self.const_missing name
34
- if Camille::Loader.eager_loading?
35
- Camille::Types.const_get(name)
36
- else
37
- super
38
- end
39
- end
40
-
41
32
  def self.inherited klass
42
33
  Camille::Loader.loaded_types << klass
43
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/camille.rb CHANGED
@@ -30,6 +30,11 @@ require_relative "camille/configuration"
30
30
  require_relative "camille/code_generator"
31
31
  require_relative "camille/main_controller"
32
32
 
33
+ require "rails/generators"
34
+ require_relative "camille/generators/install_generator"
35
+ require_relative "camille/generators/type_generator"
36
+ require_relative "camille/generators/schema_generator"
37
+
33
38
  module Camille
34
39
  class Error < StandardError; end
35
40
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 辻彩
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-14 00:00:00.000000000 Z
11
+ date: 2023-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -51,6 +51,14 @@ files:
51
51
  - lib/camille/controller_extension.rb
52
52
  - lib/camille/core_ext.rb
53
53
  - lib/camille/endpoint.rb
54
+ - lib/camille/generators/install_generator.rb
55
+ - lib/camille/generators/schema_generator.rb
56
+ - lib/camille/generators/templates/configuration.rb
57
+ - lib/camille/generators/templates/schema_example.rb
58
+ - lib/camille/generators/templates/schema_template.erb
59
+ - lib/camille/generators/templates/type_example.rb
60
+ - lib/camille/generators/templates/type_template.erb
61
+ - lib/camille/generators/type_generator.rb
54
62
  - lib/camille/line.rb
55
63
  - lib/camille/loader.rb
56
64
  - lib/camille/main_controller.rb