aasm_rbs 0.1.1 → 0.2.1

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: 0fbdb030fb9e8f7ad127801029354cde4f093b439a79a6ee6f6e34ecf434c743
4
- data.tar.gz: 94313e1c7b7d87c81a3cb60707828bfe29e0d1b912f99f23e57371e7afaff250
3
+ metadata.gz: d13405faad77a8c8a21b0ee27b74c3b57acfc470c444ceed6c485421607a13b3
4
+ data.tar.gz: 9578eea618f8dc30c6164f90712435b616562bb2af422db57b951d792a2f07f8
5
5
  SHA512:
6
- metadata.gz: 3cef3ef97abc72ae1d60fafe2fc0eb8161a833f1178ca0bb394a5171b54f98fa30373c4b369cec0326730bc578cc549d5b588766fd4a985d346801cf81171fe9
7
- data.tar.gz: 670b68f23ab091ab8d48789c793b8abd337621f92cf1e1e9037bdaa7dc229c40aaba5e2630923362ed65831da6143f62acc2117088d1be01a4538f0238a4b9a8
6
+ metadata.gz: f3a0b8d07358a4381579f2e5a42c6b2c633c73263a8cc0b7cc23de615e343c2cb1ea99ea1470e1a9a062e50e16584fed392aa67a268e8fe3aa38f04f3e572043
7
+ data.tar.gz: 7c84da47ecb1afc0b4443c13628e2ceb0efff26038d2e5fde2602dffa3a001c38da9c31a6675a0d62124d576f95b888c924ea51ffee5aac06925054fe76d722a
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # AASM RBS Generator
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/aasm_rbs.svg)](https://badge.fury.io/rb/aasm_rbs) [![Gem Downloads](https://badgen.net/rubygems/dt/aasm_rbs)](https://rubygems.org/gems/aasm_rbs) [![Linters](https://github.com/Uaitt/aasm_rbs/actions/workflows/linters.yml/badge.svg)](https://github.com/Uaitt/aasm_rbs/actions/workflows/linters.yml) [![Specs](https://github.com/Uaitt/aasm_rbs/actions/workflows/specs.yml/badge.svg)](https://github.com/Uaitt/aasm_rbs/actions/workflows/specs.yml)
4
+
2
5
  Easily generate RBS signatures for all the AASM automatically generated methods and constants of your ruby classes.
3
6
 
4
7
  ## Description
@@ -26,11 +29,30 @@ gem 'aasm_rbs'
26
29
  Then, execute `bundle install` in order to load the gem's code.
27
30
 
28
31
  ## Usage
32
+ At the moment AASM RBS only supports pure-ruby projects or Rails applications.
33
+
34
+ This gem assumes that your project is arranged with a traditional structure:
35
+ - If dealing with a Rails app, your classes should be in any folder nested inside of `app/` or `lib/`
36
+ - If dealing with a Ruby project, your actual classes should go inside of `lib/` and arranged as:
37
+ ```
38
+ lib/
39
+ ├── foo/
40
+ │ ├── bar.rb # contains Foo::Bar
41
+ │ ├── baz.rb # contains Foo::Baz
42
+ │── foo.rb # contains Foo
43
+ ```
44
+
45
+ For more information about how to structure your projects, take a look at the following articles:
46
+ - [Autoloading and reloading constants](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) from the Rails guides
47
+ - [Exploring the structure of a Ruby gem](https://www.cloudbees.com/blog/exploring-structure-ruby-gems) fantastic article from cloudbees (a little bit old but still relevant)
48
+
29
49
  Generating the RBS signatures is as easy as launching the following command from the command-line:
30
50
  ```
31
- bundle exec aasm_rbs ClassName
51
+ bundle exec aasm_rbs Namespace::ClassName
32
52
  ```
33
53
 
54
+ If your class is namespaced inside of other modules/classes, please pass the whole name as you see in the previous command to AASM RBS or it won't be able to infer the path.
55
+
34
56
  The generated signatures will appear in `stdout`.
35
57
 
36
58
 
data/exe/aasm_rbs CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
4
- abort 'Please run aasm_rbs from the project root.'
4
+ abort('Please run aasm_rbs from the project root.')
5
5
  end
6
6
 
7
7
  require_relative '../lib/aasm_rbs'
8
8
 
9
- AasmRbs.load_constants(ARGV[0])
9
+ AasmRbs.load_constants(ARGV[0] || '')
10
10
 
11
11
  $stdout.puts ''
12
12
  $stdout.puts AasmRbs.run(ARGV[0] || '')
@@ -2,17 +2,17 @@
2
2
 
3
3
  module AasmRbs
4
4
  class Output
5
+ attr_reader :data
6
+
5
7
  def initialize(klass)
6
8
  @klass = klass
7
9
  superclass = klass.superclass == Object ? nil : " < #{klass.superclass}"
8
10
  self.data = "class #{klass}#{superclass}\n"
9
11
  end
10
12
 
11
- def add_states(states)
13
+ def add_states(states, opts = {})
12
14
  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
15
+ add_state_scopes(states) if opts[:scopes]
16
16
  add_predicate_states_methods(states)
17
17
  end
18
18
 
@@ -25,6 +25,16 @@ module AasmRbs
25
25
  end
26
26
  end
27
27
 
28
+ def add_active_record_relation
29
+ self.data += <<-RBS
30
+ class ActiveRecord_Relation < ::ActiveRecord::Relation
31
+ include GeneratedRelationMethods
32
+ include _ActiveRecord_Relation[#{klass}, Integer]
33
+ include Enumerable[#{klass}]
34
+ end
35
+ RBS
36
+ end
37
+
28
38
  def new_line
29
39
  self.data += "\n"
30
40
  end
@@ -36,20 +46,21 @@ module AasmRbs
36
46
  private
37
47
 
38
48
  attr_reader :klass
39
- attr_accessor :data
49
+ attr_writer :data
40
50
 
41
51
  def add_state_constants(states)
42
52
  states.each { |state| self.data += " STATE_#{state.upcase}: String\n" }
43
- self.data += "\n"
53
+ new_line
44
54
  end
45
55
 
46
56
  def add_state_scopes(states)
47
- states.each { |state| self.data += " def self.#{state}: () -> ::ActiveRecord_Relation\n" }
48
- self.data += "\n"
57
+ states.each { |state| self.data += " def self.#{state}: () -> ActiveRecord_Relation\n" }
58
+ new_line
49
59
  end
50
60
 
51
61
  def add_predicate_states_methods(states)
52
62
  states.each { |state| self.data += " def #{state}?: () -> bool\n" }
63
+ new_line
53
64
  end
54
65
  end
55
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AasmRbs
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/aasm_rbs.rb CHANGED
@@ -4,7 +4,7 @@ require_relative 'aasm_rbs/output'
4
4
 
5
5
  module AasmRbs
6
6
  def self.load_constants(klass_name)
7
- load './Rakefile' if File.exist?('./Rakefile')
7
+ load('./Rakefile') if File.exist?('./Rakefile')
8
8
  begin
9
9
  Rake::Task[:environment].invoke
10
10
  rescue StandardError
@@ -17,21 +17,26 @@ module AasmRbs
17
17
  begin
18
18
  require file
19
19
  rescue LoadError
20
- abort puts 'There was a problem loading the class file'
20
+ abort('There was a problem loading the class file.')
21
21
  end
22
22
  end
23
23
 
24
- def self.run(klass_name)
24
+ def self.run(klass_name, opts = {})
25
25
  klass = Object.const_get(klass_name)
26
+ output = Output.new(klass)
27
+
26
28
  states = klass.aasm.states.map(&:name)
27
29
  events = klass.aasm.events.map(&:name)
28
30
 
29
- output = Output.new(klass)
30
- output.add_states(states)
31
- output.new_line
31
+ create_scopes = klass.aasm.state_machine.config.create_scopes
32
+ active_record_model = klass.respond_to?(:aasm_create_scope)
33
+ opts[:scopes] = true if create_scopes && active_record_model
34
+
35
+ output.add_states(states, opts)
32
36
  output.add_events(events)
37
+ output.new_line && output.add_active_record_relation if opts[:scopes]
33
38
  output.finalize
34
39
  rescue StandardError
35
- print "aasm_rbs received an invalid class name."
40
+ abort('aasm_rbs received an invalid class name.')
36
41
  end
37
42
  end
@@ -1,9 +1,12 @@
1
1
  module AasmRbs
2
2
  class Output
3
- def initialize: (Class) -> String
3
+ attr_reader data: String
4
4
 
5
- def add_states: (Array[String]) -> Array[String]
6
- def add_events: (Array[String]) -> Array[String]
5
+ def initialize: (Class klass) -> String
6
+
7
+ def add_states: (Array[String] states, ?::Hash[untyped, untyped] opts) -> String
8
+ def add_events: (Array[String] events) -> Array[String]
9
+ def add_active_record_relation: () -> String
7
10
 
8
11
  def new_line: () -> String
9
12
  def finalize: () -> String
@@ -11,10 +14,10 @@ module AasmRbs
11
14
  private
12
15
 
13
16
  attr_reader klass: Class
14
- attr_accessor data: String
17
+ attr_writer data: String
15
18
 
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
+ def add_state_constants: (Array[String] states) -> String
20
+ def add_state_scopes: (Array[String] states) -> String
21
+ def add_predicate_states_methods: (Array[String] states) -> String
19
22
  end
20
23
  end
data/sig/aasm_rbs.rbs CHANGED
@@ -1,5 +1,8 @@
1
1
  module AasmRbs
2
- def self.run: (String) -> String?
2
+ Rake: untyped # there is yet no Rake official RBS
3
+
4
+ def self.load_constants: (String klass_name) -> void
5
+ def self.run: (String klass_name, ?::Hash[untyped, untyped] opts) -> String?
3
6
  end
4
7
 
5
8
  # There are yet no official RBS signatures for the AASM module that gets
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm_rbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorenzo Zabot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-03 00:00:00.000000000 Z
11
+ date: 2023-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aasm
@@ -67,5 +67,5 @@ requirements: []
67
67
  rubygems_version: 3.3.7
68
68
  signing_key:
69
69
  specification_version: 4
70
- summary: AASM RBS
70
+ summary: RBS signatures for AASM classes
71
71
  test_files: []