orthoses-rails 1.0.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 620d49df1787f2c79e641435da0cf872d0d8604930729bc746a72ec787677b78
4
- data.tar.gz: fc06f6c06c7941f5ca36bbc1aabf6730e641e7cac2ea2a4f7ee1242284ebb4de
3
+ metadata.gz: d9681e80e9a106b8d984c304a0f26777deb5e2077aba6f8a0321f52976e24bcc
4
+ data.tar.gz: c1f248d0e236bc72491e507da185317d55d607cebf4d6364209cd1601cacb6aa
5
5
  SHA512:
6
- metadata.gz: 63e8a69d07faab3bda23d71df987b87352e245b46d14cf824576c2a66502ad95e2523bc8f11eb8030e3325707b7b9b8e2e28103c560dba01bb0234ecf23fb54c
7
- data.tar.gz: 13fd887d6564a51e19a0cdea17b80b5d3cfedd5a56f4096c1ec44a27dca3ba3e6a57c34dd22aa069a808fea34ec2eab1f265f0fa7d42f1394f8c00937642eb03
6
+ metadata.gz: 8ff02244b5844c28186021d4379818a5a0b56ffcb693425f500e3145ff36da721a278c972a6a324e3b3b26bcb3ce9652c98f9fb3c15549fd51924371362d37fb
7
+ data.tar.gz: 284d60f37efb290cf6e5230dd10cda55066278e89e4b81866896793c535b4864edc7cb13c389d0c98a861c34630fba09df3bb04b1035291f7c4cc46666d22b9b
data/README.md CHANGED
@@ -5,9 +5,16 @@ Orthoses::Rails automatically generates RBS for methods added by Rails.
5
5
 
6
6
  ## Usage
7
7
 
8
+ If you have never performed rbs collection initialization, you need to do it.
9
+
10
+ ```
11
+ $ bundle exec rbs collection init
12
+ $ bundle exec rbs collection install
13
+ ```
14
+
8
15
  Build your Rake task for RBS generation.
9
16
 
10
- ```rb
17
+ ```
11
18
  $ bin/rails generate orthoses:rails:install
