orthoses-rails 1.3.0 → 1.4.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: dc7b416eba2452375317a5c028aeae285d135ee8c8ef4129f28170f46d0090d9
4
- data.tar.gz: 71148b9456d3038c6960611ac78e764590f8becd9376e1d136fb06549e80a378
3
+ metadata.gz: a9a2d180cb8faa295dc3043ea66a379ee71939436075104c0f85b63d6aede665
4
+ data.tar.gz: e02fa19ea12dfd296936673d67ecc6457b0e16101c18d4851747e0b895dace96
5
5
  SHA512:
6
- metadata.gz: 6abe5dd4e76632c6039a20da11e44e63851713a4793e31661e246ce00d1de36ced9c0ecfe182efe2108df2e59c54e00cadf4d857eb1e7c447dd36d289de068b5
7
- data.tar.gz: 6e1df2008c803ffa594d2fc96312ef9d97bda0570339604a40b47e8ff35c94118dd7ed2ca1cf77f7316cf3464ba7cbdc9ec438d182a4099983c0625ece69024c
6
+ metadata.gz: 9e97ed8964e301618db94df7c0d410310ea3bd9605f66c9d7d0431dc71546e0faaff24f918ce05a58510250f7563934a9adb3977662d3526db0198016801ba77
7
+ data.tar.gz: 5d43746f2bcb1ae068902a66a0da54a852bb173fcdd85ff5d9ce0f07e2051cbbc31e7b1a2c8e0469b28e2ca6ca1661edb76a1665e6d7a3356c6bedb9de97cf00
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActionMailer
5
+ class Base
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ @loader.call.tap do |store|
12
+ ::ActionMailer::Base.descendants.each do |mailer_class|
13
+ base_name = Utils.module_name(mailer_class) or next
14
+ content = store[base_name]
15
+ mailer_class.action_methods.each do |action_method|
16
+ method_object = mailer_class.instance_method(action_method)
17
+ parameters = method_parameters_to_rbs_method_type(method_object).to_s.gsub(' -> untyped', '')
18
+ content << "def self.#{action_method}: #{parameters} -> ::ActionMailer::MessageDelivery"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def method_parameters_to_rbs_method_type(method_object)
27
+ required_positionals = []
28
+ optional_positionals = []
29
+ rest_positionals = nil
30
+ trailing_positionals = []
31
+ required_keywords = {}
32
+ optional_keywords = {}
33
+ rest_keywords = nil
34
+ requireds = required_positionals
35
+ block = nil
36
+ untyped = RBS::Types::Bases::Any.new(location: nil)
37
+
38
+ method_object.parameters.each do |kind, name|
39
+ case kind
40
+ when :req
41
+ requireds << ::RBS::Types::Function::Param.new(name: name, type: untyped)
42
+ when :opt
43
+ requireds = trailing_positionals
44
+ optional_positionals << ::RBS::Types::Function::Param.new(name: name, type: untyped)
45
+ when :rest
46
+ requireds = trailing_positionals
47
+ name = nil if name == :*
48
+ rest_positionals = ::RBS::Types::Function::Param.new(name: name, type: untyped)
49
+ when :keyreq
50
+ required_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: untyped)
51
+ when :key
52
+ optional_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: untyped)
53
+ when :keyrest
54
+ rest_keywords = ::RBS::Types::Function::Param.new(name: name, type: untyped)
55
+ when :block
56
+ block = RBS::Types::Block.new(
57
+ type: RBS::Types::Function.empty(untyped).update(rest_positionals: RBS::Types::Function::Param.new(name: nil, type: untyped)),
58
+ required: true,
59
+ self_type: nil
60
+ )
61
+ else
62
+ raise "bug"
63
+ end
64
+ end
65
+
66
+ function = ::RBS::Types::Function.new(
67
+ required_positionals: required_positionals,
68
+ optional_positionals: optional_positionals,
69
+ rest_positionals: rest_positionals,
70
+ trailing_positionals: trailing_positionals,
71
+ required_keywords: required_keywords,
72
+ optional_keywords: optional_keywords,
73
+ rest_keywords: rest_keywords,
74
+ return_type: untyped,
75
+ )
76
+ ::RBS::MethodType.new(
77
+ location: nil,
78
+ type_params: [],
79
+ type: function,
80
+ block: block
81
+ )
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'action_mailer/base'
@@ -33,7 +33,16 @@ module Orthoses
33
33
  attribute.captures.each do |capture|
34
34
  receiver_name = Utils.module_name(capture.method.receiver) or next
35
35
  name = capture.argument[:name]
36
- cast_type = capture.argument[:cast_type] || capture.argument[:type]
36
+ active_model_version = Gem::Version.new(::ActiveModel::VERSION::STRING)
37
+ cast_type =
38
+ if active_model_version >= Gem::Version.new('7.1')
39
+ # https://github.com/rails/rails/commit/608cbfae36b125d7962b7ed9083c9e9e6ce70b88
40
+ capture.argument[:*].first
41
+ elsif active_model_version >= Gem::Version.new('7.0')
42
+ capture.argument[:cast_type]
43
+ else
44
+ capture.argument[:type]
45
+ end
37
46
 
38
47
  return_type = DEFAULT_TYPES[cast_type] || 'untyped'
39
48
 
