aasm_rbs 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: 87a7a8d08d3f0fc893a4c567028f4580fb8c16bd34855d7a852acfa72e4af9cd
4
+ data.tar.gz: 338bc773165c1d7d198fdb757c721bdb4b9729069537df45a69339adfaa5da22
5
+ SHA512:
6
+ metadata.gz: a2fa51357cfd06f334f3a10b331db7cf603ee06cb444e34aac5b3d8b6194a746b4e6b688cce6a4c07ebe541ab68cf3704c9e9fef329095c2ec88705816dea0f3
7
+ data.tar.gz: 8b293ace47666124f8159ffa56f0d40ab50f8ac0c844230d98c53293b074da923ada7bcd3639f5037452f45f81d68cca5910e2353289c81ed2feaae594a4b3e0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 Lorenzo Zabot
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # AASM RBS Generator
2
+ Easily generate RBS signatures for all the AASM automatically generated methods and constants of your ruby classes.
3
+
4
+ ## Description
5
+ If you have found this gem, you probably are adding a type system with RBS on top of your Ruby project or Rails application, and you are managing the state of some of your classes with the AASM gem.
6
+
7
+ If you have no idea about what AASM is, I encourage you to take a look at their [README](https://github.com/aasm/aasm) first.
8
+
9
+ You should now know that when you `include AASM` inside of a Ruby class and you define states, events and transitions, your classes will automatically get a few things, including:
10
+ - a constant for every state
11
+ - instance methods for every state
12
+ - scopes for every state if the class is an `ActiveRecord` model and the [automatic scopes](https://github.com/aasm/aasm#automatic-scopes) feature was not disabled manually
13
+ - instance methods for every event
14
+
15
+ The problem is that when writing RBS you should write the signatures for the previous things one-by-one and it can get really frustrating/boring when dealing with large classes.
16
+
17
+ With this small gem, you can now generate all those signatures automatically with a single command, and save time for doing something more meaningful.
18
+
19
+ ## Installation
20
+ Add the following line to your application's `Gemfile` in the `development` group:
21
+
22
+ ```rb
23
+ gem 'aasm_rbs'
24
+ ```
25
+
26
+ Then, execute `bundle install` in order to load the gem's code.
27
+
28
+ ## Usage
29
+ Generating the RBS signatures is as easy as launching the following command from the command-line:
30
+ ```
31
+ bundle exec aasm_rbs ClassName
32
+ ```
33
+
34
+ The generated signatures will appear in `stdout`.
35
+
36
+
37
+ ## License
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/license/mit/).
data/exe/aasm_rbs ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/aasm_rbs'
4
+
5
+ $stdout.puts ''
6
+ $stdout.puts AasmRbs.run(ARGV[0] || '')
7
+ $stdout.puts ''
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AasmRbs
4
+ class Output
5
+ def initialize(klass)
6
+ @klass = klass
7
+ superclass = klass.superclass == Object ? nil : " < #{klass.superclass}"
8
+ self.data = "class #{klass}#{superclass}\n"
9
+ end
10
+
11
+ def add_states(states)
12
+ add_state_constants(states)
13
+ create_scopes = klass.aasm.state_machine.config.create_scopes
14
+ active_record_model = klass.respond_to?(:aasm_create_scope)
15
+ add_state_scopes(states) if active_record_model && create_scopes
16
+ add_predicate_states_methods(states)
17
+ end
18
+
19
+ def add_events(events)
20
+ events.each do |event|
21
+ self.data += " def #{event}: (*untyped) -> bool\n"
22
+ self.data += " def #{event}!: (*untyped) -> bool\n"
23
+ self.data += " def #{event}_without_validation!: (*untyped) -> bool\n"
24
+ self.data += " def may_#{event}?: (*untyped) -> bool\n"
25
+ end
26
+ end
27
+
28
+ def new_line
29
+ self.data += "\n"
30
+ end
31
+
32
+ def finalize
33
+ self.data += "end\n"
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :klass
39
+ attr_accessor :data
40
+
41
+ def add_state_constants(states)
42
+ states.each { |state| self.data += " STATE_#{state.upcase}: String\n" }
43
+ self.data += "\n"
44
+ end
45
+
46
+ def add_state_scopes(states)
47
+ states.each { |state| self.data += " def self.#{state}: () -> ::ActiveRecord_Relation\n" }
48
+ self.data += "\n"
49
+ end
50
+
51
+ def add_predicate_states_methods(states)
52
+ states.each { |state| self.data += " def #{state}?: () -> bool\n" }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AasmRbs
4
+ VERSION = '0.1.0'
5
+ end
data/lib/aasm_rbs.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'aasm_rbs/output'
4
+
5
+ module AasmRbs
6
+ def self.run(klass_name)
7
+ klass = Object.const_get(klass_name)
8
+ states = klass.aasm.states.map(&:name)
9
+ events = klass.aasm.events.map(&:name)
10
+
11
+ output = Output.new(klass)
12
+ output.add_states(states)
13
+ output.new_line
14
+ output.add_events(events)
15
+ output.finalize
16
+ rescue StandardError
17
+ print "aasm_rbs received an invalid class name."
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module AasmRbs
2
+ class Output
3
+ def initialize: (Class) -> String
4
+
5
+ def add_states: (Array[String]) -> Array[String]
6
+ def add_events: (Array[String]) -> Array[String]
7
+
8
+ def new_line: () -> String
9
+ def finalize: () -> String
10
+
11
+ private
12
+
13
+ attr_reader klass: Class
14
+ attr_accessor data: String
15
+
16
+ def add_state_constants: (Array[String]) -> String
17
+ def add_state_scopes: (Array[String]) -> String
18
+ def add_predicate_states_methods: (Array[String]) -> Array[String]
19
+ end
20
+ end
data/sig/aasm_rbs.rbs ADDED
@@ -0,0 +1,10 @@
1
+ module AasmRbs
2
+ def self.run: (String) -> String?
3
+ end
4
+
5
+ # There are yet no official RBS signatures for the AASM module that gets
6
+ # included in our classes. Therefore, the only thing we can do in order to not
7
+ # make Steep unhappy is to declare a dummy signature ourself for the #aasm method.
8
+ class Class
9
+ def aasm: () -> untyped
10
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aasm_rbs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lorenzo Zabot
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aasm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ description: Easily generate RBS signatures for all the AASM automatically generated
28
+ methods and constants of your ruby classes.
29
+ email:
30
+ - lorenzozabot@gmail.com
31
+ executables:
32
+ - aasm_rbs
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE
37
+ - README.md
38
+ - exe/aasm_rbs
39
+ - lib/aasm_rbs.rb
40
+ - lib/aasm_rbs/output.rb
41
+ - lib/aasm_rbs/version.rb
42
+ - sig/aasm_rbs.rbs
43
+ - sig/aasm_rbs/output.rbs
44
+ homepage: https://github.com/Uaitt/aasm_rbs
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ allowed_push_host: https://rubygems.org
49
+ homepage_uri: https://github.com/Uaitt/aasm_rbs
50
+ source_code_uri: https://github.com/Uaitt/aasm_rbs
51
+ rubygems_mfa_required: 'true'
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.0.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.3.7
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: AASM RBS
71
+ test_files: []