aasm_rbs 0.2.0 → 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: 5ddfc302bf61720f7444bbd35a41071341e1bccf688c1c2132c3e87545360125
4
- data.tar.gz: d4b4678955f896fef85efd6d9011563df8f09c79d36e84979f22d4f009a226ec
3
+ metadata.gz: d13405faad77a8c8a21b0ee27b74c3b57acfc470c444ceed6c485421607a13b3
4
+ data.tar.gz: 9578eea618f8dc30c6164f90712435b616562bb2af422db57b951d792a2f07f8
5
5
  SHA512:
6
- metadata.gz: fb93b9cf0fa7a90dc315ebcf13d48cab132539f01cd4235784e16224a6ea6fe8dd72834e083f561e54d341399d00647ccb11305d18ce7a4c7e3dd7a4a6588566
7
- data.tar.gz: a0488476811cd0bcc2a1aefddd06c96a34ade62c8aeb797d8df7aea13354111b72f8a002a0a91a612015376010f2332217408513123658bcc55967f54b45b9ae
6
+ metadata.gz: f3a0b8d07358a4381579f2e5a42c6b2c633c73263a8cc0b7cc23de615e343c2cb1ea99ea1470e1a9a062e50e16584fed392aa67a268e8fe3aa38f04f3e572043
7
+ data.tar.gz: 7c84da47ecb1afc0b4443c13628e2ceb0efff26038d2e5fde2602dffa3a001c38da9c31a6675a0d62124d576f95b888c924ea51ffee5aac06925054fe76d722a
data/exe/aasm_rbs CHANGED
@@ -1,7 +1,7 @@
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'
@@ -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.2.0'
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 '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,8 +1,8 @@
1
1
  module AasmRbs
2
2
  Rake: untyped # there is yet no Rake official RBS
3
3
 
4
- def self.load_constants: (String) -> void
5
- def self.run: (String) -> String?
4
+ def self.load_constants: (String klass_name) -> void
5
+ def self.run: (String klass_name, ?::Hash[untyped, untyped] opts) -> String?
6
6
  end
7
7
 
8
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.2.0
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: []