@@ -4,8 +4,6 @@ module Orthoses
4
4
  module ActiveRecord
5
5
  class GeneratedAttributeMethods
6
6
  TARGET_TYPE_MAP = {
7
- "attribute" => "() -> %<type>s",
8
- "attribute=" => "(%<type>s) -> %<type>s",
9
7
  "attribute?" => "() -> bool",
10
8
  "attribute_before_last_save" => "() -> %<opt>s",
11
9
  "attribute_before_type_cast" => "() -> %<type>s",
@@ -50,13 +48,14 @@ module Orthoses
50
48
  opt = "#{req}?"
51
49
  type = col.null ? opt : req
52
50
 
53
- ::ActiveRecord::Base.attribute_method_matchers.each do |matcher|
54
- tmpl = TARGET_TYPE_MAP[matcher.target]
51
+ lines << "attr_accessor #{name}: #{type}"
52
+ attribute_method_patterns(::ActiveRecord::Base).each do |matcher|
53
+ tmpl = TARGET_TYPE_MAP[matcher.proxy_target] or next
55
54
  lines << "def #{matcher.method_name(name)}: #{tmpl % {type: type, opt: opt}}"
56
55
  end
57
56
  end
58
57
  klass.attribute_aliases.each do |alias_name, column_name|
59
- ::ActiveRecord::Base.attribute_method_matchers.each do |matcher|
58
+ attribute_method_patterns(::ActiveRecord::Base).each do |matcher|
60
59
  lines << "alias #{matcher.method_name(alias_name)} #{matcher.method_name(column_name)}"
61
60
  end
62
61
  end
@@ -69,6 +68,20 @@ module Orthoses
69
68
  end
70
69
  end
71
70
  end
71
+
72
+ private
73
+
74
+ def attribute_method_patterns(const)
75
+ if const.respond_to?(:attribute_method_patterns)
76
+ const.attribute_method_patterns
77
+ else
78
+ const.attribute_method_matchers.each do |matcher|
79
+ def matcher.proxy_target
80
+ target
81
+ end
82
+ end
83
+ end
84
+ end
72
85
  end
73
86
  end
74
87
  end
@@ -20,7 +20,7 @@ module Orthoses
20
20
  name = capture.argument[:name]
21
21
  body = capture.argument[:body]
22
22
 
23
- definition = "#{name}: #{parameters_to_type(body.parameters)} -> #{base_name}::ActiveRecord_Relation"
23
+ definition = "#{name}: #{parameters_to_type(parameters(body))} -> #{base_name}::ActiveRecord_Relation"
24
24
  store[base_name] << "def self.#{definition}"
25
25
  store["#{base_name}::GeneratedRelationMethods"].header = "module #{base_name}::GeneratedRelationMethods"
26
26
  store["#{base_name}::GeneratedRelationMethods"] << "def #{definition}"
@@ -58,6 +58,14 @@ module Orthoses
58
58
  end
59
59
  "(#{res.join(", ")})#{block}"
60
60
  end
61
+
62
+ def parameters(body)
63
+ if body.respond_to?(:to_proc)
64
+ body.to_proc.parameters
65
+ else
66
+ body.method(:call).parameters
67
+ end
68
+ end
61
69
  end
62
70
  end
63
71
  end
@@ -36,7 +36,7 @@ module Orthoses
36
36
  def members_prototype_of(mod_name)
37
37
  mod = Object.const_get(mod_name)
38
38
  runtime = ::RBS::Prototype::Runtime.new(patterns: nil, env: nil, merge: false)
39
- type_name = runtime.to_type_name(mod_name)
39
+ type_name = TypeName(mod_name)
40
40
  [].tap do |members|
41
41
  runtime.generate_methods(mod, type_name, members)
42
42
  end
@@ -17,6 +17,8 @@ module Orthoses
17
17
  def call
18
18
  loader = @loader
19
19
  Orthoses::Builder.new do
20
+ use Orthoses::ActionMailer::Base
21
+
20
22
  use Orthoses::ActiveModel::Attributes
21
23
  use Orthoses::ActiveModel::HasSecurePassword
22
24
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Orthoses
4
4
  module Rails
5
- VERSION = "1.3.0"
5
+ VERSION = "1.4.0"
6
6
  end
7
7
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'orthoses'
4
4
 
5
+ require_relative 'action_mailer'
5
6
  require_relative 'active_model'
6
7
  require_relative 'active_record'
7
8
  require_relative 'active_storage'
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.3.0
4
+ version: 1.4.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-09-21 00:00:00.000000000 Z
11
+ date: 2023-11-05 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.11'
19
+ version: '1.13'
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.11'
26
+ version: '1.13'
27
27
  description: Orthoses middleware collection for Ruby on Rails
28
28
  email:
29
29
  - co000ri@gmail.com
@@ -37,6 +37,8 @@ files:
37
37
  - lib/generators/orthoses/rails/install_generator.rb
38
38
  - lib/generators/orthoses/rails/templates/rails.rake
39
39
  - lib/orthoses-rails.rb
40
+ - lib/orthoses/action_mailer.rb
41
+ - lib/orthoses/action_mailer/base.rb
40
42
  - lib/orthoses/active_model.rb
41
43
  - lib/orthoses/active_model/attributes.rb
42
44
  - lib/orthoses/active_model/has_secure_password.rb