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

Relax delegate's setter detection #14282

Merged
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
36 changes: 35 additions & 1 deletion spec/std/object_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ private class StringWrapper
end
end

private EQ_OPERATORS = %w(<= >= == != []= ===)

private class TestObject
getter getter1
getter getter2 : Int32
Expand Down Expand Up @@ -113,6 +115,19 @@ private class TestObject
{key, value}
end

# NOTE: these methods are a syntax error in older versions
{% if compare_versions(Crystal::VERSION, "1.12.0-dev") >= 0 %}
{% for op in EQ_OPERATORS %}
def {{ op.id }}(*args, **opts)
[args, opts]
end

def {{ op.id }}(*args, **opts, &)
[args, opts, yield]
end
{% end %}
{% end %}

annotation TestAnnotation
end

Expand All @@ -130,7 +145,10 @@ end

private class DelegatedTestObject
delegate :property1=, to: @test_object
delegate :[]=, to: @test_object

{% for op in EQ_OPERATORS %}
delegate {{ op.id.symbolize }}, to: @test_object
{% end %}

def initialize(@test_object : TestObject)
end
Expand Down Expand Up @@ -206,6 +224,22 @@ describe Object do
delegated = DelegatedTestObject.new(test_object)
(delegated["foo"] = "bar").should eq({"foo", "bar"})
end

{% if compare_versions(Crystal::VERSION, "1.12.0-dev") >= 0 %}
{% for op in EQ_OPERATORS %}
it "forwards \#{{ op.id }} with multiple parameters" do
test_object = TestObject.new
delegated = DelegatedTestObject.new(test_object)
delegated.{{ op.id }}(1, 2, a: 3, b: 4).should eq [{1, 2}, {a: 3, b: 4}]
end

it "forwards \#{{ op.id }} with multiple parameters and block parameter" do
test_object = TestObject.new
delegated = DelegatedTestObject.new(test_object)
delegated.{{ op.id }}(1, 2, a: 3, b: 4) { 5 }.should eq [{1, 2}, {a: 3, b: 4}, 5]
end
{% end %}
{% end %}
end

describe "getter" do
Expand Down
41 changes: 31 additions & 10 deletions src/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1293,24 +1293,45 @@ class Object
# wrapper.capitalize # => "Hello"
# ```
macro delegate(*methods, to object)
{% for method in methods %}
{% if method.id.ends_with?('=') && method.id != "[]=" %}
def {{method.id}}(arg)
{{object.id}}.{{method.id}} arg
end
{% else %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options)
end
{% if compare_versions(Crystal::VERSION, "1.12.0-dev") >= 0 %}
{% eq_operators = %w(<= >= == != []= ===) %}
{% for method in methods %}
{% if method.id.ends_with?('=') && !eq_operators.includes?(method.id.stringify) %}
def {{method.id}}(arg)
{{object.id}}.{{method.id}} arg
end
{% else %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options)
end

{% if method.id != "[]=" %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
end
end
{% end %}
{% end %}
{% else %}
{% for method in methods %}
{% if method.id.ends_with?('=') && method.id != "[]=" %}
def {{method.id}}(arg)
{{object.id}}.{{method.id}} arg
end
{% else %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options)
end

{% if method.id != "[]=" %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
end
end
{% end %}
{% end %}
{% end %}
{% end %}
end

Expand Down
Loading