Skip to content

Commit

Permalink
Fix state machine types
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwise-aiden committed Jul 19, 2024
1 parent ee9d273 commit 503a113
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/tapioca/dsl/compilers/state_machines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ def define_state_accessor(instance_module, machine, state_type)
instance_module.create_method(
attribute,
return_type: state_type,
)
) if ::StateMachines::HelperModule === machine.owner_class.instance_method(attribute).owner
instance_module.create_method(
"#{attribute}=",
parameters: [create_param("value", type: state_type)],
return_type: state_type,
)
) if ::StateMachines::HelperModule === machine.owner_class.instance_method("#{attribute}=").owner
end

sig { params(instance_module: RBI::Module, machine: ::StateMachines::Machine).void }
Expand Down
38 changes: 38 additions & 0 deletions spec/tapioca/dsl/compilers/state_machines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,44 @@ def state_event_transition=(value); end

assert_includes(rbi_for(:Vehicle), expected)
end

it "generates an RBI with no machine state reader if reader defined on class" do
add_ruby_file("vehicle.rb", <<~RUBY)
class Vehicle
attr_reader :state
state_machine :state
end
RUBY

reader = indented(<<~RBI, 4)
def state; end
RBI
refute_includes(rbi_for(:Vehicle), reader)

writer = indented(<<~RBI, 4)
def state=(value); end
RBI
assert_includes(rbi_for(:Vehicle), writer)
end

it "generates an RBI with no machine state writer if writer defined on class" do
add_ruby_file("vehicle.rb", <<~RUBY)
class Vehicle
attr_writer :state
state_machine :state
end
RUBY

reader = indented(<<~RBI, 4)
def state; end
RBI
assert_includes(rbi_for(:Vehicle), reader)

writer = indented(<<~RBI, 4)
def state=(value); end
RBI
refute_includes(rbi_for(:Vehicle), writer)
end
end
end
end
Expand Down

0 comments on commit 503a113

Please sign in to comment.