12
19
  ```
13
20
 
@@ -0,0 +1,31 @@
1
+ namespace :orthoses do
2
+ task :rails do
3
+ # Phase to load libraries
4
+ require Rails.root / "config/application"
5
+ require 'orthoses/rails'
6
+
7
+ # You can choose logger level
8
+ Orthoses.logger.level = :warn
9
+
10
+ # DSL for Orthoses.
11
+ Orthoses::Builder.new do
12
+ use Orthoses::CreateFileByName,
13
+ to: 'sig/orthoses', # Write to this dir. (require)
14
+ depth: 1, # Group files by module name path depth. (default: nil)
15
+ rmtree: true # Remove all `to` dir before generation. (default: false)
16
+
17
+ # Complement missing const name.
18
+ use Orthoses::MissingName
19
+
20
+ # You can load hand written RBS.
21
+ # use Orthoses::LoadRBS,
22
+ # paths: Dir.glob(Rails.root / "sig/hand-written/**/*.rbs")
23
+
24
+ # Middleware package for rails application.
25
+ use Orthoses::Rails::Application
26
+
27
+ # Application code loaded here is the target of the analysis.
28
+ run Orthoses::Rails::Application::Loader.new
29
+ end.call
30
+ end
31
+ end
@@ -18,15 +18,7 @@ module Orthoses
18
18
  type = if ref.polymorphic?
19
19
  'untyped'
20
20
  else
21
- begin
22
- Utils.module_name(ref.klass) or next
23
- rescue NameError => e
24
- while e
25
- Orthoses.logger.warn(e.message)
26
- e = e.cause
27
- end
28
- next
29
- end
21
+ Orthoses::ActiveRecord.reflection_klass_name(ref) or next
30
22
  end
31
23
  opt = "#{type}?"
32
24
 
@@ -15,16 +15,7 @@ module Orthoses
15
15
 
16
16
  lines = base.reflect_on_all_associations(:has_many).flat_map do |ref|
17
17
  singular_name = ref.name.to_s.singularize
18
- type =
19
- begin
20
- Utils.module_name(ref.klass)
21
- rescue NameError => e
22
- while e
23
- Orthoses.logger.warn(e.message)
24
- e = e.cause
25
- end
26
- next
27
- end
18
+ type = Orthoses::ActiveRecord.reflection_klass_name(ref) or next
28
19
 
29
20
  collection_proxy = "::#{type}::ActiveRecord_Associations_CollectionProxy"
30
21
  [
@@ -14,16 +14,7 @@ module Orthoses
14
14
  base_name = Utils.module_name(base) || next
15
15
 
16
16
  lines = base.reflect_on_all_associations(:has_one).flat_map do |ref|
17
- type =
18
- begin
19
- Utils.module_name(ref.klass) or next
20
- rescue NameError => e
21
- while e
22
- Orthoses.logger.warn(e.message)
23
- e = e.cause
24
- end
25
- next
26
- end
17
+ type = Orthoses::ActiveRecord.reflection_klass_name(ref) or next
27
18
  opt = "#{type}?"
28
19
 
29
20
  [
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveRecord
5
+ # def has_secure_token(attribute = :token, length: MINIMUM_TOKEN_LENGTH)
6
+ class SecureToken
7
+ def initialize(loader)
8
+ @loader = loader
9
+ end
10
+
11
+ def call
12
+ has_secure_token = CallTracer::Lazy.new
13
+ store = has_secure_token.trace('ActiveRecord::SecureToken::ClassMethods#has_secure_token') do
14
+ @loader.call
15
+ end
16
+
17
+ has_secure_token.captures.each do |capture|
18
+ base_name = Utils.module_name(capture.method.receiver) or next
19
+ attribute = capture.argument[:attribute]
20
+
21
+ store[base_name] << "def regenerate_#{attribute}: () -> bool"
22
+ end
23
+
24
+ store
25
+ end
26
+ end
27
+ end
28
+ end
@@ -9,6 +9,7 @@ require_relative 'active_record/has_one'
9
9
  require_relative 'active_record/query_methods'
10
10
  require_relative 'active_record/relation'
11
11
  require_relative 'active_record/scope'
12
+ require_relative 'active_record/secure_token'
12
13
 
13
14
  module Orthoses
14
15
  module ActiveRecord
@@ -42,5 +43,16 @@ module Orthoses
42
43
  'untyped'
43
44
  end
44
45
  end
46
+
47
+ def self.reflection_klass_name(ref)
48
+ Utils.module_name(ref.klass)
49
+ rescue NameError, ArgumentError => e
50
+ while e
51
+ Orthoses.logger.warn(e.message)
52
+ e = e.cause
53
+ end
54
+
55
+ nil
56
+ end
45
57
  end
46
58
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveStorage
5
+ module Attached
6
+ # <= 6.0
7
+ # def has_one_attached(name, dependent: :purge_later)
8
+ # >= 6.1
9
+ # def has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
10
+ class Model
11
+ def initialize(loader)
12
+ @loader = loader
13
+ end
14
+
15
+ def call
16
+ store = @loader.call
17
+
18
+ ::ActiveRecord::Base.descendants.each do |base|
19
+ next if base.abstract_class?
20
+ next if base.reflect_on_all_attachments.empty?
21
+
22
+ base_name = Utils.module_name(base) or next
23
+ base.reflect_on_all_attachments.each do |reflection|
24
+ type =
25
+ case reflection
26
+ when ::ActiveStorage::Reflection::HasOneAttachedReflection
27
+ "ActiveStorage::Attached::One"
28
+ when ::ActiveStorage::Reflection::HasManyAttachedReflection
29
+ "ActiveStorage::Attached::Many"
30
+ else
31
+ "untyped"
32
+ end
33
+ name = reflection.name
34
+
35
+ store[base_name].tap do |content|
36
+ content << "def #{name}: () -> #{type}"
37
+ content << "def #{name}=: (untyped attachable) -> untyped"
38
+ end
39
+ end
40
+ end
41
+
42
+ store
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'active_storage/attached/model'
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveSupport
5
+ # def alias_attribute(new_name, old_name)
6
+ class Aliasing
7
+ def initialize(loader)
8
+ @loader = loader
9
+ end
10
+
11
+ def call
12
+ alias_attribute = CallTracer::Lazy.new
13
+
14
+ store =
15
+ alias_attribute.trace('Module#alias_attribute') do
16
+ @loader.call
17
+ end
18
+
19
+ alias_attribute.captures.each do |capture|
20
+ base_mod_name = Utils.module_name(capture.method.receiver) or next
21
+ new_name = capture.argument[:new_name]
22
+
23
+ content = store[base_mod_name]
24
+ # TODO: Shold use alias? But, it has risc of undefined method
25
+ content << "def #{new_name}: () -> untyped"
26
+ content << "def #{new_name}?: () -> bool"
27
+ content << "def #{new_name}=: (untyped) -> untyped"
28
+ end
29
+
30
+ store
31
+ end
32
+ end
33
+ end
34
+ end
@@ -28,6 +28,13 @@ module Orthoses
28
28
  -
29
29
  ])
30
30
 
31
+ TIME_MODULES = [
32
+ TypeName("::Time"),
33
+ TypeName("::DateAndTime::Zones"),
34
+ TypeName("::DateAndTime::Calculations"),
35
+ TypeName("::DateAndTime::Compatibility")
36
+ ]
37
+
31
38
  def filter_decl(time_with_zone_store)
32
39
  writer = RBS::Writer.new(out: StringIO.new)
33
40
  time_with_zone_store.to_decl.members.each do |member|
@@ -64,8 +71,9 @@ module Orthoses
64
71
  next if twz_methods.has_key?(sym) && !TYPE_MERGE_METHODS.include?(sym)
65
72
  next if !definition_method.public?
66
73
 
67
- definition_method.defs.reject! do |type_def|
68
- type_def.implemented_in != type_name_time
74
+ # delegate to ::Time method
75
+ definition_method.defs.select! do |type_def|
76
+ TIME_MODULES.include?(type_def.implemented_in)
69
77
  end
70
78
  next if definition_method.defs.empty?
71
79
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'active_support/aliasing'
3
4
  require_relative 'active_support/class_attribute'
4
5
  require_relative 'active_support/concern'
5
6
  require_relative 'active_support/configurable'
@@ -3,6 +3,13 @@
3
3
  module Orthoses
4
4
  module Rails
5
5
  class Application
6
+ class Loader
7
+ def call
8
+ ::Rails.application.initialize!
9
+ ::Rails.application.eager_load!
10
+ end
11
+ end
12
+
6
13
  def initialize(loader)
7
14
  @loader = loader
8
15
  end
@@ -11,6 +18,7 @@ module Orthoses
11
18
  loader = @loader
12
19
  Orthoses::Builder.new do
13
20
  use Orthoses::ActiveModel::HasSecurePassword
21
+
14
22
  use Orthoses::ActiveRecord::BelongsTo
15
23
  use Orthoses::ActiveRecord::DelegatedType
16
24
  use Orthoses::ActiveRecord::Enum
@@ -19,7 +27,13 @@ module Orthoses
19
27
  use Orthoses::ActiveRecord::HasOne
20
28
  use Orthoses::ActiveRecord::Relation
21
29
  use Orthoses::ActiveRecord::Scope
30
+ use Orthoses::ActiveRecord::SecureToken
31
+
32
+ if defined?(::ActiveStorage)
33
+ use Orthoses::ActiveStorage::Attached::Model
34
+ end
22
35
 
36
+ use Orthoses::ActiveSupport::Aliasing
23
37
  use Orthoses::ActiveSupport::ClassAttribute
24
38
  use Orthoses::ActiveSupport::Concern
25
39
  use Orthoses::ActiveSupport::Delegation
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Orthoses
4
4
  module Rails
5
- VERSION = "1.0.0"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  end
@@ -4,6 +4,7 @@ require 'orthoses'
4
4
 
5
5
  require_relative 'active_model'
6
6
  require_relative 'active_record'
7
+ require_relative 'active_storage'
7
8
  require_relative 'active_support'
8
9
  require_relative 'railties'
9
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orthoses-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-27 00:00:00.000000000 Z
11
+ date: 2023-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orthoses
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.8'
19
+ version: '1.11'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.8'
26
+ version: '1.11'
27
27
  description: Orthoses middleware collection for Ruby on Rails
28
28
  email:
29
29
  - co000ri@gmail.com
@@ -35,6 +35,7 @@ files:
35
35
  - LICENSE.txt
36
36
  - README.md
37
37
  - lib/generators/orthoses/rails/install_generator.rb
38
+ - lib/generators/orthoses/rails/templates/rails.rake
38
39
  - lib/orthoses-rails.rb
39
40
  - lib/orthoses/active_model.rb
40
41
  - lib/orthoses/active_model/has_secure_password.rb
@@ -48,7 +49,11 @@ files:
48
49
  - lib/orthoses/active_record/query_methods.rb
49
50
  - lib/orthoses/active_record/relation.rb
50
51
  - lib/orthoses/active_record/scope.rb
52
+ - lib/orthoses/active_record/secure_token.rb
53
+ - lib/orthoses/active_storage.rb
54
+ - lib/orthoses/active_storage/attached/model.rb
51
55
  - lib/orthoses/active_support.rb
56
+ - lib/orthoses/active_support/aliasing.rb
52
57
  - lib/orthoses/active_support/class_attribute.rb
53
58
  - lib/orthoses/active_support/concern.rb
54
59
  - lib/orthoses/active_support/configurable.rb