orthoses-rails 0.1.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: 76d6291b36d7d10e10f2d5b0898fa06a8a886375f9f91e91b2db718847c42eb2
4
+ data.tar.gz: 9d33694af285fdc0a214af35c26e143990689bca720429ca7029ccb48f0e531b
5
+ SHA512:
6
+ metadata.gz: a00f874e057a7515e2427bef047c25fc4a5bfe5f698e177b4e3919b4acc2df4cd4093b8fd407c7c493a1b548bbed0340f916af6f43bb37b0bd01e128f3ca3f8f
7
+ data.tar.gz: 77d8183eafa99f69864d37eae0dea091f0a2d0cf57530d10d524c61cb2ababdd8478ba6a2aad19e3be76119af7a6ee7be8e7fcd3bb8d1a7c06487c54aef3ebf3
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at co000ri@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 ksss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Orthoses::Rails
2
+
3
+ [Orthoses](https://github.com/ksss/orthoses) extension for Ruby on Rails.
4
+ Orthoses::Rails automatically generates RBS for methods added by Rails.
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add orthoses-rails
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install orthoses-rails
15
+
16
+ ## Usage
17
+
18
+ ```rb
19
+ Orthoses::Builder.new do
20
+ use Orthoses::CreateFileByName
21
+ base_dir: Rails.root.join("sig/out"),
22
+ header: "# !!! GENERATED CODE !!!"
23
+ use Orthoses::Filter,
24
+ if: -> (name, _content) {
25
+ path, _lineno = Object.const_source_location(name)
26
+ return false unless path
27
+ %r{app/models}.match?(path)
28
+ }
29
+ use YourCustom::Middleware
30
+ use Orthoses::ActiveModel::HasSecurePassword
31
+ use Orthoses::ActiveRecord::BelongsTo
32
+ use Orthoses::ActiveRecord::GeneratedAttributeMethods
33
+ use Orthoses::ActiveRecord::HasMany
34
+ use Orthoses::ActiveRecord::HasOne
35
+ use Orthoses::ActiveSupport::Concern
36
+ use Orthoses::ActiveSupport::ClassAttribute
37
+ use Orthoses::ActiveSupport::MattrAccessor
38
+ use Orthoses::ActiveSupport::TimeWithZone
39
+ use Orthoses::Mixin
40
+ use Orthoses::Constant, strict: false
41
+ use Orthoses::ObjectSpaceAll
42
+ run -> () {
43
+ Rake::Task[:environment].invoke
44
+ Rails.application.eager_load!
45
+ Orthoses::Utils.unautoload!
46
+ }
47
+ end.call
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ksss/orthoses-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ksss/orthoses-rails/blob/main/CODE_OF_CONDUCT.md).
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Code of Conduct
65
+
66
+ Everyone interacting in the Orthoses::Rails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ksss/orthoses-rails/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveModel
5
+ # < 6.0
6
+ # def has_secure_password(options = {})
7
+ # >= 6.0
8
+ # def has_secure_password(attribute = :password, validations: true)
9
+ class HasSecurePassword
10
+ def initialize(loader, if: nil)
11
+ @loader = loader
12
+ @if = binding.local_variable_get(:if)
13
+ end
14
+
15
+ def call
16
+ target_method = ::ActiveRecord::Base.method(:has_secure_password)
17
+ call_tracer = Orthoses::CallTracer.new
18
+
19
+ result = call_tracer.trace(target_method) do
20
+ @loader.call
21
+ end
22
+
23
+ call_tracer.result.each do |method, argument|
24
+ base = method.receiver.to_s
25
+ if argument[:attribute].nil? # < 6.0
26
+ result[base].delete_if { |line| line == "include ActiveModel::SecurePassword::InstanceMethodsOnActivation" }
27
+ else
28
+ result[base].delete_if { |line| line.start_with?("include #<InstanceMethodsOnActivation:") }
29
+ end
30
+ attribute = argument[:attribute] || :password
31
+ mod_name = "ActiveModel_SecurePassword_InstanceMethodsOnActivation_#{attribute}"
32
+ lines = []
33
+ lines << "attr_reader #{attribute}: String?"
34
+ lines << "def #{attribute}=: (String) -> String"
35
+ lines << "def #{attribute}_confirmation=: (String) -> String"
36
+ lines << "def authenticate_#{attribute}: (String) -> (#{base} | false)"
37
+ if attribute == :password
38
+ lines << "alias authenticate authenticate_password"
39
+ end
40
+ result["module #{base}::#{mod_name}"].concat(lines)
41
+ result[base] << "include #{mod_name}"
42
+ end
43
+
44
+ result
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveRecord
5
+ class BelongsTo
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ @loader.call.tap do |store|
12
+ ::ActiveRecord::Base.descendants.each do |base|
13
+ next if base.abstract_class?
14
+
15
+ lines = base.reflect_on_all_associations(:belongs_to).flat_map do |ref|
16
+ # FIXME: Can I get list of class for polymorphic?
17
+ type = ref.polymorphic? ? 'untyped' : ref.klass.to_s
18
+ opt = "#{type}?"
19
+
20
+ [
21
+ "def #{ref.name}: () -> #{opt}",
22
+ "def #{ref.name}=: (#{opt}) -> #{opt}",
23
+ "def reload_#{ref.name}: () -> #{opt}",
24
+ ].tap do |ary|
25
+ if !ref.polymorphic?
26
+ ary << "def build_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
27
+ ary << "def create_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
28
+ ary << "def create_#{ref.name}!: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}"
29
+ end
30
+ end
31
+ end
32
+
33
+ generated_association_methods = "#{base}::GeneratedAssociationMethods"
34
+ store["module #{generated_association_methods}"].concat(lines)
35
+
36
+ code = "include #{generated_association_methods}"
37
+ store[base.to_s] << code
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveRecord
5
+ class GeneratedAttributeMethods
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ @loader.call.tap do |store|
12
+ ::ActiveRecord::Base.descendants.each do |klass|
13
+ next if klass.abstract_class?
14
+
15
+ name = klass.to_s
16
+ generated_attribute_methods = "#{name}::AttributeMethods::GeneratedAttributeMethods"
17
+ store[name] << "include #{generated_attribute_methods}"
18
+ lines = klass.columns.flat_map do |col|
19
+ req = sql_type_to_rbs(col.type)
20
+ opt = "#{req}?"
21
+ type = col.null ? opt : req
22
+
23
+ [
24
+ "def #{col.name}: () -> #{type}",
25
+ "def #{col.name}=: (#{type}) -> #{type}",
26
+ "def #{col.name}?: () -> bool",
27
+ "def #{col.name}_changed?: () -> bool",
28
+ "def #{col.name}_change: () -> [#{opt}, #{opt}]",
29
+ "def #{col.name}_will_change!: () -> void",
30
+ "def #{col.name}_was: () -> #{opt}",
31
+ "def #{col.name}_previously_changed?: () -> bool",
32
+ "def #{col.name}_previous_change: () -> Array[#{opt}]?",
33
+ "def #{col.name}_previously_was: () -> #{opt}",
34
+ "def #{col.name}_before_last_save: () -> #{opt}",
35
+ "def #{col.name}_change_to_be_saved: () -> Array[#{opt}]?",
36
+ "def #{col.name}_in_database: () -> #{opt}",
37
+ "def saved_change_to_#{col.name}: () -> Array[#{opt}]?",
38
+ "def saved_change_to_#{col.name}?: () -> bool",
39
+ "def will_save_change_to_#{col.name}?: () -> bool",
40
+ "def restore_#{col.name}!: () -> void",
41
+ "def clear_#{col.name}_change: () -> void",
42
+ ]
43
+ end
44
+ store["module #{generated_attribute_methods}"].concat(lines)
45
+ end
46
+ end
47
+ end
48
+
49
+ # Thanks https://github.com/pocke/rbs_rails/blob/8a128a8d29f0861aa2c25aa4110ff7c2ea674865/lib/rbs_rails/active_record.rb#L525-L551
50
+ def sql_type_to_rbs(t)
51
+ case t
52
+ when :integer
53
+ 'Integer'
54
+ when :float
55
+ 'Float'
56
+ when :decimal
57
+ 'BigDecimal'
58
+ when :string, :text, :citext, :uuid, :binary
59
+ 'String'
60
+ when :datetime
61
+ 'ActiveSupport::TimeWithZone'
62
+ when :boolean
63
+ "bool"
64
+ when :jsonb, :json
65
+ "untyped"
66
+ when :date
67
+ 'Date'
68
+ when :time
69
+ 'Time'
70
+ when :inet
71
+ "IPAddr"
72
+ else
73
+ # Unknown column type, give up
74
+ 'untyped'
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveRecord
5
+ class HasMany
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ @loader.call.tap do |store|
12
+ ::ActiveRecord::Base.descendants.each do |base|
13
+ next if base.abstract_class?
14
+
15
+ generated_association_methods = "#{base}::GeneratedAssociationMethods"
16
+ collection_proxy = "#{base}::ActiveRecord_Associations_CollectionProxy"
17
+
18
+ lines = base.reflect_on_all_associations(:has_many).flat_map do |ref|
19
+ singular_name = ref.name.to_s.singularize
20
+
21
+ [
22
+ "def #{ref.name}: () -> #{collection_proxy}",
23
+ "def #{ref.name}=: (#{collection_proxy} | Array[#{ref.klass}]) -> (#{collection_proxy} | Array[#{ref.klass}])",
24
+ "def #{singular_name}_ids: () -> Array[Integer]",
25
+ "def #{singular_name}_ids=: (Array[Integer]) -> Array[Integer]",
26
+ ]
27
+ end
28
+
29
+ store["module #{generated_association_methods}"].concat(lines)
30
+
31
+ code = "include #{generated_association_methods}"
32
+ store[base.to_s] << code
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveRecord
5
+ class HasOne
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ @loader.call.tap do |store|
12
+ ::ActiveRecord::Base.descendants.each do |base|
13
+ next if base.abstract_class?
14
+
15
+ lines = base.reflect_on_all_associations(:has_one).flat_map do |ref|
16
+ type = ref.klass.to_s
17
+ opt = "#{type}?"
18
+
19
+ [
20
+ "def #{ref.name}: () -> #{opt}",
21
+ "def #{ref.name}=: (#{opt}) -> #{opt}",
22
+ "def build_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
23
+ "def create_#{ref.name}: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
24
+ "def create_#{ref.name}!: (?untyped attributes) ?{ (#{type}) -> void } -> #{type}",
25
+ "def reload_#{ref.name}: () -> #{opt}",
26
+ ]
27
+ end
28
+
29
+ generated_association_methods = "#{base}::GeneratedAssociationMethods"
30
+ store["module #{generated_association_methods}"].concat(lines)
31
+
32
+ code = "include #{generated_association_methods}"
33
+ store[base.to_s] << code
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveSupport
5
+ # class Class
6
+ # # >= v5.2
7
+ # def class_attribute(*attrs)
8
+ # # <= v6.0
9
+ # def class_attribute(*attrs, instance_accessor: true,
10
+ # instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil)
11
+ class ClassAttribute
12
+ def initialize(loader, if: nil)
13
+ @loader = loader
14
+ @if = binding.local_variable_get(:if)
15
+ end
16
+
17
+ def call
18
+ require 'active_support/core_ext/class/attribute'
19
+ target_method = ::Class.instance_method(:class_attribute)
20
+ call_tracer = Orthoses::CallTracer.new
21
+
22
+ result = call_tracer.trace(target_method) do
23
+ @loader.call
24
+ end
25
+
26
+ call_tracer.result.each do |method, argument|
27
+ methods = []
28
+ if ::ActiveSupport.version < Gem::Version.new("6.0")
29
+ options = argument[:attrs].extract_options!
30
+ argument[:instance_reader] = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
31
+ argument[:instance_writer] = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
32
+ argument[:instance_predicate] = options.fetch(:instance_predicate, true)
33
+ argument[:default_value] = options.fetch(:default, nil)
34
+ end
35
+ argument[:attrs].each do |name|
36
+ next unless @if.nil? || @if.call(method, name)
37
+
38
+ # skip internal attribute
39
+ next if name.to_s.start_with?("_")
40
+ next if name == :attribute_type_decorations
41
+ next if name == :attributes_to_define_after_schema_loads
42
+
43
+ methods << "def self.#{name}: () -> untyped"
44
+ methods << "def self.#{name}?: () -> bool" if argument[:instance_predicate]
45
+ methods << "def self.#{name}=: (untyped value) -> untyped"
46
+ methods << "def #{name}: () -> untyped" if argument[:instance_reader]
47
+ methods << "def #{name}?: () -> bool" if argument[:instance_predicate] && argument[:instance_reader]
48
+ methods << "attr_writer #{name}: untyped" if argument[:instance_writer]
49
+ end
50
+ next if methods.empty?
51
+
52
+ result[method.receiver.to_s].concat(methods)
53
+ end
54
+
55
+ result
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveSupport
5
+ class Concern
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ def call
11
+ extended = CallTracer.new
12
+ result = extended.trace(::ActiveSupport::Concern.method(:extended)) do
13
+ @loader.call
14
+ end
15
+ extended.result.each do |method, argument|
16
+ result[argument[:base].to_s] << "extend ActiveSupport::Concern"
17
+ end
18
+ result
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveSupport
2
+ class TimeWithZone
3
+ end
4
+ end
@@ -0,0 +1,64 @@
1
+ module DateAndTime
2
+ module Calculations
3
+ end
4
+ end
5
+ module ActiveSupport
6
+ class TimeZone
7
+ end
8
+ class Duration
9
+ end
10
+ end
11
+
12
+ class Time
13
+ include DateAndTime::Calculations
14
+ def in_time_zone: (?(ActiveSupport::TimeZone | String) new_zone) -> ::ActiveSupport::TimeWithZone
15
+ | (false? zone) -> ::Time
16
+ def +: (ActiveSupport::Duration other) -> self
17
+ | ...
18
+ def -: (ActiveSupport::Duration other) -> self
19
+ | ...
20
+ alias at_beginning_of_day beginning_of_day
21
+ alias at_beginning_of_hour beginning_of_hour
22
+ alias at_beginning_of_minute beginning_of_minute
23
+ alias at_end_of_day end_of_day
24
+ alias at_end_of_hour end_of_hour
25
+ alias at_end_of_minute end_of_minute
26
+ alias at_midday middle_of_day
27
+ alias at_middle_of_day middle_of_day
28
+ alias at_midnight beginning_of_day
29
+ alias at_noon middle_of_day
30
+ def beginning_of_day: () -> self
31
+ def beginning_of_hour: () -> self
32
+ def beginning_of_minute: () -> self
33
+ def compare_with_coercion: (untyped other) -> (-1 | 0 | 1 | nil)
34
+ def compare_without_coercion: (untyped other) -> (-1 | 0 | 1 | nil)
35
+ def end_of_day: () -> self
36
+ def end_of_hour: () -> self
37
+ def end_of_minute: () -> self
38
+ def eql_with_coercion: (untyped other) -> bool
39
+ def eql_without_coercion: (untyped other) -> bool
40
+ alias midday middle_of_day
41
+ def middle_of_day: () -> self
42
+ alias midnight beginning_of_day
43
+ def minus_with_coercion: (Time | self arg0) -> Float
44
+ | (Numeric arg0) -> self
45
+ def minus_with_duration: (Time | self arg0) -> Float
46
+ | (Numeric arg0) -> self
47
+ def minus_without_coercion: (Time | self arg0) -> Float
48
+ | (Numeric arg0) -> self
49
+ def minus_without_duration: (Time | self arg0) -> Float
50
+ | (Numeric arg0) -> self
51
+ def next_day: () -> self
52
+ def next_month: () -> self
53
+ def next_year: () -> self
54
+ alias noon middle_of_day
55
+ def plus_without_duration: (Time | self arg0) -> Float
56
+ | (Numeric arg0) -> self
57
+ def prev_day: () -> self
58
+ def prev_month: () -> self
59
+ def prev_year: () -> self
60
+ def seconds_since_midnight: () -> Float
61
+ def seconds_until_end_of_day: () -> Integer
62
+ def sec_fraction: () -> (Integer | Rational)
63
+ def to_default_s: () -> String
64
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveSupport
5
+ # class Module
6
+ # def mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil, location: nil)
7
+ # def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
8
+ # def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
9
+ class MattrAccessor
10
+ def initialize(loader, if: nil)
11
+ @loader = loader
12
+ @if = binding.local_variable_get(:if)
13
+ end
14
+
15
+ def call
16
+ mattr_reader = Orthoses::CallTracer.new
17
+ mattr_writer = Orthoses::CallTracer.new
18
+
19
+ require 'active_support/core_ext/module/attribute_accessors.rb'
20
+ result = mattr_reader.trace(::Module.instance_method(:mattr_reader)) do
21
+ mattr_writer.trace(::Module.instance_method(:mattr_writer)) do
22
+ @loader.call
23
+ end
24
+ end
25
+
26
+ mattr_reader.result.each do |method, argument|
27
+ base = method.receiver.to_s
28
+ methods = []
29
+ argument[:syms].each do |sym|
30
+ next unless @if.nil? || @if.call(method, sym)
31
+
32
+ methods << "def self.#{sym}: () -> untyped"
33
+ if argument[:instance_reader] && argument[:instance_accessor]
34
+ methods << "def #{sym}: () -> untyped"
35
+ end
36
+ end
37
+ next if methods.empty?
38
+
39
+ result[base].concat(methods)
40
+ end
41
+
42
+ mattr_writer.result.each do |method, argument|
43
+ base = method.receiver.to_s
44
+ methods = []
45
+ argument[:syms].each do |sym|
46
+ next unless @if.nil? || @if.call(method, sym)
47
+
48
+ methods << "def self.#{sym}=: (untyped val) -> untyped"
49
+ if argument[:instance_writer] && argument[:instance_accessor]
50
+ methods << "def #{sym}=: (untyped val) -> untyped"
51
+ end
52
+ end
53
+ next if methods.empty?
54
+
55
+ result[base].concat(methods)
56
+ end
57
+
58
+ result
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module ActiveSupport
5
+ class TimeWithZone
6
+ def initialize(loader)
7
+ @loader = loader
8
+ end
9
+
10
+ LOAD_PATHS = [
11
+ File.expand_path("known_sig/time.rbs", __dir__),
12
+ File.expand_path("known_sig/active_support/time_with_zone.rbs", __dir__),
13
+ ]
14
+
15
+ # Time <= (known Time)
16
+ # TimeWithZone <= (known TimeWithZone, known Time, core Time)
17
+ def call
18
+ store = Orthoses::LoadRBS.new(@loader, paths: LOAD_PATHS).call
19
+
20
+ time_with_zone_store = store["ActiveSupport::TimeWithZone"]
21
+ each_line_from_core_time_definition do |line|
22
+ time_with_zone_store << line
23
+ end
24
+
25
+ store
26
+ end
27
+
28
+ private
29
+
30
+ NOT_DELEGATE_METHODS = %i[
31
+ utc
32
+ getgm
33
+ getutc
34
+ gmtime
35
+ localtime
36
+ ]
37
+
38
+ def each_line_from_core_time_definition
39
+ type_name_time = TypeName("::Time")
40
+ loader = RBS::EnvironmentLoader.new
41
+ env = RBS::Environment.from_loader(loader)
42
+ LOAD_PATHS.each do |path|
43
+ RBS::Parser.parse_signature(File.read(path)).each do |decl|
44
+ env << decl
45
+ end
46
+ end
47
+
48
+ builder = RBS::DefinitionBuilder.new(env: env.resolve_type_names)
49
+ one_ancestors = builder.ancestor_builder.one_instance_ancestors(type_name_time)
50
+ one_ancestors.included_modules.each do |included_module|
51
+ yield "include #{included_module.source.name}"
52
+ end
53
+ builder.build_instance(type_name_time).methods.each do |sym, definition_method|
54
+ next if !definition_method.public?
55
+ definition_method.defs.reject! do |type_def|
56
+ type_def.implemented_in != type_name_time
57
+ end
58
+ next if definition_method.defs.empty?
59
+
60
+ if !NOT_DELEGATE_METHODS.include?(sym)
61
+ definition_method.method_types.each do |method_type|
62
+ rt = method_type.type.return_type
63
+ if rt.instance_of?(RBS::Types::ClassInstance) && rt.name.to_s == "::Time"
64
+ rt.instance_variable_set(:@name, RBS::Types::Bases::Self.new(location: nil))
65
+ end
66
+ end
67
+ end
68
+
69
+ if definition_method.alias_of.nil?
70
+ yield "def #{sym}: #{definition_method.method_types.join(" | ")}"
71
+ else
72
+ yield "alias #{sym} #{definition_method.alias_of.defs.first.member.name}"
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Orthoses
4
+ module Rails
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'orthoses'
4
+
5
+ require_relative "rails/version"
6
+
7
+ require_relative 'active_model/has_secure_password'
8
+
9
+ require_relative 'active_record/generated_attribute_methods'
10
+ require_relative 'active_record/has_many'
11
+ require_relative 'active_record/has_one'
12
+ require_relative 'active_record/belongs_to'
13
+
14
+ require_relative 'active_support/class_attribute'
15
+ require_relative 'active_support/concern'
16
+ require_relative 'active_support/mattr_accessor'
17
+ require_relative 'active_support/time_with_zone'
@@ -0,0 +1 @@
1
+ require 'orthoses/rails'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/orthoses/rails/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "orthoses-rails"
7
+ spec.version = Orthoses::Rails::VERSION
8
+ spec.authors = ["ksss"]
9
+ spec.email = ["co000ri@gmail.com"]
10
+
11
+ spec.summary = "Orthoses middleware collection for Ruby on Rails"
12
+ spec.description = "Orthoses middleware collection for Ruby on Rails"
13
+ spec.homepage = "https://github.com/ksss/orthoses-rails"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ next true if (f == __FILE__)
24
+ next true if f.match?(%r{\A(?:bin|known_sig)/}) # dir
25
+ next true if f.match?(%r{\A\.(?:git)}) # git
26
+ next true if f.match?(%r{\A(?:rbs_collection|Steepfile|Rakefile|Gemfile)}) # top file
27
+ next true if f.match?(%r{_test\.rb\z}) # test
28
+ false
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "orthoses"
36
+ end
@@ -0,0 +1,6 @@
1
+ module Orthoses
2
+ module Rails
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orthoses-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: orthoses
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Orthoses middleware collection for Ruby on Rails
28
+ email:
29
+ - co000ri@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CODE_OF_CONDUCT.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/orthoses-rails.rb
38
+ - lib/orthoses/active_model/has_secure_password.rb
39
+ - lib/orthoses/active_record/belongs_to.rb
40
+ - lib/orthoses/active_record/generated_attribute_methods.rb
41
+ - lib/orthoses/active_record/has_many.rb
42
+ - lib/orthoses/active_record/has_one.rb
43
+ - lib/orthoses/active_support/class_attribute.rb
44
+ - lib/orthoses/active_support/concern.rb
45
+ - lib/orthoses/active_support/known_sig/active_support/time_with_zone.rbs
46
+ - lib/orthoses/active_support/known_sig/time.rbs
47
+ - lib/orthoses/active_support/mattr_accessor.rb
48
+ - lib/orthoses/active_support/time_with_zone.rb
49
+ - lib/orthoses/rails.rb
50
+ - lib/orthoses/rails/version.rb
51
+ - orthoses-rails.gemspec
52
+ - sig/orthoses/rails.rbs
53
+ homepage: https://github.com/ksss/orthoses-rails
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/ksss/orthoses-rails
58
+ source_code_uri: https://github.com/ksss/orthoses-rails
59
+ changelog_uri: https://github.com/ksss/orthoses-rails
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.6.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.4.0.dev
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Orthoses middleware collection for Ruby on Rails
79
+ test_files: []