Skip to content

Commit

Permalink
Fix Object.delegate to setter (#5964)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and RX14 committed Apr 19, 2018
1 parent 591580e commit 34078b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
14 changes: 14 additions & 0 deletions spec/std/object_spec.cr
Expand Up @@ -98,6 +98,13 @@ private class TestObject
end
end

private class DelegatedTestObject
delegate "property1=", to: @test_object

def initialize(@test_object : TestObject)
end
end

private class TestObjectWithFinalize
property key : Symbol?

Expand Down Expand Up @@ -129,6 +136,13 @@ describe Object do
end
matches.should eq(["l", "l"])
end

it "delegates setter" do
test_object = TestObject.new
delegated = DelegatedTestObject.new(test_object)
delegated.property1 = 42
test_object.property1.should eq 42
end
end

describe "getter" do
Expand Down
20 changes: 13 additions & 7 deletions src/object.cr
Expand Up @@ -1083,15 +1083,21 @@ class Object
# ```
macro delegate(*methods, to object)
{% for method in methods %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options)
end
{% if method.id.ends_with?('=') %}
def {{method.id}}(arg)
{{object.id}}.{{method.id}} arg
end
{% else %}
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options)
end

def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
def {{method.id}}(*args, **options)
{{object.id}}.{{method.id}}(*args, **options) do |*yield_args|
yield *yield_args
end
end
end
{% end %}
{% end %}
end

Expand Down

0 comments on commit 34078b8

Please sign in to comment.