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

Fix attribute translator setter #2375

Merged
merged 1 commit into from
Dec 21, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#### Fixes

* [#2375](https://github.com/ruby-grape/grape/pull/2375): Fix setter methods for `Grape::Router::AttributeTranslator` - [@Jell](https://github.com/Jell).
* [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
* [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia).
Expand Down
8 changes: 4 additions & 4 deletions lib/grape/router/attribute_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def to_h
end

def method_missing(method_name, *args)
if setter?(method_name[-1])
attributes[method_name[0..]] = *args
if setter?(method_name)
attributes[method_name.to_s.chomp('=').to_sym] = args.first
else
attributes[method_name]
end
end

def respond_to_missing?(method_name, _include_private = false)
if setter?(method_name[-1])
if setter?(method_name)
true
else
@attributes.key?(method_name)
Expand All @@ -56,7 +56,7 @@ def respond_to_missing?(method_name, _include_private = false)
private

def setter?(method_name)
method_name[-1] == '='
method_name.end_with?('=')
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/router/attribute_translator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

describe Grape::Router::AttributeTranslator do
(Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES + Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES).each do |attribute|
describe "##{attribute}" do
it "returns value from #{attribute} key if present" do
translator = described_class.new(attribute => 'value')
expect(translator.public_send(attribute)).to eq('value')
end

it "returns nil from #{attribute} key if missing" do
translator = described_class.new
expect(translator.public_send(attribute)).to be_nil
end
end

describe "##{attribute}=" do
it "sets value for #{attribute}", :aggregate_failures do
translator = described_class.new(attribute => 'value')
expect do
translator.public_send("#{attribute}=", 'new_value')
end.to change(translator, attribute).from('value').to('new_value')
end
end
end
end