Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip generating methods that State Machine does not generate #1945

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading